完成雙鏈表的一些基本操作(連結串列的建立,雙向輸出,插入,刪除)
#include "stdafx.h" #include<stdlib.h> #include<stdio.h> #include<string.h>
typedef struct dlink_node { int info; struct dlink_node *llink, *rlink; }dnode,*p_head;
p_head create(void)//建立雙鏈表 { int i, length = 0, info = 0; p_head Ptail = NULL, P_new = NULL; p_head head = (p_head)malloc(sizeof(dnode)); if (NULL == head) { printf("記憶體分配失敗\n"); exit(0); } head->info = 0; head->llink = NULL; head->rlink = NULL; Ptail = head; printf("請輸入想建立連結串列的長度:"); scanf_s("%d", &length); for (i = 1; i < length + 1; i++) { P_new = (p_head)malloc(sizeof(dnode)); if (NULL == P_new) { printf("記憶體分配失敗"); exit(0); } printf("請輸入第%d個元素的值:", i); scanf_s("%d", &info); P_new->info = info; P_new->rlink = NULL; // 將Ptail->llink = Ptail刪除 ; Ptail->rlink = P_new; P_new->llink = Ptail;// 加入反向指標 Ptail = P_new; } return head; }
void display(p_head head)//正向輸出連結串列 { p_head p = head->rlink; printf("從左往右輸出連結串列:"); while (p!=NULL) { printf("%5d", p->info); p = p->rlink; } putchar('\n'); }
void display1(p_head tail)//逆向輸出連結串列 { p_head q = tail->rlink; while (q->rlink!= NULL) { q = q->rlink; } printf("從右往左輸出連結串列:"); while (q->llink != NULL) //將(q!=NULL)改為 { printf("%5d", q->info); q = q->llink; } printf("\n"); }
int empty(p_head head)//判斷連結串列是否為空 { p_head p = head->rlink; if (p == NULL) return 1; else return 0; }
int getlength(p_head head)//獲取連結串列長度 { int length = 0; p_head p = head->rlink; while (p != NULL) { length++; p = p->rlink; } return length; } int insert(p_head head, int pos, int info)//連結串列的插入實現 { p_head p = NULL, q = NULL; if (pos > 0 && pos < getlength(head) + 2) { q = (p_head)malloc(sizeof(dnode)); if (NULL == q) { printf("記憶體分配失敗"); exit(0); } while (1) { pos--; if (0 == pos) break; head = head->rlink; } p = head->rlink; q->info = info; q->rlink = p; if (NULL != p) p->llink = q; q->llink = head; head->rlink = q; return 1; } else
return 0; }
int dele(p_head head, int pos)//連結串列的刪除 { p_head p=NULL; if (pos > 0 && pos < getlength(head) + 1) { while (1) { pos--; if (0 == pos) break; head = head->rlink; } p = head->rlink->rlink; free(head->rlink); head->rlink = p; if (NULL != p) p->llink = head; return 1; } else return 0; }
int get_choose()//獲取選擇操作 { int choose_item = 0; printf("你選擇的操作是: "); scanf_s("%d", &choose_item); if (choose_item >= 1 && choose_item <= 4) { return choose_item; } else { puts("--輸入有誤,退出程式--"); return 7; } }
void print_item()//選擇的選單形成 { puts(" 1.檢視所有結點"); puts(" 2.插入結點"); puts(" 3.刪除結點"); puts(" 4.退出程式"); }
void free(p_head *pphead)//釋放連結串列,防止佔用記憶體 { p_head p = NULL; while (*pphead != NULL) { p = (*pphead)->rlink; free(*pphead); if (NULL != p) p->llink = NULL; *pphead = p; } } int main() { p_head head = NULL; head = create(); print_item(); int flag = 0; int choose = 0; int pos = 0; int num = 0; int length = 0; choose = get_choose(); while (choose >= 1 && choose <= 4) { switch (choose) { case 1: display(head); display1(head);// 引用反向輸出 break; case 2: printf("請輸入要插入節點的位置和元素值(兩個數拿空格隔開):"); scanf_s("%d %d", &pos, &num); flag = insert(head, pos, num); if (flag) printf("插入節點成功!"); else printf("插入失敗!"); break; case 3: flag = empty(head); if (flag) printf("雙向連結串列為空,不能進行刪除操作!"); else printf("請輸入要刪除節點的位置:"); scanf_s("%d", &pos); flag = dele(head, pos); if (flag) printf("刪除節點成功!"); else printf("刪除結點失敗!"); break; } if (choose == 4) { puts("--退出程式--"); break; } choose = get_choose(); } return 0; }