1. 程式人生 > >線性表---單鏈表(建立、插入、刪除、排序、測長和列印輸出)

線性表---單鏈表(建立、插入、刪除、排序、測長和列印輸出)

實現了動態建立一個學生資訊的連結串列,並能夠進行建立、插入、刪除、排序、測長和列印輸出等操作。

/*-----------------------------------------------------------------
////////關鍵部分程式碼用序號標出////////////
-------------------------------------------------------------------*/
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
using
namespace std; /*-------------------------結構體定義部分------------------------------*/ struct Node //(1) { char name[10]; int score; struct Node *next; }; typedef struct Node ListNode; /*-----------------------------建立連結串列---------------------------------*/ /*在連結串列的末端插入新的節點,建立連結串列*/ ListNode *CreateList(int
n) { ListNode *head;//指向頭結點指標 //(2) ListNode *p,*pre; //(3) int i; head=(ListNode *)malloc(sizeof(ListNode));//為頭節點分配記憶體空間 //(4) head->next=NULL;//將頭結點的指標域清空 //(5) pre=head;//先將頭結點首地址賦給中間變數pre //(6) for(i=1;i<=n;i++)//通過for迴圈不斷加入新的結點 { printf("input name of the %d student: "
,i);//打印出第幾個人的名字 p=(ListNode *)malloc(sizeof(ListNode));//為要插入的節點分配 //(7) //記憶體空間p指向新插入結點的首地址 scanf("%s",&p->name);//輸入姓名 //(8) printf("input score of the %d student: ",i); scanf("%d",&p->score);//輸入分數 //(9) pre->next=p;//將p指向新結點插入連結串列也就是頭結點指標域指向 //(10) //下個結點 //第一個結點就是p指向的,因為頭結點內容為空 pre=p;//這個起著指向下一個結點的作用 //(11) } p->next=NULL;//最後將最後一個結點的指標域清空了 return head;//返回這個連結串列的首地址 } /*-------------------------輸出連結串列-----------------------------------*/ void PrintList(ListNode *h) { ListNode *p; //(1) p=h->next; //(2) while(p)//!!!! { printf("%s,%d",p->name,p->score); //(3) p=p->next; //(4) printf("\n"); } } /*----------------------插入連結串列結點--------------------------*/ /*----------------------------------------------------------------- 函式名稱:InsertList(ListNode *h,int i,char name[],int e,int n) 函式功能:插入連結串列結點 入口引數: h: 頭結點地址 i:插入到第幾個結點 name:插入結點的姓名 e:插入結點的分數 n:連結串列中結點的個數 除下頭結點外的個數 ------------------------------------------------------------------*/ void InsertList(ListNode *h,int i,char name[],int e,int n) { ListNode *q,*p;//先定義2個指向一個結點的指標 int j; if(i<1 || i>n+1) printf("Error! Please input again.\n"); else { j=0; p=h;//將指標p指向要連結串列的頭結點 while(j<i-1) //(1)遍歷找到插入節點的前一位置!!! { p=p->next;//一直查詢!!!!! j++; } q=(ListNode *)malloc(sizeof(ListNode));/*為要插入的 結點分配記憶體空間*/ //(2) //----賦值操作--------- strcpy(q->name,name); //將名字拷到要插入的節點內 //(3) q->score=e; //將要插入的節點中分數賦值 //(4) //調整指標域!!!! q->next = p->next; /*這個是將新插入的結點指標域指向 上一個結點指標域指向的結點地址即為p->next*/ //(5)關鍵!!!!! p->next=q;/*將要插入結點位置前面的結點指標域 指向現在插入的結點首地址*/ //(6)關鍵!!!!! } } /*----------------------返回連結串列長度--------------------------*/ int LengthList(ListNode *h) { int n=0; ListNode *p; p=h->next; while(p) { p=p->next; n++; } return n; } /*----------------------按連結串列資料由小到大排序------------------------*/ ListNode *BubbleSortList(ListNode *head) {//氣泡排序 if(head == NULL) return NULL; ListNode *p, *q; int temp_score;////交換score臨時變數 char temp_name[10];////交換name臨時變數 for(p = head->next; p != NULL; p = p->next)//!!!!! { for(q = p->next; q != NULL; q = q->next)//!!!!! { if(p->score > q->score) { temp_score = q->score;//交換score!!! q->score = p->score; p->score =temp_score; strcpy(temp_name,q->name);//交換name!!! strcpy(q->name,p->name); strcpy(p->name,temp_name); } } } return head; } /*-------------------------------------------------------------------- 函式名稱:DeleteList(ListNode *h, int i, int n) 函式功能:刪除連結串列結點 入口引數: h: 頭結點地址 i:要刪除的結點所在位置 n:連結串列中結點的個數除下頭結點外的個數 --------------------------------------------------------------------*/ void DeleteList(ListNode *h, int i, int n) { ListNode *p,*q;//首先定義2個指向結點型結構體的指標 int j; char name[10]; int score; if(i<1 || i>n)//如果位置超出了1和n的範圍的話則打印出錯誤資訊 printf("Error! Please input again.\n"); else//沒有超出除頭結點外的1到n 的範圍的話那麼執行刪除操作 { j=0; p=h;//將指標指向連結串列的頭結點首地址 while(j<i-1) //(1)遍歷找到刪除節點的前一位置p!!! { p=p->next; j++; } q=p->next; /*q指向要刪除的結點*/ //(2)關鍵!!!!! p->next=q->next;/*調整指標域*/ //(3)關鍵!!!!! strcpy(name,q->name); score=q->score; free(q);//釋放q指向的結點 printf("name=%s,score=%d\n",name,score); } } /*--------------------------主函式-------------------------------*/ int main() { ListNode *h;//h指向結構體NODE int i = 1, n, score; char name[10]; while (i) { /*輸入提示資訊*/ cout<<"--------------選擇選單-------------"<<endl; printf("1--建立新的連結串列\n"); printf("2--新增元素\n"); printf("3--刪除元素\n"); printf("4--連結串列測長\n"); printf("5--輸出當前表中的元素\n"); printf("6--連結串列氣泡排序\n"); printf("0--退出\n"); cout<<"--------------選擇選單-------------"<<endl; scanf("%d",&i); switch(i) { case 1: printf("input list length n : "); /*輸入建立連結串列結點的個數*/ scanf("%d",&n); h=CreateList(n);/*建立連結串列*/ //printf("list elements is : \n"); //PrintList(h); break; case 2: printf("input the position of insert element : "); scanf("%d",&i); printf("input name of the student : "); scanf("%s",name); printf("input score of the student : "); scanf("%d",&score); InsertList(h,i,name,score,n); //printf("list elements is:\n"); //PrintList(h); break; case 3: printf("input the position of delete element : "); scanf("%d",&i); DeleteList(h,i,n); //printf("list elements is : \n"); //PrintList(h); break; case 4: cout<<"list length is : "; cout<<LengthList(h)<<endl; break; case 5: printf("list element is : \n"); PrintList(h); break; case 6: cout<<"after bubble_sort : "<<endl; BubbleSortList(h); PrintList(h); break; case 0: break; default: printf("ERROR!Try again!\n"); } } return 0; }

結果:

--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
1
input list length n : 2
input name of the 1 student: s1
input score of the 1 student: 95
input name of the 2 student: s2
input score of the 2 student: 87
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
6
after bubble_sort :
s2,87
s1,95
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
2
input the position of insert element : 3
input name of the student : s3
input score of the student : 60
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
5
list element is :
s2,87
s1,95
s3,60
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
6
after bubble_sort :
s3,60
s2,87
s1,95
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
4
list length is : 3
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
3
input the position of delete element : 2
name=s2,score=87
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
5
list element is :
s3,60
s1,95
--------------選擇選單-------------
1--建立新的連結串列
2--新增元素
3--刪除元素
4--連結串列測長
5--輸出當前表中的元素
6--連結串列氣泡排序
0--退出
--------------選擇選單-------------
0

Process returned 0 (0x0)   execution time : 220.717 s
Press any key to continue.