/************************************************************************* > File Name: double_link.c > Author: heathcliff > Mail: --------------------------- > Created Time: 2016年03月30日 星期三 11时15分05秒 ************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct DulNode { char name[20]; struct DulNode *prior,*next; }stud; stud *create(int n); void print(stud *head); void search(stud *head,int n); stud *delete(stud *head,int n); stud *insert(stud *head,int n); int main(void) { stud * head; int n = 3; head = create(n); print(head); // search(head,n); // head = delete(head,n); // print(head); head = insert(head,n); return 0; } stud *create(int n) { stud *p,*head,*s; int i; head = (stud *) malloc (sizeof(stud)); head->name[0] = '\0'; head->next = NULL; p = head; for(i = 0;i<n;i++){ s = (stud *) malloc (sizeof(stud)); printf("please input student's name:"); scanf("%s",p->name); /*将p和s链接起来*/ p->next = s; //指定后继节点 s->prior = p;//指定前驱节点 p = p->next;//p 向后移动一格 //p = s;//这个也是能够的 } p->next = NULL; head->prior = NULL; return head; } void print(stud *head) { stud *p; p = head; while(p != NULL){ printf("\n------------print--------\n"); printf("%4s\n",p->name); p = p->next; } } void search(stud *head,int n) { int ser,i = 0; stud *p; p = head; printf("\n请输入要寻找第几个(从0开始):"); scanf("%d",&ser); while(i != ser && i <= n){//此处不能写为i<n, //不然最后一个元素将没法查找到 i++; p = p->next; } if(i==n) printf("没找到\n"); else{ printf("已经找到,你要的信息是:\n"); printf("%s\n",p->name); } } stud *delete(stud *head,int n) { stud *p,*s; int del, i = 0; p = head; printf("请输入你要删除的信息的序号:"); scanf("%d",&del); while(i != del && i <= n){//此处不能写为i<n, //不然最后一个元素将没法查找到 s = p; i++; p = p->next; } if(i==n) printf("没找到\n"); else{ printf("已经找到,你要删除的信息是:\n"); printf("%s\n",p->name); /*开始删除*/ if(i == 0){ //说明删除的是头节点 head = p->next; head->prior = NULL; } else{ s->next = p->next; s->prior = p->prior; } printf("删除成功\n"); free(p); n--; } return head; } stud *insert(stud *head,int n) { stud *p,*s; p = head; int ser,i = 0; char ins[20]; char p_temp[20]; printf("\n请输入你要插入的信息:"); scanf("%s",ins); s = (stud *) malloc (sizeof(stud)); strcpy(s->name,ins); printf("\n请输入要插入***的后面:"); scanf("%d",&ser); while(i != ser){//此处不能写为i<n, //不然最后一个元素将没法查找到 i++; p = p->next; } if(i==n) printf("没找到\n"); else{ printf("已经找到,你要的信息是:\n"); printf("%s\n",p->name); printf("----------\n"); } s->next = p->next; //printf("p->next->name = %s\n",p->next->name); //printf("s->next->name = %s\n",s->next->name); p->next->prior = s; //printf("p->next->prior->name = %s\n",p->next->prior->name); s->prior = p; //负责链接 //printf("p->prior->name = %s\n",p->prior->name); p->next = s;//负责链接 //printf("p->next->name = %s\n",p->next->name); //printf("\np->next->prior->name = %s\n",p->next->prior->name); //printf("p->prior->next->name = %s\n",p->prior->next->name); //printf("p->next->name = %s\n",p->next->name); //printf("p->prior->name = %s\n",p->prior->name); p = head; while(p != NULL){ printf("\n------------print--------\n"); printf("%4s\n",p->name); p = p->next; } free(p); return head; }
在插入这里我想理顺一下,在各大博客、论坛上,全部的插入都长一个样,索性本身画了一个长得不同的(电脑画太麻烦了,手绘,不喜勿喷哈)
1.s->next = p->next;
2.p->next->prior = s;
3.s->prior = p;
4.p->next = s; spa