数据结构(一)线性表
- 一、线性表定义
- 二、顺序表
- 定义
- 动态数组
- 三、单链表
- 定义
- 不带头结点
- 带头结点
- 头结点与不带头结点的区别
- 头插法与尾插法
- 双链表
- 循环链表
- 循环单链表
- 循环双链表
- 静态链表
一、线性表定义
线性表是具有相同数据类型的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