双向链表基本操作
header.h头文件
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 10 5 typedef struct node 6 { 7 int key; 8 struct node* next; 9 struct node* prior; 10 }DL; 11 DL* init();//初始化 12 void creat_link(DL* h);//创建链表 13 void print(DL* h);//打印 14 DL * get_Node(DL *h); 15 DL * get_Nodepos(DL *h,int pos); 16 DL* insert_val(int val); 17 void delet_pos(DL *h); 18 void delet_val(DL *h); 19 void insert_head(DL * h); 20 void insert_end(DL *h); 21 void insert_pos(DL* h); 22 void search( DL*h);
function.c文件
1 #include"header.h" 2 DL* init() 3 { 4 //创建头节点 5 DL* h = (DL*)malloc(sizeof(DL)); 6 h->next = NULL; 7 h->prior = NULL; 8 return h; 9 } 10 DL* create_node() 11 { 12 DL *p; 13 p = (DL*)malloc(sizeof(DL)); 14 if(p == NULL) 15 { 16 printf("动态内存分配失败!\n"); 17 exit(0); 18 } 19 20 p->key = rand()%101; 21 p->prior = NULL; 22 p->next = NULL; 23 return (p); 24 } 25 26 DL* insert_val(int val) 27 { 28 DL *p; 29 p = (DL*)malloc(sizeof(DL)); 30 if(p == NULL) 31 { 32 printf("动态内存分配失败!\n"); 33 exit(0); 34 } 35 36 p->key =val; 37 p->prior = NULL; 38 p->next = NULL; 39 return (p); 40 } 41 42 void creat_link(DL* h) 43 { 44 DL * p,* newnode; 45 int i =0; 46 p = h; 47 //带头结点的尾插法 48 srand((unsigned)time(NULL)); 49 while(i<N) 50 { 51 newnode = create_node(); 52 p->next = newnode; 53 newnode->prior = p; 54 p = newnode; 55 i++; 56 } 57 } 58 59 DL * get_Node(DL *h) 60 { 61 int i = 0,count = 1; 62 DL* p = h->next; 63 printf("请输入要获取第几个节点的元素\n"); 64 scanf("%d",&i); 65 while(p) 66 { 67 if(count == i) 68 { 69 printf("%-5d",p->key); 70 break; 71 } 72 else 73 { 74 count++; 75 p = p->next; 76 } 77 } 78 return p; 79 } 80 81 DL * get_Nodepos(DL *h,int pos) 82 { 83 int i = 0,count = 1; 84 DL* p = h->next; 85 86 while(p) 87 { 88 if(count == pos) 89 { 90 printf("%-5d",p->key); 91 break; 92 } 93 else 94 { 95 count++; 96 p = p->next; 97 } 98 } 99 return p; 100 } 101 102 103 //删除指定位置的元素 104 void delet_pos(DL *h) 105 { 106 int pos; 107 DL* p = h->next; 108 DL * pre; 109 int count = 1; 110 printf("请输入要删除节点的位置\n"); 111 scanf("%d",&pos); 112 while(pos != count) 113 { 114 p = p->next; 115 count++; 116 } 117 pre = p->prior; 118 pre->next = p->next; 119 p->next->prior = pre; 120 free(p); 121 } 122 123 124 void delet_val(DL *h) 125 { 126 int val; 127 DL* p = h->next; 128 DL * pre; 129 printf("请输入要删除的值\n"); 130 scanf("%d",&val); 131 132 while(p) 133 { 134 if(val == p->key) 135 break; 136 else 137 p = p->next; 138 } 139 if(p ==NULL) 140 printf("没有找到这个元素\n"); 141 else 142 { 143 pre = p->prior; 144 pre->next = p->next; 145 p->next->prior = pre; 146 free(p); 147 } 148 149 } 150 151 void insert_head(DL *h) 152 { 153 int val; 154 DL* p = h->next; 155 DL * newnode; 156 printf("请输入插入的值\n"); 157 scanf("%d",&val); 158 newnode = insert_val(val); 159 160 newnode->next = h->next; 161 p->prior = newnode; 162 newnode->prior = h; 163 h->next = newnode; 164 165 } 166 void insert_end(DL *h) 167 { 168 int val; 169 DL* p = h->next; 170 DL * newnode; 171 printf("请输入插入的值\n"); 172 scanf("%d",&val); 173 newnode = insert_val(val); 174 while(p->next) 175 p = p->next; 176 177 p->next = newnode; 178 newnode->prior = p; 179 180 } 181 182 void insert_pos(DL *h) 183 { 184 int val,pos; 185 DL* p = h->next,*insert; 186 DL * newnode; 187 printf("请输入插入的值\n"); 188 scanf("%d",&val); 189 printf("请输入插入的位置\n"); 190 scanf("%d",&pos); 191 insert = get_Nodepos(h,pos); 192 newnode = insert_val(val); 193 194 if(pos == 1) 195 { 196 newnode->next = h->next; 197 p->prior = newnode; 198 newnode->prior = h; 199 h->next = newnode; 200 } 201 else 202 { 203 insert->prior->next = newnode; 204 newnode->prior = insert->prior; 205 newnode->next = insert; 206 insert->prior = newnode; 207 } 208 209 } 210 211 void search(DL* h) 212 { 213 int val; 214 DL* p = h->next; 215 printf("请输入查询的值\n"); 216 scanf("%d",&val); 217 while(p) 218 { 219 if(p->key == val) 220 break; 221 else 222 p = p->next; 223 } 224 if(p) 225 printf("Find it\n"); 226 else 227 printf("NOT EXIST\n"); 228 } 229 230 void print(DL* h) 231 { 232 DL*p =h->next; 233 printf("链表是:"); 234 while(p) 235 { 236 printf("%-5d",p->key); 237 p = p->next; 238 } 239 }
main.c文件
1 #include"header.h" 2 void main() 3 { 4 DL*h = init(); 5 int choice; 6 while(1) 7 { 8 choice = 0; 9 printf("\n\n\n\n\n\n\n\n"); 10 printf("========================================================================\n"); 11 printf("按提示输入您的操作\n"); 12 printf("1:初始化线性表\n"); 13 printf("2:输出链表\n"); 14 printf("3:取链表中第i个元素的键值\n"); 15 printf("4:删除指定位置的元素\n"); 16 printf("5:删除指定键值的元素\n"); 17 printf("6:表头添加键值为key的元素\n"); 18 printf("7:表尾添加键值为key的元素\n"); 19 printf("8:向表中指定的位置POS处添加键值为key的元素\n"); 20 printf("9:搜索键值为key的元素,判断是否存在\n"); 21 printf("输入999退出\n"); 22 printf("========================================================================\n"); 23 24 printf("请输入你的操作:"); 25 scanf("%d",&choice); 26 if(choice==999) 27 break; 28 switch(choice) 29 { 30 case 1:system("cls");creat_link(h);break; 31 case 2:system("cls");print(h);break; 32 case 3:system("cls");get_Node(h);break; 33 case 4:delet_pos(h);break; 34 case 5:delet_val(h);break; 35 case 6:insert_head(h);break; 36 case 7:insert_end(h);break; 37 case 8:insert_pos(h);break; 38 case 9:search(h);break; 39 default:break; 40 } 41 42 } 43 }

浙公网安备 33010602011771号