数据结构(一)线性表
生活随笔
收集整理的這篇文章主要介紹了
数据结构(一)线性表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
數(shù)據(jù)結(jié)構(gòu)(一)線性表
- 一、線性表定義
- 二、順序表
- 定義
- 動態(tài)數(shù)組
- 三、單鏈表
- 定義
- 不帶頭結(jié)點
- 帶頭結(jié)點
- 頭結(jié)點與不帶頭結(jié)點的區(qū)別
- 頭插法與尾插法
- 雙鏈表
- 循環(huán)鏈表
- 循環(huán)單鏈表
- 循環(huán)雙鏈表
- 靜態(tài)鏈表
一、線性表定義
線性表是具有相同數(shù)據(jù)類型的n個數(shù)據(jù)元素的有限序列
特點:
- 表中的元數(shù)的個數(shù)有限
- 表中具有邏輯上的順序性,表中元素有其先后次序
- 表中元素都是數(shù)據(jù)元素,每個元素都是單個元素
- 表中元素的數(shù)據(jù)類型都相同,這意味著每個元素占有相同大小的存儲空間
二、順序表
定義
線性表的順序存儲稱為順序表。邏輯上相鄰的元素物理空間上也相鄰
特點:
- 隨機(jī)訪問(存取方式)
- 存儲密度高
- 擴(kuò)展容量不方便
- 插入,刪除操作不方便(需要大量元素的前移和后移操作)
動態(tài)數(shù)組
// linear_list_dynast.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> using namespace std; #define InitSize 10 typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的 typedef struct {ElemType* data; //用動態(tài)數(shù)組的方式實現(xiàn)順序表int MaxSize; //最大長度int length; //順序表的長度 }SqList; /* 函數(shù)作用:初始化線性表 函數(shù)名:void InitList(SqList& L) 參數(shù):SqList& L 返回值:無 */ void InitList(SqList& L) {//malloc分配空間L.data = (ElemType*)malloc(sizeof(ElemType) * InitSize);//初始化L.length = 0;L.MaxSize = InitSize; } /* 函數(shù)作用:增加動態(tài)表的長度 函數(shù)名:void IncreaseSize(SqList& L,int len) 參數(shù):SqList& L,int len 返回值:無 */ void IncreaseSize(SqList& L,int len) {ElemType* p = L.data;L.data=(ElemType*)malloc(sizeof(ElemType) * (L.MaxSize+len));for (int i = 0; i < L.length; i++){L.data[i] = p[i];//復(fù)制數(shù)據(jù)}L.MaxSize = L.MaxSize + len; //順序表最大長度添加lenfree(p); //釋放原來的內(nèi)存空間} /* 函數(shù)作用:線性表的長度 函數(shù)名:int Length(SqList L) 參數(shù):SqList L 返回值:線性表的長度 */ int Length(SqList L) {return L.length;} /* 函數(shù)作用:按值查找 函數(shù)名:void LocateElem(SqList L, ElemType e) 參數(shù):SqList L, ElemType e 返回值:返回位序 */ int LocateElem(SqList L, ElemType e) {for (int i = 0; i < L.length; i++){if (L.data[i] == e){return i + 1;}}return false; } /* 函數(shù)作用:獲取第i個元素 函數(shù)名:ElemType GetElem(SqList L, int i) 參數(shù):SqList L, int i 返回值:獲取的值 */ ElemType GetElem(SqList L, int i) {return L.data[i - 1]; } /* 函數(shù)作用:在第i個元素插入指定的元素 函數(shù)名:ElemType GetElem(SqList L, int i) 參數(shù):SqList L, int i 返回值:獲取的值 */ bool ListInsert(SqList& L, int i, ElemType e) {if (i > L.length + 1 || i < 1)//判斷i的范圍是否有效{return false;}if (L.length >= L.MaxSize) //當(dāng)前存儲空間大于最大空間{return false;}for (int j = L.length; j > i; j--) //將第i個元素級之后的元素后移{L.data[j] = L.data[j - 1];}L.data[i - 1] = e;L.length++;return true;} /* 函數(shù)作用:刪除第i個元素的值,e返回 函數(shù)名:ElemType GetElem(SqList L, int i) 參數(shù):SqList L, int i 返回值:獲取的值 */bool ListDelete(SqList& L, int i, ElemType& e) {if (i > L.length || i < 1)//判斷i的范圍是否有效{return false;}e = L.data[i - 1];for (int j = i; j <= L.length; j++){if (j == L.length){L.data[j - 1] = 0;L.length--;return true;}L.data[j - 1] = L.data[j];}L.length--;return true;}/* 函數(shù)作用:打印所有元素值 函數(shù)名:void PrintList(SqList& L) 參數(shù):SqList& L 返回值:獲取所有元素值 */ void PrintList(SqList& L) {for (int i = 0; i < L.length; i++){//ListInsert(L, i + 1, i + 3);cout << L.data[i] << endl;} } /* 函數(shù)作用:順序表是否為空 函數(shù)名:bool Empty(SqList& L) 參數(shù):SqList& L 返回值:空為true , 非空為false */ bool Empty(SqList& L) {if (L.data == NULL){return true;}else{return false;} }void DestroyList(SqList& L) {free(L.data); }int main() {SqList L; //聲明一個線性表InitList(L); //初始化一個線性表int e;for (int i = 0; i < L.MaxSize; i++){ListInsert(L, i + 1, i + 3);cout << L.data[i] << endl;}ListDelete(L, 5, e);DestroyList(L);for (int i = 0; i < L.MaxSize; i++){//ListInsert(L, i + 1, i + 3);cout << L.data[i] << endl;} } void IncreaseSize(SqList& L,int len) {ElemType* p = L.data;L.data=(ElemType*)malloc(sizeof(ElemType) * (L.MaxSize+len));for (int i = 0; i < L.length; i++){L.data[i] = p[i];//復(fù)制數(shù)據(jù)}L.MaxSize = L.MaxSize + len; //順序表最大長度添加lenfree(p); //釋放原來的內(nèi)存空間}動態(tài)數(shù)增加空間需要重新申請堆空間,并不是在原來的空間上操作的,對比鏈表來說,擴(kuò)展不是很方便
三、單鏈表
定義
線性表的鏈?zhǔn)酱鎯閱捂湵?/p>
不帶頭結(jié)點
// linear_singlelist.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> #include<stdlib.h> using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的typedef struct LNode {ElemType data; //數(shù)據(jù)域struct LNode* next; //指針域指向下一個指針}LNode,*LinkList;/* 函數(shù)作用:初始化空的單鏈表 函數(shù)名:bool InitList(LinkList & L) 參數(shù):LinkList & L 返回值:無 *///不帶頭節(jié)點 bool InitList(LinkList & L) {//初始化一個空表L = NULL;return true; }/* 函數(shù)作用:順序表是否為空 函數(shù)名:bool Empty(SqList& L) 參數(shù):SqList& L 返回值:空為true , 非空為false */ bool Empty(LinkList L) {if (L == NULL){return true;}else{return true;} }/* 函數(shù)作用:插入到第i個節(jié)點 函數(shù)名:bool ListInsert(LinkList& L,int i,ElemType e) 參數(shù):LinkList& L,int i,ElemType e 返回值:獲取的值 */bool ListInsert(LinkList& L, int i, ElemType e) {if (i < 1){return false;}//帶有頭節(jié)點的插入LNode* p = L;int j = 1;if (i == 1){LNode* s = (LNode*)malloc(sizeof(LNode));s->data = e;s->next = L;L = s;return true;}while (p != NULL && j < i - 1){p = p->next;j++;}if (p == NULL) //i值不合法{return false;}LNode* s = (LNode*)malloc(sizeof(LNode));s->next = p->next;s->data = e;p->next = s;return true;} /* 函數(shù)作用:前插 函數(shù)名:bool InsertPriorNode(LNode * p , ElemType e) 參數(shù):LNode * p , ElemType e 返回值:插入成功 true ;插入失敗 false; */ bool InsertPriorNode(LNode * p , ElemType e) {if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創(chuàng)建新節(jié)點if (s == NULL) //分配失敗{return false;}//插入 s->next = p->next;p->next = s;s->data = p->data;p->data = e;}//刪除指定節(jié)點p bool DeleteNode(LNode* p) {//有bugif (p == NULL){return false;}LNode* q = p->next;if (q == NULL){}//O(1)時間復(fù)雜度p->data = p->next->data;p->next = q->next;free(q);return true;}/* 函數(shù)作用:求鏈表長度 函數(shù)名:int Length(LinkList L) 參數(shù):LinkList L 返回值:鏈表長度 */ int Length(LinkList L) {int len = 0;LNode* p = L;while (p!= NULL){len++;p = p->next;}return len;}/* 函數(shù)作用:刪除指定的節(jié)點 函數(shù)名:bool ListDelete(LinkList& L, int i, ElemType& e) 參數(shù):LinkList& L, int i, ElemType& e 返回值:插入成功 true ;插入失敗 false; */bool ListDelete(LinkList& L, int i, ElemType& e) {int j = 1;LNode* p = L;if (i < 0){return false;}if (i == 1){LNode* q = L; //刪除的節(jié)點L = L->next;free(q);return true;}while (p != NULL && j < i){p = p->next;j++;}if (p == NULL) //i值不合法{return false;}if (p->next == NULL){return false;}LNode* q = p->next; //刪除的節(jié)點e = q->data;p->next = q->next;free(q);return true;}/* 函數(shù)作用:插入到指定節(jié)點之后 函數(shù)名:bool InsertNextNode(LNode* p,ElemType e) 參數(shù):LNode* p,ElemType e 返回值:插入成功 true ;插入失敗 false; */ bool InsertNextNode(LNode* p,ElemType e) {if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創(chuàng)建新節(jié)點if (s == NULL) //分配失敗{return false;}//插入 s->data = e;s->next = p->next;p->next = s;return true;}/* 函數(shù)作用:打印所有元素值 函數(shù)名:void PrintList(SqList& L) 參數(shù):SqList& L 返回值:獲取所有元素值 */ void PrintList(LinkList L) {LNode* p = L;while (p != NULL){cout << p->data << endl;p = p->next;}}/* 函數(shù)作用:獲取第i個元素 函數(shù)名:ElemType GetElem(LinkList L, int i) 參數(shù):LinkList L , int (LinkList強調(diào)單鏈表) (LNode* 強調(diào)節(jié)點) 返回值:獲取的值 */ LNode* GetElem(LinkList L, int i) {int j = 1;LNode* p = L;//對i的范圍進(jìn)行判斷if (i == 0){return p;}if (i < 0){return NULL;}while (p->next != NULL && j<=i){p = p->next;j++;}return p;}/* 函數(shù)作用:尾插法 函數(shù)名:LinkList list_TailInsert(LinkList& L) 參數(shù):LinkList& L 返回值:鏈表 */ LinkList list_TailInsert(LinkList& L) //正向建立單鏈表 {int x;L = NULL;LNode* s = NULL, * r = L;scanf_s("%d", &x, sizeof(int));while (x != 9999){if (L == NULL){s = (LNode*)malloc(sizeof(LNode));s->data = x;s->next = NULL;L = s;r = s;scanf_s("%d", &x, sizeof(int));continue;}s = (LNode*)malloc(sizeof(LNode));s->data = x;s->next = NULL;r->next = s;r = s;scanf_s("%d", &x, sizeof(int));}return L;}LinkList List_HeadInsert(LinkList& L) //逆向建立單鏈表 {LNode* s;int x;L = NULL;scanf_s("%d", &x, sizeof(int));while (x != 9999){if (L == NULL){s = (LNode*)malloc(sizeof(LNode));s->data = x;s->next = NULL;L = s;scanf_s("%d", &x, sizeof(int));continue;}s = (LNode*)malloc(sizeof(LNode)); //創(chuàng)建新節(jié)點s->data = x;s->next = L;L = s;scanf_s("%d", &x, sizeof(int));}return L; }int main() {LinkList L;初始化頭節(jié)點//bool ret = InitList(L);//if (ret)//{// printf("分配成功\n");//}//else//{//}//int e;//插入節(jié)點//ListInsert(L, 1, 3);//ListInsert(L, 1, 2);//InsertNextNode(L, 5);//ListDelete(L,1,e);List_HeadInsert(L);PrintList(L);printf("表長=%d",Length(L)); }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file帶頭結(jié)點
#include <iostream> #include <stdio.h>using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的typedef struct LNode {ElemType data; //數(shù)據(jù)域struct LNode* next; //指針域指向下一個指針}LNode, * LinkList;/* 函數(shù)作用:初始化空的單鏈表 函數(shù)名:bool InitList(LinkList & L) 參數(shù):LinkList & L 返回值:無 *///不帶頭節(jié)點 bool InitList(LinkList& L) {//初始化頭節(jié)點L = (LNode*)malloc(sizeof(LNode)); //分配頭節(jié)點//初始化節(jié)點L->data = 0;L->next = NULL;if (L == NULL) //分配不足,分配失敗{return false;}else{return true;}}/* 函數(shù)作用:順序表是否為空 函數(shù)名:bool Empty(SqList& L) 參數(shù):SqList& L 返回值:空為true , 非空為false */ bool Empty(LinkList L) {if (L->next == NULL){return true;}else{return true;} }/* 函數(shù)作用:獲取第i個元素 函數(shù)名:ElemType GetElem(LinkList L, int i) 參數(shù):LinkList L , int (LinkList強調(diào)單鏈表) (LNode* 強調(diào)節(jié)點) 返回值:獲取的值 */ LNode* GetElem(LinkList L, int i) {int j = 0;LNode* p = L;//對i的范圍進(jìn)行判斷if (i < 0){return NULL;}while (p != NULL && j < i){p = p->next;j++;}return p;}/* 函數(shù)作用:插入到第i個節(jié)點 函數(shù)名:bool ListInsert(LinkList& L,int i,ElemType e) 參數(shù):LinkList& L,int i,ElemType e 返回值:獲取的值 */bool ListInsert(LinkList& L,int i,ElemType e) {if (i < 1){return false;}//帶有頭節(jié)點的插入LNode* p = L;GetElem(L, i - 1);if (p == NULL) //i值不合法{return false;}LNode *s= (LNode*)malloc(sizeof(LNode));s->next = p->next;s->data = e;p->next = s;return true;}bool ListDelete(LinkList& L, int i, ElemType& e) {if (i < 0){return false;}int j = 0;LNode* p = L;while (p !=NULL && j<i-1){p = p->next;j++;}if (p == NULL) //i值不合法{return false;}if (p->next == NULL){return false;}LNode* q = p->next; //刪除的節(jié)點e = q->data;p->next = q->next;free(q);return true;} /* 函數(shù)作用:插入到指定節(jié)點之后 函數(shù)名:bool InsertNextNode(LNode* p,ElemType e) 參數(shù):LNode* p,ElemType e 返回值:插入成功 true ;插入失敗 false; */ bool InsertNextNode(LNode* p, ElemType e) {if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創(chuàng)建新節(jié)點if (s == NULL) //分配失敗{return false;}//插入 s->data = e;s->next = p->next;p->next = s;return true;}/* 函數(shù)作用:打印所有元素值 函數(shù)名:void PrintList(SqList& L) 參數(shù):SqList& L 返回值:獲取所有元素值 */ void PrintList(LinkList L) {LNode* p = L->next;while (p != NULL){cout << p->data << endl;p = p->next;}} LNode* LocateElem(LinkList &L,ElemType e) {LNode* p = L->next;while (p != NULL && p->data != e){p = p->next;}return p; } /* 函數(shù)作用:求鏈表長度 函數(shù)名:int Length(LinkList L) 參數(shù):LinkList L 返回值:鏈表長度 */ int Length(LinkList L) {int len = 0;LNode* p = L;while (p->next != NULL){len++;p = p->next;}return len;} /* 函數(shù)作用:尾插法 函數(shù)名:LinkList list_TailInsert(LinkList& L) 參數(shù):LinkList& L 返回值:鏈表 */ LinkList list_TailInsert(LinkList& L) //正向建立單鏈表 {int x;L= (LNode*)malloc(sizeof(LNode));//創(chuàng)建頭節(jié)點L->data = 0;L->next = NULL;LNode* s, * r = L;scanf_s("%d",&x,sizeof(int));while (x != 9999){s=(LNode*)malloc(sizeof(LNode));s->data = x;s->next = NULL;r->next = s;r = s;scanf_s("%d", &x, sizeof(int));}return L;}LinkList List_HeadInsert(LinkList & L) //逆向建立單鏈表 {LNode* s;int x;L = (LNode*)malloc(sizeof(LNode));//創(chuàng)建頭節(jié)點L->next = NULL;scanf_s("%d", &x, sizeof(int));while (x != 9999){s= (LNode*)malloc(sizeof(LNode)); //創(chuàng)建新節(jié)點s->data = x;s->next = L->next;L->next = s;scanf_s("%d", &x, sizeof(int));}return L;}int main() {LinkList L;//初始化頭節(jié)點//bool ret=InitList(L);//if (ret)//{// printf("分配成功\n");//}//else//{//}//int e;//插入節(jié)點//ListInsert(L, 1, 3); //ListInsert(L, 1, 4);//ListInsert(L, 1, 5);//ListDelete(L, 1, e);List_HeadInsert(L);PrintList(L);}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file頭結(jié)點與不帶頭結(jié)點的區(qū)別
/* 函數(shù)作用:插入到第i個節(jié)點 函數(shù)名:bool ListInsert(LinkList& L,int i,ElemType e) 參數(shù):LinkList& L,int i,ElemType e 返回值:獲取的值 */bool ListInsert(LinkList& L,int i,ElemType e) {if (i < 1){return false;}//帶有頭節(jié)點的插入LNode* p = L;GetElem(L, i - 1);if (p == NULL) //i值不合法{return false;}LNode *s= (LNode*)malloc(sizeof(LNode));s->next = p->next;s->data = e;p->next = s;return true;} /* 函數(shù)作用:插入到第i個節(jié)點 函數(shù)名:bool ListInsert(LinkList& L,int i,ElemType e) 參數(shù):LinkList& L,int i,ElemType e 返回值:獲取的值 */bool ListInsert(LinkList& L, int i, ElemType e) {if (i < 1){return false;}//不帶有頭節(jié)點的插入LNode* p = L;int j = 1;if (i == 1){LNode* s = (LNode*)malloc(sizeof(LNode));s->data = e;s->next = L;L = s;return true;}while (p != NULL && j < i - 1){p = p->next;j++;}if (p == NULL) //i值不合法{return false;}LNode* s = (LNode*)malloc(sizeof(LNode));s->next = p->next;s->data = e;p->next = s;return true;}不帶頭結(jié)點插入操作比帶頭結(jié)點的插入操作多了i==1時的判斷,不帶頭結(jié)點因為初始化時指向的是NULL,所有最開始時插入要先創(chuàng)建頭結(jié)點,之后的插入就跟帶頭結(jié)點的操作一樣。所以不帶頭結(jié)點的更麻煩,一般情況下都是以帶頭結(jié)點編程的。
頭插法與尾插法
雙鏈表
// linear_doublelist.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的 typedef struct DNode { ElemType data; //數(shù)據(jù)域struct DNode* prior, * next; //前驅(qū)和后繼指針}DNode,*DLinklist;//初始化雙鏈表 bool InitDLinkList(DLinklist& L) {L= (DNode*)malloc(sizeof(DNode));L->prior = NULL;L->next = NULL;return true;}/* 函數(shù)作用:順序表是否為空 函數(shù)名:bool Empty(SqList& L) 參數(shù):SqList& L 返回值:空為true , 非空為false */ bool Empty(DLinklist L) {if (L->next == NULL){return true;}else{return true;} }//p節(jié)點之后插入s節(jié)點 bool InsertNextDNode(DNode * p,DNode *s) {if (p == NULL || s == NULL){return false;}s->next = p->next;if (p->next != NULL){p->next->prior = s;}s->prior = p;p->next = s;}//刪除p節(jié)點的后繼節(jié)點 bool DeleteNextDNode(DNode* p) {if (p == NULL){return false;}DNode* q = p->next;if (q == NULL) //沒有后繼{return false;}p->next = q->next;if (q->next != NULL){q->next->prior = p;}free(q);return true; } //銷毀鏈表 void DestoryList(DLinklist& L) {while (L->next != NULL){DeleteNextDNode(L);}free(L); //釋放頭節(jié)點L = NULL; //指向空 }int main() {std::cout << "Hello World!\n"; }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file循環(huán)鏈表
循環(huán)單鏈表
// linear_cyclelist.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的 typedef struct LNode {ElemType data;struct LNode* next; }LNode, * Linklist;//初始化雙鏈表 bool InitDLinkList(Linklist& L) {L = (LNode*)malloc(sizeof(LNode));if (L == NULL) //內(nèi)存分配不足{return false;}L->data = 0;L->next = L;return true;}//判斷節(jié)點是否位循環(huán)單鏈表的表尾節(jié)點 bool isTail(Linklist L,LNode *p) {if (p->next == L){return true;}else{return false;} }//判斷循環(huán)雙鏈表是否是空的 bool Empty(Linklist L) {if (L->next == NULL){return true;}else{return false;}} int main() {std::cout << "你好!\n"; }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file循環(huán)雙鏈表
// linear_doublecyclelist.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的 typedef struct LNode {ElemType data;struct LNode* next,* prior; }LNode, * Linklist; //初始化雙鏈表 bool InitDLinkList(Linklist& L) {L = (LNode*)malloc(sizeof(LNode));if (L == NULL) //內(nèi)存分配不足{return false;}L->data = 0;L->next = L;L->prior = L;return true;} //判斷節(jié)點是否位循環(huán)單鏈表的表尾節(jié)點 bool isTail(Linklist L, LNode* p) {if (p->next == L){return true;}else{return false;} }//判斷循環(huán)雙鏈表是否是空的 bool Empty(Linklist L) {if (L->next == NULL){return true;}else{return false;}} //p節(jié)點之后插入s節(jié)點 bool InsertNextDNode(LNode* p, LNode* s) {if (p == NULL || s == NULL){return false;}s->next = p->next;p->next->prior = s;s->prior = p;p->next = s;} //刪除p節(jié)點的后繼節(jié)點 bool DeleteNextDNode(LNode* p) {if (p == NULL){return false;}LNode* q = p->next;p->next = q->next;q->next->prior = p;free(q);return true; } int main() {std::cout << "Hello World!\n"; }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file靜態(tài)鏈表
定義:分配一整個連續(xù)的空間,各個結(jié)點集中安置
// linear_staticlist.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h> using namespace std; typedef int ElemType; //數(shù)據(jù)元素的類型,假設(shè)是int型的 #define MaxSize 10 //靜態(tài)鏈表的最大長度 typedef struct {ElemType data; //數(shù)據(jù)域int cur; //下一個元素的數(shù)組下標(biāo)}Component,SLinkList[MaxSize];//初始化雙鏈表 bool InitSLinkList(SLinkList& L) {for (int i = 1; i < MaxSize-1; i++){L[i].cur = i + 1;}L[MaxSize - 1].cur = 0;return true;}void CreateList(SLinkList& L) {int n, i;printf("請輸入鏈表的長度:");scanf_s("%d", &n);for (i=1;i<=n;i++){printf("請輸入數(shù)據(jù):");scanf_s("%d", &L[i].data);L[0].cur = L[i].cur;}L[i - 1].cur = 0;L[MaxSize - 1].cur = 1;//記錄鏈表第一個元素的下標(biāo)}int Malloc_List(SLinkList space)//模擬創(chuàng)建節(jié)點,返回的是節(jié)點的下標(biāo) {int i;i = space[0].cur;//空的空間,相當(dāng)于創(chuàng)建心的節(jié)點space[0].cur = space[i].cur;//去下一個空的空間return i; }void ListInsert(SLinkList& L) {int k, n, i, j;printf("請輸入插入的位置:");scanf_s("%d", &n);if (n<1 || n>L[0].cur){printf("插入位置異常,插入失敗!\n");return;}k = MaxSize - 1;j = Malloc_List(L); //創(chuàng)建節(jié)點for (i = 1; i < n; i++){k = L[k].cur;}L[j].cur = L[k].cur;L[k].cur = j;printf("請輸入插入的數(shù)據(jù):");scanf_s("%d", &L[j].data);}void DeleteList(SLinkList& L)//刪除節(jié)點 {int n, i, k, j;printf("請輸入刪除的位置:");scanf_s("%d", &n);k = MaxSize - 1;for (i = 1; i < n; i++){k = L[k].cur;}j = L[k].cur;L[k].cur = L[j].cur;}int main() {SLinkList a;InitSLinkList(a);}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file總結(jié)
以上是生活随笔為你收集整理的数据结构(一)线性表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈利波特魔法觉醒毒囊兽怎么打
- 下一篇: 数据结构(二)栈