六月周末作业

 1 #include "./head.h"                                                                                
 2                                                                                                    
 3 int main(int argc, const char *argv[])                                                             
 4 {                                                                                                  
 5                                                                                                    
 6     //链表的初始化                                                                                 
 7     int option = 1;                                                                                
 8     int select = -1;                                                                               
 9     int value = 0;                                                                                 
10     int number = 0;                                                                                
11     linklist head = initialize(option);                                                            
12     printf("链表初始化完成\n");                                                                    
13     do                                                                                             
14     {                                                                                              
15         printf("选择功能>>>\n");                                                                   
16         printf("****   0>>>退出程序            ****\n");                                           
17         printf("****   1>>>遍历链表            ****\n");                                           
18         printf("****   2>>>链表的插入          ****\n");                                           
19         printf("****   3>>>链表的删除          ****\n");                                           
20         printf("****   4>>>按指定位置操作链表  ****\n");                                           
21         printf("select>>>");                                                                       
22         scanf("%d",&select);                                                                       
23         if(select == 0)                                                                            
24             break;                                                                                 
25         else if(1 == select)                                                                       
26         {                                                                                          
27             int num1 = ergodic(head);                                                              
28             printf("共有%d个节点\n",num1);                                                         
29         }                                                                                          
30         else if( 2 == select)                                                                      
31         {                                                                                          
32             printf("option: 0:头插法 1:尾插法>>>");                                                
33             scanf("%d",&option);                                                                   
34             printf("value>>>");                                                                    
35             scanf("%d",&value);                                                                    
36             head = insert(head, option, value);                                                    
37             if(NULL == head)                                                                       
38             {                                                                                      
39                 printf("节点插入失败\n");                                                          
40                 break;                                                                             
41             }                                                                                      
42             printf("节点插入成功\n");                                                              
43         }                                                                                          
44         else if(3 == select)                                                                       
45         {                                                                                          
46             printf("opti删除>>>");                                                                 
47             scanf("%d",&option);                                                                   
48             printf("number(要删除的个数)>>>");                                                     
49             scanf("%d",&number);                                                                   
50             head = delete(head, option, number);                                                   
51             if(NULL == head)                                                                       
52             {                                                                                      
53                 printf("删除失败\n");                                                              
54                 break;                                                                             
55             }                                                                                      
56             printf("删除成功\n");                                                                  
57         }                                                                                          
58         else if(4 == select)                                                                       
59         {                                                                                          
60             int postion = 0;                                                                       
61             printf("select: 0:在指定位置插入 1:删除指定位置>>>");                                  
62             scanf("%d",&option);                                                                   
63             printf("postion>>>");                                                                  
64             scanf("%d",&postion);                                                                  
65             printf("value>>>");                                                                    
66             scanf("%d",&value);                                                                    
67             head = positionoperation(head, option, postion, value);                                
68             if(NULL == head)                                                                       
69             {                                                                                      
70                 printf("操作失败\n");                                                              
71                 break;                                                                             
72             }                                                                                      
73             printf("操作成功\n");                                                                  
74         }                                                                                          
75         else                                                                                       
76         {                                                                                          
77             printf("select输入错误\n");                                                            
78             break;                                                                                 
79         }                                                                                          
80                                                                                                    
81     }while(select != 0);                                                                           
82                                                                                                    
83     free(head);                                                                                    
84     head =NULL;                                                                                    
85                                                                                                    
86     return 0;                                                                                      
87 }                                                                                                  
                                                                                                      
                                                                                                      
                                                                                                      
                                                                                                      
                                                                                                      
                                                                                                      
                                                                                                      
