單鏈表很全的例子,插入,刪除,,查詢,排序
阿新 • • 發佈:2019-01-08
單鏈表很全的例子,增加,刪除,排序,都有了
- #include <stdio.h>
- #include <stdlib.h>
- typedefstruct node
- {
- int nDate;
- struct node *pstnext;
- }Node;
- //連結串列輸出
- void output(Node *head)
- {
- Node *p = head->pstnext;
- while(NULL != p)
- {
- printf("%d ", p->nDate);
- p = p->pstnext;
- }
- printf("\r\n");
- }
- //連結串列建立
- Node* creat()
- {
- Node *head = NULL, *p = NULL, *s = NULL;
- int Date = 0, cycle = 1;
- head = (Node*)malloc(sizeof(Node));
- if(NULL == head)
- {
- printf("分配記憶體失敗\r\n");
- return NULL;
- }
- head->pstnext = NULL;
- p = head;
- while(cycle)
- {
- printf("請輸入資料且當輸入資料為0時結束輸入\r\n");
- scanf("%d", &Date);
- if(0 != Date)
- {
- s = (Node*)malloc(sizeof(Node));
- if(NULL == s)
- {
- printf("分配記憶體失敗\r\n");
- return NULL;
- }
- s->nDate = Date;
- p->pstnext = s;
- p = s;
- }
- else
- {
- cycle = 0;
- }
- }
- p->pstnext = NULL;
- return(head);
- }
- //單鏈表測長
- void length(Node *head)
- {
- Node *p = head->pstnext;
- int j=0;
- while(NULL != p)
- {
- p = p->pstnext;
- j++;
- }
- printf("%d\r\n", j);
- }
- //連結串列按值查詢
- void research_Date(Node *head, int date)
- {
- Node *p;
- int n=1;
- p = head->pstnext;
- while(NULL != p && date != p->nDate)
- {
- p = p->pstnext;
- ++n;
- }
- if(NULL == p)
- {
- printf("連結串列中沒有找到該值");
- }elseif(date == p->nDate)
- {
- printf("要查詢的值%d在連結串列中第%d個位置\r\n", date, n);
- }
- return;
- }
- //按序號查詢
- void research_Number(Node *head, int Num)
- {
- Node *p=head;
- int i = 0;
- while(NULL != p && i < Num)
- {
- p = p->pstnext;
- i++;
- }
- if(p == NULL)
- {
- printf("查詢位置不合法\r\n");
- }elseif(i == 0)
- {
- printf("查詢位置為頭結點\r\n");
- }elseif(i == Num)
- {
- printf("第%d個位置資料為%d\r\n", i, p->nDate);
- }
- }
- //在指定元素之前插入新結點
- void insert_1(Node *head, int i, int Newdate)
- {
- Node *pre = head, *New = NULL;
- int j = 0;
- while(NULL != pre && j < i-1)
- {
- pre = pre->pstnext;
- j++;
- }
- if(NULL == pre || j > i-1)
- {
- printf("插入位置不存在\r\n");
- }else
- {
- New = (Node*)malloc(sizeof(Node));
- if(NULL == New)
- {
- printf("分配記憶體失敗\r\n");
- return;
- }
- New->nDate = Newdate;
- New->pstnext = pre->pstnext;
- pre->pstnext = New;
- }
- }
- //在指定元素之後插入新結點
- void insert_2(Node *head, int i, int Newdate)
- {
- Node *pre = head, *New = NULL;
- int j = 0;
- while(NULL != pre->pstnext && j < i)
- {
- pre = pre->pstnext;
- j++;
- }
- if(j == i)
- {
- New = (Node*)malloc(sizeof(Node));
- if(NULL == New)
- {
- printf("分配記憶體失敗\r\n");
- return;
- }
- New->nDate = Newdate;
- New->pstnext = pre->pstnext;
- pre->pstnext = New;
- }else
- {
- printf("插入位置不存在\r\n");
- }
- }
- //刪除指定結點
- void Delete_1(Node *head, int i3)
- {
- Node *p = head, *pre = NULL;
- int j = 0;
- while(NULL != p && j < i3)
- {
- pre = p;
- p = p->pstnext;
- j++;
- }
- if(NULL == p)
- {
- printf("刪除位置不存在\r\n");
- }else
- {
- pre->pstnext = p->pstnext;
- free(p);
- }
- }
- //指定刪除單鏈表中某個資料,並統計刪除此資料的個數
- int Delete_2(Node *head, int Delete_date)
- {
- int count = 0;
- Node *p = head, *q;
- while(NULL != p->pstnext)
- {
- q = p->pstnext;
- if(q->nDate == Delete_date)
- {
- p->pstnext = q->pstnext;
- free(q);
- ++count;
- }
- else
- {
- p = q;
- }
- }
- return count;
- }
- //連結串列逆置
- void Reverse_list(Node *head)
- {
- Node *q, *s;
- if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
- {
- return;
- }
- q = head->pstnext->pstnext;
- head->pstnext->pstnext = NULL;
- while(NULL != q)
- {
- s = q->pstnext;
- q->pstnext = head->pstnext;
- head->pstnext = q;
- q = s;
- }
- }
- //單鏈表的連線
- void connect_list(Node *head, Node *head_New)
- {
- Node *p = head;
- while(NULL != p->pstnext)
- {
- p = p->pstnext;
- }
- p->pstnext = head_New->pstnext;
- }
- //單鏈表銷燬
- void destroy_list(Node* head)
- {
- while (NULL != head)
- {
- Node* temp = head;
- head = head->pstnext;
- free(temp);
- }
- }
- main()
- {
- int date, num; //待查詢資料
- int i3; //指定刪除元素的位置
- int i1, i2, Newdate_1, Newdate_2; //待插入的新資料
- int Delete_date, k; //待刪除的資料與其個數
- Node *Head = NULL; //定義頭結點
- Node *Head_New = NULL;
- //連結串列建立
- Head = creat();
- printf("輸出建立的單鏈表\r\n");
- output(Head);
- //單鏈表測長
- printf("單鏈表長度為\r\n");
- length(Head);
- //連結串列按值查詢
- printf("請輸入待查詢的資料\r\n");
- scanf("%d", &date);
- research_Date(Head, date);
- //連結串列按序號查詢
- printf("請輸入待查詢序號\r\n");
- scanf("%d", &num);
- research_Number(Head, num);
- //在指定第i1個元素之前插入新元素Newdate
- printf("在指定第i個元素之前插入新元素Newdate");
- printf("請輸入i與元素且以逗號間隔\r\n");
- scanf("%d,%d", &i1, &Newdate_1);
- insert_1(Head, i1, Newdate_1);
- printf("插入後新連結串列\r\n");
- output(Head);
- //在指定第i2個元素之後插入新元素Newdate
- printf("在指定第i個元素之後插入新元素Newdate");
- printf("請輸入i與元素且以逗號間隔\r\n");
- scanf("%d,%d", &i2, &Newdate_2);
- insert_2(Head, i2, Newdate_2);
- printf("插入後新連結串列\r\n");
- output(Head);
- //指定刪除i3元素
- printf("刪除元素的位置\r\n");
- scanf("%d", &i3);
- Delete_1(Head, i3);
- printf("刪除後新連結串列\r\n");
- output(Head);
- //指定刪除單鏈表中某個資料,並統計刪除此資料的個數
- printf("請輸入待刪除的元素\r\n");
- scanf("%d", &Delete_date);
- k = Delete_2(Head, Delete_date);
- printf("刪除後新連結串列\r\n");
- output(Head);
- printf("刪除指定元素在連結串列中的個數為:");
- printf("%d\r\n", k);
- //單鏈表逆置
- Reverse_list(Head);
- printf("逆置後輸出\r\n");
- output(Head);
- //單鏈表的連線
- printf("建立一個新連結串列\r\n");
- Head_New = creat();
- printf("輸出新連結串列");
- output(Head);
- printf("將新連結串列連線到原來連結串列的尾部並輸出\r\n");
- connect_list(Head, Head_New);
- output(Head);
- destroy_list(Head);
- }
- 下面是輸出結果:
VS2010下除錯結果