#include<stdio.h>
#include<stdlib.h>
typedef struct{
char name[10];
int age;
int adress;
}Student;
typedef struct Node{
Student data;
struct Node * Next;
}Node;
int init_Node(Node **h)
{ if((*h=(Node *)malloc(sizeof(Node)))==NULL) return 0;
(*h)->Next=NULL;
return 1;
}
void Creat_list(Node *h)
{ Node *p,*r;
r=h;
p=NULL;
Student a[100];
int n,i;
printf("请输入单链表节点个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{printf("请输入第%d个节点的值:",i+1);
scanf("%s%d%d",a[i].name,&a[i].age,&a[i].adress);
}
for(i=0;i<n;i++)
{ p=(Node *)malloc(sizeof(Node));
if(p==NULL)
{return;}
p->data=a[i];
p->Next=NULL;
r->Next=p;
r=p;
}
}
void Delete_list(Node **h)
{ Node *p,*q;
p=(*h)->Next;
while(p)
{q=p->Next;
free(p);
p=q;
}
(*h)->Next=NULL;
}
void print_Node(Node *h)
{ Node *p=h->Next;
while(p!=NULL)
{ printf("%s%5d%5d\n",p->data.name,p->data.age,p->data.adress);
p=p->Next;
}
}
int main()
{Node *head;
int i;
init_Node(&head);
Creat_list(head);
print_Node(head);
Delete_list(&head);
print_Node(head);
return 0;
}
单链表的整表删除
最新推荐文章于 2022-01-28 20:57:05 发布