在前文实现单向链表的基本操作下,本文实现双向链表的基本操作.
双向链表与单链表差异,是双向链表结点中有前向指针和后向指针.所以在插入和删除新结点元素时候不见要考虑后向指针还要考虑前向指针.
以下是双向链表的C代码:
1 #include<stdio.h> 2 3 typedef struct node 4 { 5 int data; 6 struct node *next; 7 struct node *prior 8 }Node; 9 10 //链表的初始化 11 Node* InitList(int number) 12 { 13 int i; 14 Node *pHead=(Node *)malloc(sizeof(Node)); 15 Node *TempHead=pHead; 16 Node *Head=pHead; 17 Head->prior=NULL; 18 int data; 19 for(i=0;i<number;i++) 20 { 21 pHead=(Node *)malloc(sizeof(Node)); 22 printf("Please input the %dst node data: ",i+1); 23 scanf("%d",&data); 24 pHead->data=data; 25 pHead->next=NULL; 26 pHead->prior=TempHead; 27 TempHead->next=pHead; 28 TempHead=pHead; 29 } 30 return Head; 31 } 32 33 //显示链表 34 void ShowList(Node *Head) 35 { 36 37 Head=Head->next; 38 while(Head->next!=NULL) 39 { 40 printf("%d ",Head->data); 41 Head=Head->next; 42 } 43 printf("%d",Head->data); 44 printf(" "); 45 } 46 47 //输出链表某个值的位置 48 int ListLocation(Node *Head,int data,int number) 49 { 50 Node *TempNode=Head; 51 int location=1; 52 TempNode=TempNode->next; 53 while(TempNode->next!=NULL) 54 { 55 if(TempNode->data==data) 56 { 57 return location; 58 } 59 location++; 60 TempNode=TempNode->next; 61 } 62 if(location>=number) 63 printf("Not found!"); 64 } 65 66 //输出链表某个位置的值 67 int ListData(Node *Head,int location,int number) 68 { 69 if(location>number) 70 printf("Not found!"); 71 72 Node *TempNode=Head; 73 TempNode=TempNode->next; 74 int i; 75 for(i=1;i<=number;i++) 76 { 77 if(location==i) 78 return TempNode->data; 79 TempNode=TempNode->next; 80 } 81 } 82 83 //头入法插入元素 84 void HeadInsertData(Node *Head,int data) 85 { 86 Node *InsertNode=(Node *)malloc(sizeof(Node)); 87 InsertNode->data=data; 88 InsertNode->next=Head->next; 89 Head->next->prior=InsertNode; 90 Head->next=InsertNode; 91 InsertNode->prior=Head; 92 } 93 94 //尾入插入除元素 95 void TailInsertData(Node *Head,int data) 96 { 97 Node *TempNode=Head; 98 Node *DeleteNode=(Node *)malloc(sizeof(Node)); 99 DeleteNode->data=data; 100 while(TempNode->next!=NULL) 101 TempNode=TempNode->next; 102 103 TempNode->next=DeleteNode; 104 DeleteNode->next=NULL; 105 DeleteNode->prior=TempNode; 106 107 free(DeleteNode); 108 } 109 110 111 112 //删除头结点 113 void HeadDeleteData(Node *Head) 114 { 115 Head->next=Head->next->next; 116 Head->next->prior=Head; 117 } 118 119 120 //删除尾结点 121 void TailDeleteData(Node *Head) 122 { 123 Node *TempNode=Head; 124 while(Head->next->next!=NULL) 125 Head=Head->next; 126 127 Head->next=NULL; 128 Head->next->prior=NULL; 129 } 130 131 int main() 132 { 133 Node *Head; 134 int number; 135 printf("Please input the node number: "); 136 scanf("%d",&number); 137 Head=InitList(number); 138 printf("The initital list is: "); 139 ShowList(Head); 140 141 int flag; 142 printf(" "); 143 printf("**********************Your Choice******************** "); 144 printf("****************1-输出链表某个值的位置*************** "); 145 printf("****************2-输出链表某个位置的值*************** "); 146 printf("****************3-头入法插入元素********************* "); 147 printf("****************4-尾入法插入元素********************* "); 148 printf("****************5-删除头结点************************* "); 149 printf("****************6-删除尾结点************************* "); 150 printf("****************0-退出******************************* "); 151 printf(" "); 152 printf("Please input flag: "); 153 scanf("%d",&flag); 154 155 switch(flag) 156 { 157 case 1: 158 { 159 int data; 160 printf("Please input the data you want locate: "); 161 scanf("%d",&data); 162 int location; 163 location=ListLocation(Head,data,number); 164 printf("The data's location is: %d",location); 165 break; 166 } 167 case 2: 168 { 169 int location; 170 printf("Please input the location you want data: "); 171 scanf("%d",&location); 172 int data; 173 data=ListData(Head,location,number); 174 printf("The location's data is: %d ",data); 175 break; 176 } 177 case 3: 178 { 179 int data; 180 printf("Please input the data you want insert in head: "); 181 scanf("%d",&data); 182 HeadInsertData(Head,data); 183 ShowList(Head); 184 break; 185 } 186 case 4: 187 { 188 int data; 189 printf("Please input the data you want insert in tail: "); 190 scanf("%d",&data); 191 TailInsertData(Head,data); 192 ShowList(Head); 193 break; 194 } 195 case 5: 196 { 197 HeadDeleteData(Head); 198 ShowList(Head); 199 break; 200 } 201 case 6: 202 { 203 TailDeleteData(Head); 204 ShowList(Head); 205 break; 206 } 207 case 7: 208 { 209 printf("You choose to exit. "); 210 break; 211 } 212 } 213 return 0; 214 }
结果图: