數據結構(一)線性表

數據結構(一)線性表

  • 一、線性表定義
  • 二、順序表
    • 定義
    • 動態數組
  • 三、單鏈表
    • 定義
    • 不帶頭結點
    • 帶頭結點
    • 頭結點與不帶頭結點的區別
    • 頭插法與尾插法
  • 雙鏈表
  • 循環鏈表
    • 循環單鏈表
    • 循環雙鏈表
  • 靜態鏈表

一、線性表定義

線性表是具有相同數據類型的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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384389.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384389.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384389.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux網絡編程(二)TCP通訊狀態

linux網絡編程&#xff08;二&#xff09;TCP通訊狀態TCP狀態轉換為什么需要等待2MSL&#xff1f;端口復用TCP狀態轉換 tcp協議連接開始會經過三次握手&#xff0c;客戶端和服務器開始都會處于CLOSED狀態 第一次握手&#xff1a;客戶端會先發送SYN請求給服務器&#xff0c;客戶…

gethostbyname() 函數說明

轉載&#xff1a;http://www.cnblogs.com/cxz2009/archive/2010/11/19/1881611.html gethostbyname()函數說明——用域名或主機名獲取IP地址 包含頭文件 #include <netdb.h> #include <sys/socket.h> 函數原型 struct hostent *gethostbyna…

linux網絡編程(三)select、poll和epoll

linux網絡編程&#xff08;三&#xff09;select、poll和epoll一、為什么會有多路I/O轉接服務器&#xff1f;二、select三、poll三、epoll一、為什么會有多路I/O轉接服務器&#xff1f; 為什么會有多路I/O轉接服務器呢&#xff1f;在學這個之前&#xff0c;我們同使用的是多線…

Linux socket編程(一) 對套接字操作的封裝

轉載:http://www.cnblogs.com/-Lei/archive/2012/09/04/2670942.html 以前寫的&#xff0c;現在回顧一下&#xff1a; 下面是對socket操作的封裝&#xff0c;因為在Linux下寫中文到了windows里面會亂碼&#xff0c;所以注釋用英文來寫&#xff0c;有空再查下解決方法吧 socket.…

數據結構(二)棧

棧一、棧順序棧線性棧(不帶頭結點)線性棧(帶頭結點)共享棧一、棧 棧是只允許在一端進行插入或刪除操作的線性表。 棧頂&#xff1a;線性表允許進行插入刪除的那一端 棧底&#xff1a;固定的&#xff0c;不允許進行插入和刪除的那一端 空棧&#xff1a;不含如何元素的空表 順序…

如何在linux上安裝sqlite數據庫

如何在linux上安裝sqlite數據庫一、下載二、解壓三、配置&#xff08;configure&#xff09;四、編譯和安裝五、執行sqlite3程序六、測試代碼一、下載 首先要先下載sqlite3源碼包 鏈接&#xff1a;https://pan.baidu.com/s/1_70342ZLlPjLlqGzpy5IHw 提取碼&#xff1a;84ne …

數據結構(三)隊列

數據結構&#xff08;三&#xff09;隊列隊列隊列&#xff08;順序存儲&#xff09;循環隊列&#xff08;順序存儲&#xff09;隊列&#xff08;鏈式存儲&#xff09;隊列 隊列是一種受限制的線性表&#xff0c;只允許表的一端插入&#xff0c;在表的另一端刪除 隊列&#xf…

Linux fcntl函數詳解

轉載&#xff1a;http://www.cnblogs.com/xuyh/p/3273082.html 功能描述&#xff1a;根據文件描述詞來操作文件的特性。 文件控制函數 fcntl -- file control 頭文件&#xff1a; #include <unistd.h> #include <fcntl.h> 函數原型&#xff1a; …

vs2019使用sqlite數據庫遠程連接linux

vs2019使用sqlite數據庫遠程連接linux一、sqlite3添加到目錄二、添加依賴庫三、測試一、sqlite3添加到目錄 將兩個sqlite3頭文件放入目錄中 二、添加依賴庫 打開項目屬性 添加完成 三、測試 #include <stdio.h> #include <sqlite3.h>int main(int argc, cha…

linux網絡編程(四)線程池

linux網絡編程&#xff08;四&#xff09;線程池為什么會有線程池&#xff1f;實現簡單的線程池為什么會有線程池&#xff1f; 大多數的服務器可能都有這樣一種情況&#xff0c;就是會在單位時間內接收到大量客戶端請求&#xff0c;我們可以采取接受到客戶端請求創建一個線程的…

AIGC:大語言模型LLM的幻覺問題

引言 在使用ChatGPT或者其他大模型時&#xff0c;我們經常會遇到模型答非所問、知識錯誤、甚至自相矛盾的問題。 雖然大語言模型&#xff08;LLMs&#xff09;在各種下游任務中展示出了卓越的能力&#xff0c;在多個領域有廣泛應用&#xff0c;但存在著幻覺的問題&#xff1a…

關于C++子類父類成員函數的覆蓋和隱藏

轉載&#xff1a;http://blog.csdn.net/worldmakewayfordream/article/details/46827161 函數的覆蓋 覆蓋發生的條件&#xff1a; &#xff08;1&#xff09; 基類必須是虛函數&#xff08;使用virtual 關鍵字來進行聲明&#xff09; &#xff08;2&#xff09;發生覆蓋的兩個函…

數據結構(四)串的順序存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char ch[MAXLEN]; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;char* …

數據結構(四)串的動態數組存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char* ch; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;T.ch (char*)m…

C++名字隱藏

轉載&#xff1a;http://www.weixueyuan.net/view/6361.html 如果派生類中新增一個成員變量&#xff0c;該成員變量與基類中的成員變量同名&#xff0c;則新增的成員變量就會遮蔽從基類中繼承過來的成員變量。同理&#xff0c;如果派生類中新增的成員函數與基類中的成員函數同…

c語言深入淺出(一)strcpy和memcpy的區別

c語言深入淺出&#xff08;一&#xff09;strcpy和memcpy的區別strcpy和memcpy都是c語言的庫函數 strcpy:只用于字符串的復制&#xff0c;當碰到‘\0’就停止了 memcpy:用于這個內存的拷貝&#xff0c;適用于結構體、字符數組、類等 char * strcpy(char * dest, const char * s…

C++ dynamic_cast操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6377.html 在C中&#xff0c;編譯期的類型轉換有可能會在運行時出現錯誤&#xff0c;特別是涉及到類對象的指針或引用操作時&#xff0c;更容易產生錯誤。Dynamic_cast操作符則可以在運行期對可能產生問題的類型轉換進行測試。…

數據結構(五)樹

數據結構&#xff08;五&#xff09;樹一、基本操作樹是n個節點的有限集&#xff0c;它是一種遞歸的數據結構 一、基本操作 #include <stdio.h> #include <stdlib.h> #include <iostream>#define Elemtype charusing namespace std; typedef struct BiTNod…

C++ typeid操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6378.html typeid操作符用于判斷表達式的類型&#xff0c;注意它和sizeof一樣是一個操作符而不是函數。如果需要使用typeid操作符&#xff0c;最好加上typeinfo頭文件。 給出以下定義 int a;double b;char * c;long d; 下表列…

數據結構(五)層次遍歷

數據結構&#xff08;五&#xff09;層次遍歷// linear_listqueue.cpp : This file contains the main function. Program execution begins and ends there. //#include <iostream> #include <stdlib.h> #include <stdio.h> #define ElemType BiTree using …