連結串列&鏈棧&佇列

-小淮要努力發表於2021-01-02

資料結構線性儲存

一級目錄

連結串列:



#if 0
#include<iostream>
using namespace std;
typedef int Status;
//typedef struct {
//	char num[8];
//	char name[8];
//	int score;
//}ElemType;

typedef int ElemType;
typedef struct Lnode
{
	ElemType data;
	struct Lnode *next;
}Lnode,*LinkList;
//Lnode L;
Status InitList_L(LinkList &L)
{
	
	//L = (LinkList)malloc(sizeof(Lnode));
	L = new Lnode;
	L->next = NULL;
}
int ListEmpty(LinkList L)
{
	if (L->next) {
		return 0;
	}
	else {
		return false;
	}
}
Status DeStroyList(LinkList &L)
{
	Lnode *p;
	while (L)
	{
		p = L;
		L = L->next;
		delete p;
	}
}
Status ClearList(LinkList &L)
{
	Lnode *p,*q;
	p = L->next;
	while (p)
	{
		q = p->next;
		delete p;
		p = q;
	}
	L->next = NULL;

}
int ListLength_L(LinkList L)
{
	Lnode *p;
	int i = 0;
	p = L->next;
	if (L->next == NULL) return 0;
	else
	{
		while (p)
		{
			i++;
			p = p->next;
		}
		return i;
	}
}
//單連結串列的基本操作
Status GetElem_L(LinkList L, int i, ElemType &e)
{
	Lnode *p;
	int j = 1;
	p = L->next;
	while (p&&j < i)
	{
		p = p->next;
		j++;
	}
	if (!p || j > i) return false;
	e = p->data;

}
Lnode *LocateElem_L(LinkList L, ElemType e)
{
	
	Lnode *p = L->next;
	while (p && p->data != e)
	{
		p = p->next;
		return p;
	}

}
int LocateElem(LinkList L, ElemType e)
{
	Lnode *p = L->next;
	int j = 1;
	while (p && p->data != e)
	{
		p = p->next;
		j++;
	}
	if (p) {
		return j;
	}
	 return false;
}
//插入第i個節點
Status ListInsert_L(LinkList &L, int i, ElemType e)
{
	Lnode *p = L;
	int j = 0;
	while (p && j < i - 1)
	{
		p = p->next;
		j++;  //讓p指向i-1位置
	}
	if (!p || j > i - 1)
	{
		return false;
	}
	Lnode * s = new Lnode;
	s->data = e;
	s->next = p->next;
	p->next = s;
}
// 刪除
Status ListDelete_L(LinkList &L, int i, ElemType &e)
{
	Lnode *p = L;
	int j = 0;
	int i = 0;
	Lnode *q = new Lnode;
	while (p->next && j < i - 1)
	{
		p = p->next;
		j++;
	}
	if (!(p->next) || j > i - 1) return false;
	q = p->next;
	p->next = q->next;
	e = q->data;
	delete q;

}
//頭插法建立單連結串列
void CreatList_H(LinkList &L, int n)
{
	Lnode *L = new Lnode;
	L->next = NULL;
	for (int i = n; i > 0; --i)
	{
		Lnode *p = (Lnode*)malloc(sizeof(Lnode));
		p->next = L->next;
		L->next = p;
	}
}
//尾插法建立單連結串列
void CreatList_R(LinkList &L, int n)
{
	Lnode *L = new Lnode;
	L->next = NULL;
	Lnode *r = new Lnode;
	r = L;
	for (int i = 0; i < n; i++)
	{
		Lnode *p = new Lnode;
		p->next = NULL;
		r->next = p;
		r = p;
	}
}





typedef int ElemType;
typedef struct Lnode
{
	ElemType data;
	struct Lnode *next;
}Lnode,*LinkList;

int main()
{

	return 0;
}

#endif // 0

二級目錄

鏈棧:


#if 0

#include<iostream>
using namespace std;

 //鏈棧是運算受限的單連結串列,只能在連結串列頭部進行操作
