本题目要求实现下面三个函数,实现如下功能:
输入若干个正整数,以-1结束,采取向链表中添加节点的方式来建立一个单向链表。
遍历并输出这个单向链表。
向链表尾部添加节点函数:
struct link *AppendNode(struct link *head,int data);
-
head:链表头节点指针,如果head为NULL,则会新建头节点
-
data:要添加的节点data数值
-
函数返回:添加过节点后的链表头节点指针
遍历显示链表函数:
void DisplyNode(struct link *head);
- head :链表头节点指针
删除链表内存函数:
void DeleteMemory(struct link *head);
- head:链表头节点指针
主函数样例:
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
int main()
{
char c;
int data = 0;
struct link *head = NULL;
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);
}
DisplyNode(head);
DeleteMemory(head);
return 0;
}
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
int main()
{
char c;
int data = 0;
struct link *head = NULL;
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);
}
DisplyNode(head);
DeleteMemory(head);
return 0;
}
head:链表头节点指针,如果head为NULL,则会新建头节点
data:要添加的节点data数值
函数返回:添加过节点后的链表头节点指针
void DisplyNode(struct link *head);
- head :链表头节点指针
删除链表内存函数:
void DeleteMemory(struct link *head);
- head:链表头节点指针
主函数样例:
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
int main()
{
char c;
int data = 0;
struct link *head = NULL;
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);
}
DisplyNode(head);
DeleteMemory(head);
return 0;
}
#include
#include
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
int main()
{
char c;
int data = 0;
struct link *head = NULL;
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);
}
DisplyNode(head);
DeleteMemory(head);
return 0;
}
#include#include struct link { int data; struct link *next; }; struct link *AppendNode(struct link *head,int data); void DisplyNode(struct link *head); void DeleteMemory(struct link *head); int main() { char c; int data = 0; struct link *head = NULL; while (1) { scanf("%d",&data); if (data==-1) break; head = AppendNode(head,data); } DisplyNode(head); DeleteMemory(head); return 0; }
#include#include struct link { int data; struct link *next; }; struct link *AppendNode(struct link *head,int data); void DisplyNode(struct link *head); void DeleteMemory(struct link *head); int main() { char c; int data = 0; struct link *head = NULL; while (1) { scanf("%d",&data); if (data==-1) break; head = AppendNode(head,data); } DisplyNode(head); DeleteMemory(head); return 0; }
#include#include struct link { int data; struct link *next; }; struct link *AppendNode(struct link *head,int data); void DisplyNode(struct link *head); void DeleteMemory(struct link *head); int main() { char c; int data = 0; struct link *head = NULL; while (1) { scanf("%d",&data); if (data==-1) break; head = AppendNode(head,data); } DisplyNode(head); DeleteMemory(head); return 0; } struct link *AppendNode(struct link *head,int data){ struct link *p=NULL, *q=head; p=(struct link*)malloc(sizeof(struct link)); if(p==NULL){ exit(0); } if(head==NULL){ head=p; } else{ while(q->next!=NULL){ q=q->next; } q->next=p; } p->data=data; p->next=NULL; return head; } void DisplyNode(struct link *head){ struct link*p; p=head; int i=1; while(p!=NULL) { if(i==1){ printf("%d",p->data); }else{ printf("->%d",p->data); } i++; p=p->next; } } void DeleteMemory(struct link *head) { struct link *p = NULL; struct link *pr = head; while(pr!=NULL) { p = pr; pr = pr->next; free(p); } }



