双链表的基本操作

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 typedef struct line{
  4     struct line * prior;
  5     int data;
  6     struct line * next;
  7 }line;
  8 //双链表的创建
  9 line* initLine(line * head);
 10 //双链表插入元素,add表示插入位置
 11 line * insertLine(line * head,int data,int add);
 12 //双链表删除指定元素
 13 line * delLine(line * head,int data);
 14 //双链表中查找指定元素
 15 int selectElem(line * head,int elem);
 16 //双链表中更改指定位置节点中存储的数据,add表示更改位置
 17 line *amendElem(line * p,int add,int newElem);
 18 //输出双链表的实现函数
 19 void display(line * head);
 20 int main() {
 21     line * head=NULL;
 22     //创建双链表
 23     head=initLine(head);
 24     display(head);
 25     //在表中第 3 的位置插入元素 7
 26     head=insertLine(head, 7, 3);
 27     display(head);
 28     //表中删除元素 2
 29     head=delLine(head, 2);
 30     display(head);
 31 
 32     printf("元素 3 的位置是:%d\n",selectElem(head,3));
 33     //表中第 3 个节点中的数据改为存储 6
 34     head = amendElem(head,3,6);
 35     display(head);
 36     return 0;
 37 }
 38 line* initLine(line * head){
 39     head=(line*)malloc(sizeof(line));
 40     head->prior=NULL;
 41     head->next=NULL;
 42     head->data=1;
 43     line * list=head;
 44     for (int i=2; i<=5; i++) {
 45         line * body=(line*)malloc(sizeof(line));
 46         body->prior=NULL;
 47         body->next=NULL;
 48         body->data=i;
 49 
 50         list->next=body;
 51         body->prior=list;
 52         list=list->next;
 53     }
 54     return head;
 55 }
 56 line * insertLine(line * head,int data,int add){
 57     //新建数据域为data的结点
 58     line * temp=(line*)malloc(sizeof(line));
 59     temp->data=data;
 60     temp->prior=NULL;
 61     temp->next=NULL;
 62     //插入到链表头,要特殊考虑
 63     if (add==1) {
 64         temp->next=head;
 65         head->prior=temp;
 66         head=temp;
 67     }else{
 68         line * body=head;
 69         //找到要插入位置的前一个结点
 70         for (int i=1; i<add-1; i++) {
 71             body=body->next;
 72         }
 73         //判断条件为真,说明插入位置为链表尾
 74         if (body->next==NULL) {
 75             body->next=temp;
 76             temp->prior=body;
 77         }else{
 78             body->next->prior=temp;
 79             temp->next=body->next;
 80             body->next=temp;
 81             temp->prior=body;
 82         }
 83     }
 84     return head;
 85 }
 86 line * delLine(line * head,int data){
 87     line * temp=head;
 88     //遍历链表
 89     while (temp) {
 90         //判断当前结点中数据域和data是否相等,若相等,摘除该结点
 91         if (temp->data==data) {
 92             temp->prior->next=temp->next;
 93             temp->next->prior=temp->prior;
 94             free(temp);
 95             return head;
 96         }
 97         temp=temp->next;
 98     }
 99     printf("链表中无该数据元素");
100     return head;
101 }
102 //head为原双链表,elem表示被查找元素
103 int selectElem(line * head,int elem){
104 //新建一个指针t,初始化为头指针 head
105     line * t=head;
106     int i=1;
107     while (t) {
108         if (t->data==elem) {
109             return i;
110         }
111         i++;
112         t=t->next;
113     }
114     //程序执行至此处,表示查找失败
115     return -1;
116 }
117 //更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
118 line *amendElem(line * p,int add,int newElem){
119     line * temp=p;
120     //遍历到被删除结点
121     for (int i=1; i<add; i++) {
122         temp=temp->next;
123     }
124     temp->data=newElem;
125     return p;
126 }
127 //输出链表的功能函数
128 void display(line * head){
129     line * temp=head;
130     while (temp) {
131         if (temp->next==NULL) {
132             printf("%d\n",temp->data);
133         }else{
134             printf("%d->",temp->data);
135         }
136         temp=temp->next;
137     }
138 }

猜你喜欢

转载自www.cnblogs.com/wy0526/p/11788493.html
今日推荐