独立思考_潜心修行

AllenNewOK

常用链接

统计

最新评论

单链表的创建、计数打印、删除与插入操作


单链表的创建、计数打印、删除与插入操作,提供四轮删除与插入操作,gcc编译通过。

 

  1 #include<stdio.h>
  2 #include<stdlib.h>  /*使用到其中的malloc和exit函数*/
  3 #define times 4  /*用于循环次数的控制*/
  4 
  5 static int N=4;  /*静态全局变量,用于控制单链表长度*/
  6 
  7 typedef struct _person
  8 {
  9     char name[12];
 10     int age;
 11     struct _person *next;
 12 }stud;
 13 
 14 stud *Create(int num)  /*创建单链表的函数,num为单链表的长度*/
 15 {
 16     int i;
 17     stud *h,*p,*q;  /* h为头指针,指向单链表的第一个节点*/
 18     h=(stud*)malloc(sizeof(stud));
 19     if(h!=NULL)
 20     {
 21         p=h;
 22         for(i=0;i<num;i++)
 23         {
 24             q=(stud*)malloc(sizeof(stud));  /* q为指向新建节点的指针*/
 25             if(q!=NULL)
 26             {
 27                 printf("依次输入第%d个人的姓名和年龄:\n",i+1);
 28                 scanf("%s%d",q->name,&q->age);
 29                 q->next=NULL;  /*创建新节点完毕*/
 30                 p->next=q;
 31                 p=q;
 32             }
 33         }
 34     }
 35     printf("\n");
 36     return(h);
 37 }
 38 
 39 stud *Delete(stud *person,int post)  /*删除单链表指定位置节点的函数*/
 40 {
 41     int i;
 42     stud *cur,*pre;
 43     cur=person;
 44 
 45     if(0==post)  /*如果输入的值为0,则不删除任何节点*/
 46     {
 47         printf("\n注意:您决定不删除任何节点!!!\n\n");
 48         return(person);
 49     }
 50     else if(post>N||post<0)  /*如果输入的值大于单链表长度或者小于0,程序结束*/
 51     {
 52         printf("输入有误,程序终止。\n");
 53         exit(1);
 54     }
 55     else
 56     {
 57         if(1==post)  /*在单链表头部删除的情况*/
 58         {
 59             cur=cur->next;
 60             person->next=cur->next;
 61             free(cur);
 62         }
 63         else  /*在其它位置删除的情况*/
 64         {
 65             for(i=2;i<post+1;i++)  /*使pre成为要插入位置的上一位置的节点*/
 66             {
 67                 cur=cur->next;
 68                 pre=cur;
 69             }
 70             cur=cur->next;
 71             pre->next=cur->next;
 72             free(cur);
 73         }
 74         return(person);
 75     }
 76 }
 77 
 78 stud *Insert(stud *person,int post)  /*在单链表指定位置插入新的节点的函数*/
 79 {
 80     int i;
 81     stud *cur,*pre,*node;
 82     
 83     if(post>N+1||post<1)  /*如果输入的值大于单链表长度加1或者小于1,程序结束*/
 84     {
 85         printf("输入错误,程序终止。\n");
 86         exit(1);
 87     }
 88 
 89     if(person!=NULL)
 90     {
 91         cur=person;
 92         node=(stud*)malloc(sizeof(stud));
 93         if(node!=NULL)
 94         {
 95             printf("请输入新人的姓名和年龄:\n");
 96             scanf("%s%d",node->name,&node->age);  /*为新的节点输入数据内容*/
 97 
 98             if(1==post)
 99             {
100                 node->next=person->next;
101                 person->next=node;
102             }
103             else
104             {
105                 for(i=2;i<post+2;i++)
106                 {
107                     pre=cur;
108                     cur=cur->next;
109                 }
110                 node->next=pre->next;
111                 pre->next=node;
112             }
113         }
114     }
115     printf("\n");
116     return(person);
117 }
118 
119 void Print(stud *person)
120 {
121     int post=1;
122     stud *cur;
123     cur=person->next;
124     printf("当前的节点信息如下所示:\n");
125     while(cur!=NULL)
126     {
127         printf("第%d个人的姓名是:%s,年龄为:%d\n",post,cur->name,cur->age);
128         cur=cur->next;
129         post++;
130     }
131     N=--post;
132     printf("当前单链表的长度是:%d\n\n",N);
133 }
134 
135 int main()
136 {
137     int number,post,i;
138     stud *head;
139     head=Create(N);
140     Print(head);
141 
142     for(i=0;i<times;i++)
143     {
144         printf("请输入要删除的节点的位置:\n");
145         scanf("%d",&number);
146         Delete(head,number);
147         Print(head);
148 
149         printf("请输入要插入节点的位置(此位置是指预期插入成功后新节点在单链表中的位置):\n");
150         scanf("%d",&post);
151         Insert(head,post);
152         Print(head);
153     
154         printf("\n注意:剩余输入轮数为:%d  !!!!!\n\n",(times-(i+1)));
155     }
156 
157     return 0;
158 }

 


调试环境:Ubuntu Desktop 8.04.4   VIM 7.1.138 GCC 4.2.4
QQ:81064483
E-mail:AllenNewOK@126.com

不足之处,敬请指正。< ^_^ >

posted on 2010-09-12 13:00 AllenNewOK 阅读(1714) 评论(2)  编辑 收藏 引用

评论

# re: 单链表的创建、计数打印、删除与插入操作 2010-09-12 15:10 yugi

有意思吗  回复  更多评论   

# re: 单链表的创建、计数打印、删除与插入操作 2011-08-09 11:11 LeeVam

如果num 传入的数字为0 ,岂不是要内存泄露哦  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理