本题目要求实现下面三个函数,实现如下功能:
输入若干个正整数,以-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 <stdio.h>
#include <stdlib.h>
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);/* 向head为头指针的链表末尾添加节点 */
}
DisplyNode(head); /* 显示当前链表中的各节点信息 */
DeleteMemory(head); /* 释放所有动态分配的内存 */
return 0;
}
/*在此实现 struct link *AppendNode(struct link *head,int data); */
/*在此实现 void DisplyNode(struct link *head); */
/*在此实现 void DeleteMemory(struct link *head);*/
#include <stdio.h>
#include <stdlib.h>
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);/* 向head为头指针的链表末尾添加节点 */
}
DisplyNode(head); /* 显示当前链表中的各节点信息 */
DeleteMemory(head); /* 释放所有动态分配的内存 */
return 0;
}
/*在此实现 struct link *AppendNode(struct link *head,int data); */
/*在此实现 void DisplyNode(struct link *head); */
/*在此实现 void DeleteMemory(struct link *head);*/
#include <stdio.h>
#include <stdlib.h>
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);/* 向head为头指针的链表末尾添加节点 */
}
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);
}
}