數據結構(一)線性表
- 一、線性表定義
- 二、順序表
- 定義
- 動態數組
- 三、單鏈表
- 定義
- 不帶頭結點
- 帶頭結點
- 頭結點與不帶頭結點的區別
- 頭插法與尾插法
- 雙鏈表
- 循環鏈表
- 循環單鏈表
- 循環雙鏈表
- 靜態鏈表
一、線性表定義
線性表是具有相同數據類型的n個數據元素的有限序列
特點:
- 表中的元數的個數有限
- 表中具有邏輯上的順序性,表中元素有其先后次序
- 表中元素都是數據元素,每個元素都是單個元素
- 表中元素的數據類型都相同,這意味著每個元素占有相同大小的存儲空間
二、順序表
定義
線性表的順序存儲稱為順序表。邏輯上相鄰的元素物理空間上也相鄰
特點:
- 隨機訪問(存取方式)
- 存儲密度高
- 擴展容量不方便
- 插入,刪除操作不方便(需要大量元素的前移和后移操作)
動態數組
// 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; //數據元素的類型,假設是int型的
typedef struct
{ElemType* data; //用動態數組的方式實現順序表int MaxSize; //最大長度int length; //順序表的長度
}SqList;
/*
函數作用:初始化線性表
函數名:void InitList(SqList& L)
參數:SqList& L
返回值:無
*/
void InitList(SqList& L)
{//malloc分配空間L.data = (ElemType*)malloc(sizeof(ElemType) * InitSize);//初始化L.length = 0;L.MaxSize = InitSize;
} /*
函數作用:增加動態表的長度
函數名:void IncreaseSize(SqList& L,int len)
參數: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];//復制數據}L.MaxSize = L.MaxSize + len; //順序表最大長度添加lenfree(p); //釋放原來的內存空間}
/*
函數作用:線性表的長度
函數名:int Length(SqList L)
參數:SqList L
返回值:線性表的長度
*/
int Length(SqList L)
{return L.length;}
/*
函數作用:按值查找
函數名:void LocateElem(SqList L, ElemType e)
參數: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;
}
/*
函數作用:獲取第i個元素
函數名:ElemType GetElem(SqList L, int i)
參數:SqList L, int i
返回值:獲取的值
*/
ElemType GetElem(SqList L, int i)
{return L.data[i - 1];
}
/*
函數作用:在第i個元素插入指定的元素
函數名:ElemType GetElem(SqList L, int i)
參數: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) //當前存儲空間大于最大空間{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;}
/*
函數作用:刪除第i個元素的值,e返回
函數名:ElemType GetElem(SqList L, int i)
參數: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;}/*
函數作用:打印所有元素值
函數名:void PrintList(SqList& L)
參數: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;}
}
/*
函數作用:順序表是否為空
函數名:bool Empty(SqList& L)
參數: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];//復制數據}L.MaxSize = L.MaxSize + len; //順序表最大長度添加lenfree(p); //釋放原來的內存空間}
動態數增加空間需要重新申請堆空間,并不是在原來的空間上操作的,對比鏈表來說,擴展不是很方便
三、單鏈表
定義
線性表的鏈式存儲為單鏈表
不帶頭結點
// 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; //數據元素的類型,假設是int型的typedef struct LNode
{ElemType data; //數據域struct LNode* next; //指針域指向下一個指針}LNode,*LinkList;/*
函數作用:初始化空的單鏈表
函數名:bool InitList(LinkList & L)
參數:LinkList & L
返回值:無
*///不帶頭節點
bool InitList(LinkList & L)
{//初始化一個空表L = NULL;return true;
}/*
函數作用:順序表是否為空
函數名:bool Empty(SqList& L)
參數:SqList& L
返回值:空為true , 非空為false
*/
bool Empty(LinkList L)
{if (L == NULL){return true;}else{return true;}
}/*
函數作用:插入到第i個節點
函數名:bool ListInsert(LinkList& L,int i,ElemType e)
參數:LinkList& L,int i,ElemType e
返回值:獲取的值
*/bool ListInsert(LinkList& L, int i, ElemType e)
{if (i < 1){return false;}//帶有頭節點的插入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;}
/*
函數作用:前插
函數名:bool InsertPriorNode(LNode * p , ElemType e)
參數:LNode * p , ElemType e
返回值:插入成功 true ;插入失敗 false;
*/
bool InsertPriorNode(LNode * p , ElemType e)
{if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創建新節點if (s == NULL) //分配失敗{return false;}//插入 s->next = p->next;p->next = s;s->data = p->data;p->data = e;}//刪除指定節點p
bool DeleteNode(LNode* p)
{//有bugif (p == NULL){return false;}LNode* q = p->next;if (q == NULL){}//O(1)時間復雜度p->data = p->next->data;p->next = q->next;free(q);return true;}/*
函數作用:求鏈表長度
函數名:int Length(LinkList L)
參數:LinkList L
返回值:鏈表長度
*/
int Length(LinkList L)
{int len = 0;LNode* p = L;while (p!= NULL){len++;p = p->next;}return len;}/*
函數作用:刪除指定的節點
函數名:bool ListDelete(LinkList& L, int i, ElemType& e)
參數: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; //刪除的節點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; //刪除的節點e = q->data;p->next = q->next;free(q);return true;}/*
函數作用:插入到指定節點之后
函數名:bool InsertNextNode(LNode* p,ElemType e)
參數:LNode* p,ElemType e
返回值:插入成功 true ;插入失敗 false;
*/
bool InsertNextNode(LNode* p,ElemType e)
{if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創建新節點if (s == NULL) //分配失敗{return false;}//插入 s->data = e;s->next = p->next;p->next = s;return true;}/*
函數作用:打印所有元素值
函數名:void PrintList(SqList& L)
參數:SqList& L
返回值:獲取所有元素值
*/
void PrintList(LinkList L)
{LNode* p = L;while (p != NULL){cout << p->data << endl;p = p->next;}}/*
函數作用:獲取第i個元素
函數名:ElemType GetElem(LinkList L, int i)
參數:LinkList L , int (LinkList強調單鏈表) (LNode* 強調節點)
返回值:獲取的值
*/
LNode* GetElem(LinkList L, int i)
{int j = 1;LNode* p = L;//對i的范圍進行判斷if (i == 0){return p;}if (i < 0){return NULL;}while (p->next != NULL && j<=i){p = p->next;j++;}return p;}/*
函數作用:尾插法
函數名:LinkList list_TailInsert(LinkList& L)
參數: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)); //創建新節點s->data = x;s->next = L;L = s;scanf_s("%d", &x, sizeof(int));}return L;
}int main()
{LinkList L;初始化頭節點//bool ret = InitList(L);//if (ret)//{// printf("分配成功\n");//}//else//{//}//int e;//插入節點//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
帶頭結點
#include <iostream>
#include <stdio.h>using namespace std;
typedef int ElemType; //數據元素的類型,假設是int型的typedef struct LNode
{ElemType data; //數據域struct LNode* next; //指針域指向下一個指針}LNode, * LinkList;/*
函數作用:初始化空的單鏈表
函數名:bool InitList(LinkList & L)
參數:LinkList & L
返回值:無
*///不帶頭節點
bool InitList(LinkList& L)
{//初始化頭節點L = (LNode*)malloc(sizeof(LNode)); //分配頭節點//初始化節點L->data = 0;L->next = NULL;if (L == NULL) //分配不足,分配失敗{return false;}else{return true;}}/*
函數作用:順序表是否為空
函數名:bool Empty(SqList& L)
參數:SqList& L
返回值:空為true , 非空為false
*/
bool Empty(LinkList L)
{if (L->next == NULL){return true;}else{return true;}
}/*
函數作用:獲取第i個元素
函數名:ElemType GetElem(LinkList L, int i)
參數:LinkList L , int (LinkList強調單鏈表) (LNode* 強調節點)
返回值:獲取的值
*/
LNode* GetElem(LinkList L, int i)
{int j = 0;LNode* p = L;//對i的范圍進行判斷if (i < 0){return NULL;}while (p != NULL && j < i){p = p->next;j++;}return p;}/*
函數作用:插入到第i個節點
函數名:bool ListInsert(LinkList& L,int i,ElemType e)
參數:LinkList& L,int i,ElemType e
返回值:獲取的值
*/bool ListInsert(LinkList& L,int i,ElemType e)
{if (i < 1){return false;}//帶有頭節點的插入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; //刪除的節點e = q->data;p->next = q->next;free(q);return true;}
/*
函數作用:插入到指定節點之后
函數名:bool InsertNextNode(LNode* p,ElemType e)
參數:LNode* p,ElemType e
返回值:插入成功 true ;插入失敗 false;
*/
bool InsertNextNode(LNode* p, ElemType e)
{if (p == NULL){return false;}LNode* s = (LNode*)malloc(sizeof(LNode)); //創建新節點if (s == NULL) //分配失敗{return false;}//插入 s->data = e;s->next = p->next;p->next = s;return true;}/*
函數作用:打印所有元素值
函數名:void PrintList(SqList& L)
參數: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;
}
/*
函數作用:求鏈表長度
函數名:int Length(LinkList L)
參數:LinkList L
返回值:鏈表長度
*/
int Length(LinkList L)
{int len = 0;LNode* p = L;while (p->next != NULL){len++;p = p->next;}return len;}
/*
函數作用:尾插法
函數名:LinkList list_TailInsert(LinkList& L)
參數:LinkList& L
返回值:鏈表
*/
LinkList list_TailInsert(LinkList& L) //正向建立單鏈表
{int x;L= (LNode*)malloc(sizeof(LNode));//創建頭節點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));//創建頭節點L->next = NULL;scanf_s("%d", &x, sizeof(int));while (x != 9999){s= (LNode*)malloc(sizeof(LNode)); //創建新節點s->data = x;s->next = L->next;L->next = s;scanf_s("%d", &x, sizeof(int));}return L;}int main()
{LinkList L;//初始化頭節點//bool ret=InitList(L);//if (ret)//{// printf("分配成功\n");//}//else//{//}//int e;//插入節點//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
頭結點與不帶頭結點的區別
/*
函數作用:插入到第i個節點
函數名:bool ListInsert(LinkList& L,int i,ElemType e)
參數:LinkList& L,int i,ElemType e
返回值:獲取的值
*/bool ListInsert(LinkList& L,int i,ElemType e)
{if (i < 1){return false;}//帶有頭節點的插入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;}
/*
函數作用:插入到第i個節點
函數名:bool ListInsert(LinkList& L,int i,ElemType e)
參數:LinkList& L,int i,ElemType e
返回值:獲取的值
*/bool ListInsert(LinkList& L, int i, ElemType e)
{if (i < 1){return false;}//不帶有頭節點的插入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;}
不帶頭結點插入操作比帶頭結點的插入操作多了i==1時的判斷,不帶頭結點因為初始化時指向的是NULL,所有最開始時插入要先創建頭結點,之后的插入就跟帶頭結點的操作一樣。所以不帶頭結點的更麻煩,一般情況下都是以帶頭結點編程的。
頭插法與尾插法
頭插法建立單鏈表的算法如下: /逆向建 立單鏈表
LinkList List HeadInsert(LinkList&L) {LNode *s; int x;L= (LinkList)malloc(sizeof (LNode));//初始為空鏈表L->next=NULL;//輸入結點的值scanf ("號d", &x) ;//輸入999表示結束while(x!=9999) {s=(LNode* )malloc(sizeof(LNode));//創建新結點s->data=x;s->next=L->next;L->next=s; //將新結點插入表中,L為頭指針scanf ("%d",&X) ;}
return L;}
//采用頭插法建立單鏈表時,讀入數據的順序與生成的鏈表中的元素的順序是相反的。
LinkList List_ TailInsert(LinkList &L)
{ //正向建立單鏈表int x;//設元素類型為整型L= (LinkList)malloc (sizeof (LNode)) ;LNode *s, *r=L;//r為表尾指針scanf ("&d", &X) ;//輸入結點的值while(x!=9999) { //輸入9999表示結束s= (LNode★ )malloc (sizeof (LNode) ) ;s->data=x;r->next=s ;r=s;//r指向新的表尾結點scanf ("號d", &X) ;}r->next=NULL;/ /尾結點指針置空return L;
}
雙鏈表
// linear_doublelist.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
using namespace std;
typedef int ElemType; //數據元素的類型,假設是int型的
typedef struct DNode
{ ElemType data; //數據域struct DNode* prior, * next; //前驅和后繼指針}DNode,*DLinklist;//初始化雙鏈表
bool InitDLinkList(DLinklist& L)
{L= (DNode*)malloc(sizeof(DNode));L->prior = NULL;L->next = NULL;return true;}/*
函數作用:順序表是否為空
函數名:bool Empty(SqList& L)
參數:SqList& L
返回值:空為true , 非空為false
*/
bool Empty(DLinklist L)
{if (L->next == NULL){return true;}else{return true;}
}//p節點之后插入s節點
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節點的后繼節點
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); //釋放頭節點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
循環鏈表
循環單鏈表
// linear_cyclelist.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
using namespace std;
typedef int ElemType; //數據元素的類型,假設是int型的
typedef struct LNode
{ElemType data;struct LNode* next;
}LNode, * Linklist;//初始化雙鏈表
bool InitDLinkList(Linklist& L)
{L = (LNode*)malloc(sizeof(LNode));if (L == NULL) //內存分配不足{return false;}L->data = 0;L->next = L;return true;}//判斷節點是否位循環單鏈表的表尾節點
bool isTail(Linklist L,LNode *p)
{if (p->next == L){return true;}else{return false;}
}//判斷循環雙鏈表是否是空的
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
循環雙鏈表
// linear_doublecyclelist.cpp : This file contains the 'main' function. Program execution begins and ends there.
//#include <iostream>
using namespace std;
typedef int ElemType; //數據元素的類型,假設是int型的
typedef struct LNode
{ElemType data;struct LNode* next,* prior;
}LNode, * Linklist;
//初始化雙鏈表
bool InitDLinkList(Linklist& L)
{L = (LNode*)malloc(sizeof(LNode));if (L == NULL) //內存分配不足{return false;}L->data = 0;L->next = L;L->prior = L;return true;}
//判斷節點是否位循環單鏈表的表尾節點
bool isTail(Linklist L, LNode* p)
{if (p->next == L){return true;}else{return false;}
}//判斷循環雙鏈表是否是空的
bool Empty(Linklist L)
{if (L->next == NULL){return true;}else{return false;}}
//p節點之后插入s節點
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節點的后繼節點
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
靜態鏈表
定義:分配一整個連續的空間,各個結點集中安置
// 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; //數據元素的類型,假設是int型的
#define MaxSize 10 //靜態鏈表的最大長度
typedef struct
{ElemType data; //數據域int cur; //下一個元素的數組下標}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("請輸入數據:");scanf_s("%d", &L[i].data);L[0].cur = L[i].cur;}L[i - 1].cur = 0;L[MaxSize - 1].cur = 1;//記錄鏈表第一個元素的下標}int Malloc_List(SLinkList space)//模擬創建節點,返回的是節點的下標
{int i;i = space[0].cur;//空的空間,相當于創建心的節點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); //創建節點for (i = 1; i < n; i++){k = L[k].cur;}L[j].cur = L[k].cur;L[k].cur = j;printf("請輸入插入的數據:");scanf_s("%d", &L[j].data);}void DeleteList(SLinkList& L)//刪除節點
{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