顺序表相关:
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
#define OK 0;
#define FAIL -1;
#define TRUE 1;
#define ERROR -1;
typedef struct//顺序表节点
{
ElemType* elem;
int listsize;
int length;
}SqList;
Status InitSqList(SqList& L)//顺序表初始化
{
L.elem = new ElemType (MAXSIZE);
if (!L.elem)
{
return ERROR;
}
L.length = 0;
L.listsize = MAXSIZE;
return OK;
}
bool SqListInsert(SqList& L, int i, ElemType e)//按位插入
{
if (i<1 || i>L.length + 1)
{
return false;
}
for (int j = L.length; j >= i; j--)
{
L.elem[j] = L.elem[j - 1];
}
L.elem[i - 1] = e;
L.length++;
return true;
}
bool SqListDelete1(SqList& L, int i)//按位删除
{
if (i<1 || i>L.length)
{
return false;
}
for (int j = i - 1; j < L.length - 1; j++)
{
L.elem[j] = L.elem[j + 1];
}
L.length--;
return true;
}
bool SqListDelete2(SqList& L, int i)//按值删除
{
for (int j = 0; j < L.length; j++)
{
if (L.elem[j] == i)
{
if ((j + 1) == L.length)
{
L.elem[j] = NULL;
L.length--;
return true;
}
for (int k = j; k < L.length - 1; k++)
{
L.elem[k] = L.elem[k + 1];
L.length--;
}
L.elem[L.length] = NULL;
return true;
}
}
}
bool SqListLocate1(SqList L, int i)//按位查找
{
if (i > L.length)
{
cout << "No." << i << " number is NULL" << endl;
return false;
}
cout << "the No." << i << "number is " << L.elem[i-1] << endl;
return true;
}
void SqListLocate2(SqList L, int i)//按值查找
{
for (int j = 0; j < L.length; j++)
{
if (L.elem[j] == i)
{
cout << "the number " << i << " is No." << j + 1 << endl;
}
}
}
int SqListLength(SqList L)//求长度
{
return L.length;
}
void SqListTraversal(SqList L)//遍历
{
cout << "SqList Traversal:";
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i] << " ";
}
cout << endl;
}
int main()
{
SqList L;
InitSqList(L);
SqListInsert(L, 1, 10);
SqListInsert(L, 2, 20);
SqListInsert(L, 3, 30);
SqListInsert(L, 4, 40);
SqListInsert(L, 5, 50);
SqListTraversal(L);
SqListDelete1(L,3);
SqListTraversal(L);
SqListDelete2(L,50);
SqListTraversal(L);
SqListDelete2(L, 20);
SqListTraversal(L);
SqListLocate1(L, 3);
SqListLocate2(L, 20);
cout << "L length =" << SqListLength(L) << endl;
system("pause");
return 0;
}
头插法建立单链表(带头结点):
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LNode//定义节点类型
{
ElemType data;
struct LNode* next;
}LNode,* LinkList;
LinkList List_HeadInsert(LinkList& L)//头插法建立单链表(带头结点)
{
LNode* s; ElemType x;
L = new LNode();
L->next = NULL;
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
while (x != 9999)
{
s = new LNode();
s->data = x;
s->next = L->next;
L->next = s;
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
}
return L;
}
bool List_Traversal(LinkList L)//遍历单链表
{
if (L->next ==NULL)
{
return false;
}
while (L->next != NULL)
{
cout << L->next->data << " ";
L = L->next;
}
return true;
}
int main()
{
LinkList L;
List_HeadInsert(L);
List_Traversal(L);
return 0;
}
头插法建立单链表(不带头结点):
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList List_HeadInsert(LinkList& L)//头插法建立单链表,不带头结点
{
LNode* s; ElemType x;
L = new LNode();
cout << "Please input value number,ending when number is 9999" << endl;
cin >> x;
L->data = x;
L->next = NULL;
while (x != 9999)
{
cout << "Please input value number,ending when number is 9999" << endl;
cin >> x;
s = new LNode();
s->data = x;
s->next = L;
L = s;
}
return L;
}
bool List_Traversal(LinkList L)//遍历单链表
{
if (L == NULL)
{
return false;
}
while (L != NULL)
{
cout << L->data << " ";
L = L->next;
}
return true;
}
int main()
{
LinkList L;
List_HeadInsert(L);
List_Traversal(L);
return 0;
}
尾插法建立单链表:
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LNode//节点类型
{
ElemType data;
struct LNode* next;
}LNode,* LinkList;
LinkList List_TailInsert1(LinkList& L)//尾插法建立单链表(带头节点)
{
LNode* s; ElemType x;
L = new LNode();//创建头结点
L->next = NULL;
LNode* r = L;//表尾指针指向头结点
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
while (x != 9999)
{
s = new LNode();
s->data = x;
r->next = s;
r = s;
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
}
r->next = NULL;
return L;
}
LinkList List_TailInsert2(LinkList& L)//尾插法建立单链表(不带头节点)
{
LNode* s; ElemType x;
L = new LNode();
LNode* r = L;//表尾指针指向头结点
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
if (x ==9999)
{
return L;
}
L->data = x;
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
while (x != 9999)
{
s = new LNode();
s->data = x;
r->next = s;
r = s;
cout << "Please input valid number,Ending when the number is 9999" << endl;
cin >> x;
}
r->next = NULL;
return L;
}
bool List_Traversal(LinkList L)//带头结点的遍历
{
if (L->next ==NULL)
return false;
while (L->next != NULL)
{
cout << L->next->data << " ";
L = L->next;
}
return true;
}
bool List_Traversal2(LinkList L)//不带头结点的遍历
{
if (L == NULL)
return false;
while (L != NULL)
{
cout << L->data << " ";
L = L->next;
}
return true;
}
int main()
{
LinkList L;
List_TailInsert1(L);
List_Traversal(L);
List_TailInsert2(L);
List_Traversal2(L);
system("pause");
return 0;
}
双链表的常用操作:
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct DLNode
{
struct DLNode* prev, * next;
ElemType data;
}DLNode, * DLinklist;
DLinklist DLinklist_HeadInsert(DLinklist& L, int n)//头插法建立双链表
{
L = new DLNode();
L->prev = NULL;
L->next = NULL;
int x;
for (int i = 0; i < n; i++)
{
cout << "please enter No." << i + 1 << " number that needs to be inserted" << endl;
cin >> x;
DLNode* s, * p = L;
s = new DLNode();
s->data = x;
if (p->next == NULL)
{
s->next = NULL;
p->next = s;
s->prev = p;
}
else
{
s->next = p->next;
p->next->prev = s;
s->prev = p;
p->next = s;
}
}
return L;
}
DLinklist DLinklist_TailInsert(DLinklist& L, int n)//尾插法建立双链表
{
L = new DLNode();
L->next = NULL;
L->prev = NULL;
DLNode* p = L;
for (int i = 0; i < n; i++)
{
DLNode* s;int x;
cout << "please enter No."<<i+1<<" number that needs to be inserted" << endl;
cin >> x;
s = new DLNode();
s->data = x;;
s->next = p->next;
s->prev = p;
p->next = s;
p = s;
}
return L;
}
bool DLinklist_Search1(DLinklist L, int x)//按位查找
{
DLNode* p = L;
if (p->next != NULL)
{
p = p->next;
}
int i = 1;
while (p!=NULL && i!=x)
{
i++;
p = p->next;
}
if (p != NULL)
{
cout << "Search success,the No." << x << " number is " << p->data << endl;
return true;
}
else
{
cout << "serach error" << endl;
return false;
}
}
bool DLinklist_Search2(DLinklist L, int x)//按值查找
{
DLNode* p = L;
if (p->next == NULL)
{
cout << "the number you searched is not exist" << endl;
return false;
}
p = p->next;
int i = 1; int j = 0;
while (p != NULL)
{
if (p->data == x)
{
cout << "the No." << i << " number is " << x << endl;
j++;
}
p = p->next;
i++;
}
if (j != 0)
{
return true;
}
else
{
cout << "the number you searched is not exist" << endl;
return false;
}
}
DLinklist DLinklist_Delete1(DLinklist& L, int x)//按位删除
{
cout << "the No." << x << " number you entered needs to be deleted" << endl;
int i = 1; DLNode* p = L;
while (i != x && p->next!=NULL)
{
i++;
p = p->next;
}
p = p->next;
if (p == NULL)
{
cout << "delete error" << endl;
}
else if(p->next!=NULL)
{
p->prev->next = p->next;
p->next->prev = p->prev;
}
else
{
p->prev->next = p->next;
}
return L;
}
DLinklist DLinklist_Delete2(DLinklist& L, int x)//按值删除
{
DLNode* p = L;
while (p->next != NULL)
{
DLNode* s;
p = p->next;
if (p->data == x)
{
if (p->next == NULL)
{
p->prev->next = p->next;
}
else
{
p->next->prev = p->prev;
p->prev->next = p->next;
}
}
}
return L;
}
bool DLinklist_Traversal(DLinklist L)//双链表遍历
{
if (L->next ==NULL)
return false;
else
do
{
cout << L->next->data << " ";
L = L->next;
} while (L->next!= NULL);
cout << endl;
return true;
}
int main()
{
DLinklist L1; DLinklist L2;
DLinklist_HeadInsert(L1, 5);
DLinklist_TailInsert(L2,5);
DLinklist_Traversal(L1);
DLinklist_Traversal(L2);
DLinklist_Delete1(L2, 3);
DLinklist_Traversal(L2);
DLinklist_Delete2(L2, 10);
DLinklist_Traversal(L2);
DLinklist_Search1(L2, 4);
DLinklist_Search2(L2, 20);
system("pause");
return 0;
}
循环单链表:
#include <iostream>
using namespace std;
typedef struct CSLinkNode
{
int data;
struct CSLinkNode* next;
}CSLinkNode, * CSLinkList;
CSLinkList CSLinkListHeadInsert(CSLinkList & L, int n)//头插法建立循环单链表
{
L = new CSLinkNode();
for (int i = 0; i < n; i++)
{
cout << "please enter No." << i + 1 << " number" << endl;
int x;
cin >> x;
CSLinkNode* s = new CSLinkNode();
s->data = x;
if (i == 0)
{
s->next = L;
L->next = s;
}
else
{
s->next = L->next;
L->next = s;
}
}
return L;
}
CSLinkList CSLinkListTailInsert(CSLinkList& L, int n)//尾插法建立循环单链表
{
L = new CSLinkNode();
CSLinkNode* r = L; r->next = L;
for (int i = 0; i < n; i++)
{
cout << "please enter No." << i + 1 << " number" << endl;
int x;
cin >> x;
CSLinkNode* s=new CSLinkNode();
s->data = x;
s->next = r->next;
r->next = s;
r = s;
r->next = L;
}
return L;
}
bool CSLinkListTraversal(CSLinkList L)//循环单链表遍历
{
if (L->next == L)
{
return false;
}
CSLinkNode* r = L;
while (r->next != L)
{
r = r->next;
cout << r->data << " ";
}
cout << endl;
return true;
}
int main()
{
CSLinkList L;
CSLinkListHeadInsert(L, 5);
CSLinkListTailInsert(L, 5);
CSLinkListTraversal(L);
system("pause");
return 0;
}
循环双链表:
#include <iostream>
using namespace std;
typedef struct CSDLinkNode
{
int data;
struct CSDLinkNode* prev, * next;
}CSDLinkNode,* CSDLinkList;
CSDLinkList CSDLinkListHeadInsert(CSDLinkList& L,int n)//头插法建立循环双链表
{
L = new CSDLinkNode();
L->prev = NULL; L->next = NULL;
for (int i = 0; i < n; i++)
{
cout << "Please enter the No." << i + 1 << " number that you need insert" << endl;
int x;
cin >> x;
CSDLinkNode* s = new CSDLinkNode();
s->data = x;
if (i == 0)
{
s->next = L;
L->prev = s;
s->prev = L;
L->next = s;
}
else
{
s->next = L->next;
L->next->prev = s;
s->prev = L;
L->next = s;
}
}
return L;
}
CSDLinkList CSDLinkListTailInsert(CSDLinkList& L, int n)//尾插法建立循环双链表
{
L = new CSDLinkNode();
L->prev = NULL; L->next = NULL;
CSDLinkNode* s;
for (int i = 0; i < n; i++)
{
cout << "Please enter the No." << i + 1 << " number that you need insert" << endl;
int x;
cin >> x;
s = new CSDLinkNode();
s->data = x;
if (i == 0)
{
s->next = L;
L->prev = s;
s->prev = L;
L->next = s;
}
else
{
s->next = L;
s->prev = L->prev;
L->prev->next = s;
L->prev = s;
}
}
return L;
}
bool CSDLinkListTraversal(CSDLinkList L)//遍历循环双链表
{
CSDLinkNode* r = L;
if (r->next == L)
{
return false;
}
else
{
r = r->next;
while(r!=L)
{
cout << r->data << " ";
r = r->next;
}
return true;
}
}
int main()
{
CSDLinkList L;
CSDLinkListHeadInsert(L, 5);
CSDLinkListTailInsert(L, 5);
CSDLinkListTraversal(L);
system("pause");
return 0;
}