|
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct Node
{
int num;
struct Node *pnext;
};
void AddtoEnd(struct Node**pHead,struct Node **pEnd,int value);
void show_all(struct Node *phead);
void freeaa(struct Node *phead);
void AddtoHead(struct Node **phead, struct Node **pend, int value);
int main()
{
struct Node * head = NULL ;
struct Node * hend = NULL ;
AddtoEnd(&head, &hend, 5);
AddtoEnd(&head, &hend, 10);
AddtoEnd(&head, &hend, 28);
AddtoHead(&head, &hend, 9999);
//AddtoHead(&head, &hend, 6666);
show_all(head);
freeaa(head);
head = NULL;
hend = NULL;
while (1);
}
void AddtoEnd(struct Node ** pHead, struct Node ** pEnd, int value) //增加尾结点
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); //1创建节点,且注意malloc是否赋值成功
if (NULL != temp)
{
temp->num = value; //2节点赋值
temp->pnext = NULL;
if (NULL == *pHead || NULL == *pEnd) //空链表
{
*pHead = temp;
*pEnd = temp;
}
else //3连上节点
{
(*pEnd)->pnext = temp;
*pEnd = temp;
}
}
}
void show_all(struct Node *phead) //输出所有节点
{
while (phead != NULL)
{
printf("%d ", phead->num);
phead = phead->pnext;
}
printf("\n");
}
void freeaa(struct Node *phead) //释放所有节点
{
while (phead != NULL)
{
struct Node *pt = phead;
phead = phead->pnext;
free(pt);
}
}
void AddtoHead(struct Node **phead, struct Node **pend, int value) //增加头节点
{
struct Node *temp = (struct Node *)malloc(sizeof(temp));
temp->num = value;
temp->pnext = NULL;
if (NULL == *phead)
{
*phead = temp;
*pend = temp;
}
else
{
temp->pnext = *phead;
*phead = temp;
}
}
拜托各位了 |
上一篇: VS代码报错下一篇: vs2019的msdn为什么很多东西搜不到?
|