线性链表C

来源:互联网 发布:有道翻译无网络连接 编辑:程序博客网 时间:2024/05/22 05:20
#include<stdio.h>#include<malloc.h>#define NULL 0typedef int ElemType;typedef struct node{ElemType data;node *next;}*Node;void Creat_list(Node &head)//头插法插入新节点{int x;head->next=NULL;Node p=new node;printf("请输入数据,以0结束\n");scanf("%d",&x);while(x){Node u=new node;u->data=x;u->next=head->next;head->next=u;scanf("%d",&x);}}void Display_list(Node head){Node p=new node;p=head->next;printf("现在的LIST为:\n");while(p){printf("%d\n",p->data);p=p->next;}}void Insert_list(Node &head,ElemType x,int n){Node p=new node;p=head->next;int i=1;for(i=1;i<=n;i++)p=p->next;Node u=new node;u->data=x;u->next=p->next;p->next=u;}void Delete_list(Node &head,int n){   Node p=new node;   Node u=new node;p=head->next;int i=1;for(i=1;i<=n;i++)p=p->next;u=p->next;p->next=u->next;}void main(){printf("本程序是用头插法插入节点,因此反序乃正常现象\n");node head;Node p=new node;Creat_list(p);Display_list(p);Insert_list(p,677,2);Display_list(p);Delete_list(p,2);Display_list(p);}

0 0