|  1 #ifndef _HEAD_
|  2 #define _HEAD_
|  3 
|  4 #include <stdio.h>
|  5 #include <stdlib.h>
|  6 
|  7 typedef int elemtype;
|  8 
|  9 
| 10 //链表的定义
| 11 typedef struct linklist
| 12 {
| 13     union
| 14     {
| 15         elemtype data;//普通节点的数据域
| 16         int len;//头结点数据域记录长度                                                                                                                                                                                               
| 17     }datado;
| 18     struct linklist* next;//节点地址域
| 19 }llist, *linklist;
| 20 
| 21 
| 22 linklist initialize(int option);//链表的初始化
| 23 
| 24 int ergodic(llist* head);//链表的遍历
| 25 
| 26 linklist insert(linklist head, int option, int value);//链表的插入
| 27 
| 28 linklist delete(linklist head, int option, int number);//链表
| 29 
| 30 linklist positionoperation(linklist head, int option, int postion, int value);//链表的按位置操作
| 31 
| 32 
| 33 
| 34 
| 35 #endif
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
|~                                                                                                                                                                                                                                        
  1 de "head.h"                                                                           
  2                                                                                       
  3 节点的大小                                                                            
  4 ze = sizeof(llist);                                                                   
  5                                                                                       
  6                                                                                       
  7                                                                                       
  8 的初始化                                                                              
  9 st initialize(int option)                                                             
 10                                                                                       
 11 (1 == option)                                                                         
 12                                                                                       
 13   llist* head = (linklist)malloc(size);                                               
 14   if(NULL == head)                                                                    
 15       return NULL;                                                                    
 16   head->next = NULL;                                                                  
 17   head->datado.len = 0;                                                               
 18   return head;                                                                        
 19                                                                                       
 20 se                                                                                    
 21                                                                                       
 22   printf("option输入错误。\n");                                                       
 23   return NULL;                                                                        
 24                                                                                       
 25                                                                                       
 26                                                                                       
 27                                                                                       
 28 的遍历                                                                                
 29 godic(llist* head)                                                                    
 30                                                                                       
 31 t count = 0, i = 0;                                                                   
 32 ist* p = head;                                                                        
 33 ile(p->next != NULL)                                                                  
 34                                                                                       
 35   count++;                                                                            
 36   i++;                                                                                
 37   p = p->next;                                                                        
 38   printf("list[%d] = %d\n",i,p->datado.data);                                         
 39                                                                                       
 40 turn count;//返回链表中节点的个数                                                     
 41                                                                                       
 42                                                                                       
 43                                                                                       
 44 的插入 0:头插法 1:尾插法                                                              
 45 st insert(linklist head, int option, int value)                                       
 46                                                                                       
 47 (0 == option)//头插法                                                                 
 48                                                                                       
 49   llist* l = (llist*)malloc(size);                                                    
 50   if(NULL == l)                                                                       
 51       return NULL;                                                                    
 52   l->datado.data = value;                                                             
 53   l->next = head->next;                                                               
 54   head->next = l;                                                                     
 55   return head;                                                                        
 56                                                                                       
 57 se if(1 == option)//尾插法                                                            
 58                                                                                       
 59   llist* l = (llist*)malloc(size);                                                    
 60   if(NULL == l)                                                                       
 61       return NULL;                                                                    
 62   llist* p = head;                                                                    
 63   while(p->next != NULL)                                                              
 64       p = p->next;                                                                    
 65   l->datado.data = value;                                                             
 66   p->next = l;                                                                        
 67   l->next = NULL;                                                                     
 68   return head;                                                                        
 69                                                                                       
 70 se                                                                                    
 71                                                                                       
 72   printf("option输入错误。\n");                                                       
 73   return head;                                                                        
 74                                                                                       
 75                                                                                       
 76                                                                                       
 77                                                                                       
 78 的删除 0:从头开始删除 1:从尾开始删除                                                  
 79 st delete(linklist head, int option, int number)                                      
 80                                                                                       
 81 (0 == option)                                                                         
 82                                                                                       
 83   linklist p = head;                                                                  
 84   for(int i = 0; i < number && (p->next != NULL); i++)                                
 85       p = p->next;                                                                    
 86   head->next = p->next;                                                               
 87                                                                                       
 88 se if(1 == option)                                                                    
 89                                                                                       
 90   int i = 0;                                                                          
 91   while(number > 0)                                                                   
 92   {                                                                                   
 93       linklist p = head->next;                                                        
 94       linklist q = head;                                                              
 95       while(p != NULL)                                                                
 96       {                                                                               
 97           p = p->next;                                                                
 98           q = q->next;                                                                
 99       }                                                                               
100       i++;                                                                            
101       printf("倒数第%d个节点 = %d\n",i,p->datado.data);                               
102       q->next = NULL;                                                                 
103   }                                                                                   
104   return head;                                                                        
105                                                                                       
106 se                                                                                    
107                                                                                       
108   printf("option输入错误。\n");                                                       
109   return head;                                                                        
110                                                                                       
111                                                                                       
112                                                                                       
113                                                                                       
114 置操作 0:在指定位置插入  1:删除指定位置                                              
115 st positionoperation(linklist head, int option, int postion, int value)               
116                                                                                       
117 (0 == option)                                                                         
118                                                                                       
119   int i = 0;                                                                          
120   linklist p = head;                                                                  
121   while((i < postion-1) && (p->next != NULL))                                         
122   {                                                                                   
123       i++;                                                                            
124       p = p->next;                                                                    
125   }                                                                                   
126   if(i < postion-1 && (p->next == NULL))                                              
127   {                                                                                   
128       printf("没有这个位置。\n");                                                     
129       return NULL;                                                                    
130   }                                                                                   
131   else                                                                                
132   {                                                                                   
133       linklist q = (linklist)malloc(size);                                            
134       if(NULL == q)                                                                   
135           return NULL;                                                                
136       q->datado.data = value;                                                         
137       q->next = p->next;                                                              
138       p->next = q;                                                                    
139       head->datado.len++;                                                             
140       return head;                                                                    
141   }                                                                                   
142                                                                                       
143 se if(1 == option)                                                                    
144                                                                                       
145   int i = 0;                                                                          
146   linklist p = head;                                                                  
147   while(i < (postion-1) && (p->next != NULL))                                         
148   {                                                                                   
149       i++;                                                                            
150       p = p->next;                                                                    
151   }                                                                                   
152   if(i < (postion-1) && (p->next != NULL))                                            
153   {                                                                                   
154       printf("位置不对.\n");                                                          
155       return NULL;                                                                    
156   }                                                                                   
157   else                                                                                
158   {                                                                                   
159       p->next = p->next->next;                                                        
160       head->datado.len--;                                                             
161       return head;                                                                    
162   }                                                                                   
163                                                                                       
164 se                                                                                    
165                                                                                       
166   printf("option输入错误.\n");                                                        
167   return head;                                                                        
168                                                                                       
169                                                                                       
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值