/*
	連結串列的頭指標就是棧頂
	不需要頭節點
	基本不存在棧滿的情況
	空棧相當於頭指標指向空
	插入和刪除僅在棧頂處執行

*/
typedef int SElemType;
typedef int Status;
typedef struct StackNode
{
	SElemType data;
	struct SatckNode *next;
}StackNode,*Linkstack;

//鏈棧的初始化
Status InitStack(Linkstack &S)
{
	//構造一個空棧,棧頂指標置空
	Linkstack S = (Linkstack)malloc(sizeof(StackNode));
	if (!S)
	{
		return false;
	}
	else
	{
		S = NULL;
		return true;
	}
	
}
Status StackEmpty(Linkstack S)
{
	if (S == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}

}
Status Push(Linkstack &S, SElemType e)
{
	StackNode *p = new StackNode;
	p->data = e;
	p->next = S;
	S = p;
	return true;

}
Status Pop(Linkstack &S, SElemType &e)
{
	StackNode *p = new StackNode;
	if (S == NULL) return false;
	e = S->data;
	p = S;
	S = S->next;
	delete p;
	return true;
}

int main()
{


	return 0;
}
#endif // 0

三級目錄

佇列:


#if 0
#include<iostream>
using namespace std;
//鏈式佇列
#define MAXQSIZE 100
typedef int Status;
typedef int QEleType;
typedef struct Qnode
{
	QEleType data;
	struct Qnode *next;
}Qnode,*QueuePtr;
typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(Qnode));
	if (!Q.front) exit(OVERFLOW);
	else
	{
		Q.front->next = NULL;
		return true;
	}
}

//從隊頭結點開始,以此釋放所有結點
Status DestroyQueue(LinkQueue &Q)
{
	Qnode *p = new Qnode;
	while (Q.front)
	{
		p = Q.front->next;
		free(Q.front);
		Q.front = p;
		return true;
	}
	delete p;
	p = nullptr;
}
//入隊

Status EnQueue(LinkQueue &Q, QEleType e)
{
	Qnode *p = (QueuePtr)malloc(sizeof(Qnode));
	if (!p)  exit(OVERFLOW);
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return true;
}
Status DeQueue(LinkQueue &Q, QEleType e)
{
	Qnode *p = (QueuePtr)malloc(sizeof(Qnode));
	if (Q.front == Q.rear) exit(OVERFLOW);
	p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear == p) Q.rear = Q.front;
	delete p;
	return true;
}
Status GetHead(LinkQueue Q, QEleType &e)
{
	if (Q.front == Q.rear) return false;
	return e = Q.front->next->data;
}





//迴圈佇列
#define MAXQSIZE 100
typedef int QElemType;
typedef int Status;
typedef struct
{
	QElemType *base;
	int front;
	int rear;
}SqQueue;
Status IninQueue(SqQueue &Q)
{
	Q.base = new QElemType(MAXQSIZE);
	//Q.base = (QElemType*)malloc(sizeof(QElemType)*MAXQSIZE);
	if (!Q.base) exit(OVERFLOW);
	Q.front = Q.rear = 0;
}
int QueueLength(SqQueue Q)
{
	return ((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE);
}
//入隊
Status EnQueue(SqQueue &Q, QElemType &e)
{
	if ((Q.rear + 1) % MAXQSIZE == Q.front) exit(OVERFLOW);//隊滿
	else
	{
		Q.base[Q.rear] = e;
		Q.rear = (Q.rear + 1) % MAXQSIZE;
		return true;
	}
}
//出隊
Status DeQueue(SqQueue &Q, QElemType e)
{
	if (Q.front == Q.rear) exit(OVERFLOW);
	else
	{
		e = Q.base[Q.front];
		Q.front = (Q.front + 1) % MAXQSIZE;
		return true;
	}
}
//取隊頭元素
/*Status GetHead(SqQueue Q)
{
	if (Q.base != Q.front);
	return Q.base[Q.front];
}*/

int main()
{
	
	return 0;

}


#endif // 0

相關文章