數據結構基礎(線性表與棧與隊列)


定長順序表 Seqlist

Seqlist.h

#pragma once   //  防止頭文件重複包含,適用於VS編譯器下

/*	防止頭文件重複包含
#ifndef __SEQLIST_H
#define __SEQLIST_H
……
……
#endif
*/

typedef int ElemType;

#define SEQLEN 10

typedef struct _SqList
{
	ElemType data[SEQLEN];  //  存儲數據元素的連續空間
	int count;   			//   記錄有效元素個數的變量
}SqList;

// 初始化
bool Init_SqList(SqList* sq);

// 插入
bool Insert_Head(SqList* sq, ElemType val);
bool Insert_Tail(SqList* sq, ElemType val);
bool Insert_Pos(SqList* sq, ElemType val, int pos);

//刪除
bool Delete_Head(SqList* sq);
bool Delete_Tail(SqList* sq);
bool Delete_Pos(SqList* sq, int pos);

//查找
int FindPos_Element(SqList* sq, ElemType val); //  0 --- SEQLEN-1   -1
bool FindElement_Pos(SqList* sq, int pos, ElemType* result);

// 清空
void Clear_SqList(SqList* sq);

// 銷燬

// 顯示
void Show_SqList(SqList* sq);

Seqlist.cpp

#include "Seqlist.h"
#include <assert.h>
#include <stdio.h>

bool Init_SqList(SqList* sq)
{
	assert(NULL != sq);
	if (NULL == sq)  return false;

	sq->count = 0;    //  初始化順序表,表示順序中沒有存儲任何有效數據

	return true;
}

bool Insert_Head(SqList* sq, ElemType val)
{
	return Insert_Pos(sq, val, 0);
}
bool Insert_Tail(SqList* sq, ElemType val)
{
	return Insert_Pos(sq, val, sq->count);
}
//指定位置插入
bool Insert_Pos(SqList* sq, ElemType val, int pos)
{
	if (sq->count >= SEQLEN || sq->count < 0)	return false;	//滿或非法

	if (pos > sq->count)		pos = sq->count;				//位置不合理

	for (int i = sq->count; i >= pos; i--)			//從後往前遍歷
	{
		sq->data[i] = sq->data[i - 1];
	}

	sq->data[pos] = val;
	++sq->count;

	return true;
}


bool Delete_Head(SqList* sq)
{
	return Delete_Pos(sq, 0);
}
bool Delete_Tail(SqList* sq)
{
	return Delete_Pos(sq, sq->count - 1);
}
//指定位置刪除
bool Delete_Pos(SqList* sq, int pos)//pos表示下標
{
	if (sq->count == 0)	return false;
	if (pos < 0 || pos >= SEQLEN)	return false;

	int i = pos;
	while (i < sq->count - 1)
	{
		sq->data[i] = sq->data[i + 1];
		i++;
	}
	sq->count--;
	return true;
}

//指定元素查找,返回順序表下標
int FindPos_Element(SqList* sq, ElemType val)
{
	/*
	分析:
	1、循環遍歷查找元素val,返回元素下標
	*/
	int i = 0;
	while (i < sq->count)
	{
		if (sq->data[i] == val)
		{
			break;
		}
		++i;
	}

	if (i == sq->count)	return -1;	//查找失敗,返回-1
	return i;
}

//指定位置查找,形參接收查找結果
bool FindElement_Pos(SqList* sq, int pos, ElemType* result)
{
	/*
	分析:
	1、pos非法,pos <0 || pos >SEQLEN -1
	2、pos位置無值,pos >count-q
	3、找到,賦值給result
	*/
	if (pos < 0 || pos > SEQLEN - 1)	return false;
	if (pos > sq->count - 1)			return false;

	*result = sq->data[pos];
	return true;
}

// 清空
void Clear_SqList(SqList* sq)
{	
	sq->count = 0;
}

//打印表
void Show_SqList(SqList* sq)
{
	for (int i = 0; i < sq->count; ++i)
	{
		printf("<-- %d ", sq->data[i]);
	}

	printf("\n");
}

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
#include <stdio.h>

int main()
{
	printf("順序表,可以儲存10個整型數字\n");
	SqList sq;  //  定義一個順序表
	Init_SqList(&sq);
	while (true)
	{

		int a;
		scanf("%d", &a);
		Insert_Head(&sq, a);
		Show_SqList(&sq);

	}

	return 0;
}


回到標題

不定長順序表AlterList

AlterList.h

#pragma once

typedef int ElemType;
#define INITSIZE  5

typedef struct _AlterList
{
	ElemType* data;  //  指向用戶申請的堆區空間
	int count;     //  元素個數
	int listsize;  // 存儲空間的容量
}AList;

// 初始化
void AlterList_Init(AList* alist);

//頭插
void Insert_Head(AList* alist, ElemType val);

//尾插
void Insert_Tail(AList* alist, ElemType val);

//按位置插
void Insert_Pos(AList* alist, ElemType val, int pos);

//顯示元素
void Show_AList(AList* alist);

//按位置刪除
void Delete_Pos(AList* alist, int pos);
//頭刪
void Delete_Head(AList* alist);
// 尾刪
void Delete_Tail(AList* alist);
//刪除重複元素
void Delete_Element(AList* alist, ElemType val);

//根據元素查位置
int  FindPos_Element(AList* alist, ElemType val);
//根據條件查找
//int  FindPos_Locate(AList* alist, ElemType val, bool(*compare)(ElemType, ElemType));

//逆置
void Reverse_AList(AList* alist);

//清空
void  Clear_AList(AList* alist);

//銷燬
void  Destroy_AList(AList* alist);

AlterList.cpp

#include "AlterList.h"
#include <malloc.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

static bool IsFull(AList* alist)
{
	return alist->count == alist->listsize;
}

static bool IsEmpty(AList* alist)
{
	return alist->count == 0;
}

static void AppendSpace(AList* alist)
{
	alist->listsize *= 2;
	ElemType* s = (ElemType*)malloc(alist->listsize * sizeof(ElemType));
	assert(s != NULL);

	for (int i = 0; i < alist->count; ++i)
	{
		s[i] = alist->data[i];
	}

	free(alist->data);
	alist->data = s;
}

void AlterList_Init(AList* alist)
{
	assert(alist != NULL);
	if (alist == NULL)
	{
		return;
	}

	alist->count = 0;
	alist->listsize = INITSIZE;
	alist->data = (ElemType*)malloc(sizeof(ElemType) * alist->listsize);
	assert(alist->data != NULL);
}

//頭插
void Insert_Head(AList* alist, ElemType val)
{
	Insert_Pos(alist, val, 0);
}

//尾插
void Insert_Tail(AList* alist, ElemType val)
{
	Insert_Pos(alist, val, alist->count);
}

//按位置插
void Insert_Pos(AList* alist, ElemType val, int pos)
{
	if (pos < 0 || pos > alist->count) return;

	if (IsFull(alist))
	{
		//  擴容
		AppendSpace(alist);
	}

	int i = alist->count;
	while (i > pos)   //  最後一次 i == pos+1
	{
		alist->data[i] = alist->data[i - 1];
		i--;
	}

	alist->data[pos] = val;
	alist->count++;
}

void Show_AList(AList* alist)
{
	for (int i = 0; i < alist->count; ++i)
	{
		printf("%d  ", alist->data[i]);
	}

	printf("\n");
}


//按位置刪除
void Delete_Pos(AList* alist, int pos)
{
	/*
	1、檢查pos位置是否合法
	2、從後往前移動,刪除(覆蓋)POS位置的值
	*/
	if (pos < 0 || pos >= alist->count)	return;

	int i = pos;
	while (i < alist->count - 1)
	{
		alist->data[i] = alist->data[i + 1];
	}

	alist->count--;
}

//頭刪
void Delete_Head(AList* alist)
{
	return Delete_Pos(alist, 0);
}

// 尾刪
void Delete_Tail(AList* alist)
{
	return Delete_Pos(alist, alist->count - 1);
}

//刪除重複元素
void Delete_Element(AList* alist, ElemType val)
{
	int i = 0;
	int j = 0;
	while (j < alist->count)
	{
		if (alist->data[j] == val)
		{
			++j;
		}
		else if (alist->data[j] != val)
		{
			if (alist->data[i] != alist->data[j])		//避免不必要的賦值運算
			{
				alist->data[i] = alist->data[j];
			}
			//向後遍歷
			++i;
			++j;
		}

	}
	alist->count -= j - i;	//差值爲該重複元素的數量
}


//根據元素查位置
int  FindPos_Element(AList* alist, ElemType val)
{
	for (int i = 0; i < alist->count; i++)
	{
		if (alist->data[i] == val)
		{
			return i;
		}

	}

	return -1;
}


//逆置
void Reverse_AList(AList* alist)
{
	/*
	首位互換,通過指向頭與爲的下標向中間遍歷
	*/
	for (int i = 0, j = alist->count - 1; i < j; ++i, --j)
	{
		if (alist->data[i] != alist->data[j])//相同的值不交換
		{
			ElemType tmp = alist->data[i];
			alist->data[i] = alist->data[j];
			alist->data[j] = tmp;
		}

	}

}


//清空
void  Clear_AList(AList* alist)
{
	alist->count = 0;
}


//銷燬
void  Destroy_AList(AList* alist)
{
	free(alist->data);
	alist->count = alist->listsize = 0;
}

main.cpp

#include <stdio.h>
#include "AlterList.h"

int main()
{
	AList alist;
	AlterList_Init(&alist);

	for (int i = 0; i < 5; ++i)
	{
		Insert_Head(&alist, i);
	}

	Show_AList(&alist);
	//頭插入測試
	Insert_Head(&alist, 10);
	Insert_Head(&alist, 10);
	Show_AList(&alist);
	//尾插測試
	Insert_Tail(&alist, 10);
	Show_AList(&alist);
	//查找測試
	//int i = FindPos_Element(&alist, 3);
	//alist.data[i];
	//printf("是3的下標\n");
	//去重刪除
	printf("去重刪除\n");
	Delete_Element(&alist, 10);
	Show_AList(&alist);
	//逆置
	printf("逆置\n");
	Reverse_AList(&alist);
	Show_AList(&alist);
	//清空
	Clear_AList(&alist);
	Show_AList(&alist);
	//銷燬
	Destroy_AList(&alist);

	return 0;
}


回到標題

單鏈表 LinkList

LinkList.h

#pragma once

typedef int ElemType;

typedef struct _Node
{
	ElemType data;
	struct _Node* next;
}LNode;

// 初始化
void LinkList_Init(LNode* list);

// 頭插
bool Insert_Head(LNode* list, ElemType val);

// 尾插
bool Insert_Tail(LNode* list, ElemType val);

//位置插
bool  Insert_Pos(LNode* list, ElemType val, int pos);

//顯示
void Show(LNode* list);

// 刪除
bool Delete_Head(LNode* list);
bool Delete_tail(LNode* list);
bool Dele_pos(LNode* list, ElemType pos);

// 查找位置
LNode* FindPos_Element(LNode* list, ElemType val);

//條件查找
LNode* FindPos_Locate(LNode* list, ElemType val,
	bool(*compare)(ElemType, ElemType));

// 逆置
void Reserve_List(LNode* list);

// 清空
void Clear_List(LNode *list);

// 銷燬
void Destory_List(LNode* list);

LinkList.cpp

#include "LinkList.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>

//判空
static bool IsEmpty(LNode* list)
{
	return list->next == NULL;
}

//求長度
static int GetLength(LNode* list)
{
	int count = 0;
	LNode* p = list->next;

	while (p != NULL)
	{
		count++;
		p = p->next;
	}

	return count;
}

//追加新結點
static LNode* ApplyNode(ElemType val, LNode* next)
{
	LNode* s = (LNode*)malloc(sizeof(LNode));
	assert(s != NULL);
	if (s == NULL)
	{
		return NULL;
	}

	s->data = val;
	s->next = next;

	return s;
}

// 初始化
void LinkList_Init(LNode* list)
{
	assert(list != NULL);
	if (list == NULL) return;

	list->next = NULL;
}

// 頭插
bool Insert_Head(LNode* list, ElemType val)
{
	if (NULL == (list->next = ApplyNode(val, list->next)))
	{
		return false;
	}

	return true;
}

// 尾插
bool Insert_Tail(LNode* list, ElemType val)
{
	LNode* p = list;

	while (p->next != NULL)
	{
		p = p->next;
	}

	if (NULL == (p->next = ApplyNode(val, p->next)))
	{
		return false;
	}

	return true;
}

//位置插
bool  Insert_Pos(LNode* list, ElemType val, int pos)
{
	if (pos < 0 || pos > GetLength(list)) return false;

	LNode* p = list;
	while (pos)   //  while循環退出後,p指向要插入位置的前一個結點
	{
		p = p->next;
		pos--;
	}

	if (NULL == (p->next = ApplyNode(val, p->next)))
	{
		return false;
	}

	return true;
}

//顯示
void Show(LNode* list)
{
	LNode* p = list->next;	//第一個節點
	while (p != NULL)
	{

		printf("%d   ", p->data);
		p = p->next;
	}
	printf("\n");
}

// 刪除
bool Delete_Head(LNode* list)
{
	//LNode* p = list->next;
	//list->next = p->next;
	//free(p);
	Dele_pos(list, 0);
	return true;
}
// 尾刪
bool Delete_tail(LNode* list)
{
	Dele_pos(list, GetLength(list) - 1);
	return true;
}
//位置刪
bool Dele_pos(LNode* list, ElemType pos)
{
	if (pos<0 || pos >= GetLength(list))	return false;

	//找前驅
	LNode* p = list;
	while (pos > 0)
	{
		p = p->next;
		--pos;
	}

	//pos位置
	LNode* q = p->next;

	p->next = q->next;
	free(q);
}

// 查找位置
LNode* FindPos_Element(LNode* list, ElemType val)
{
	LNode* p = list->next;
	while (p != NULL)
	{
		if (p->data == val)
		{
			return p;
		}
		p = p->next;
	}
	
	//找不到返回NULL
	return NULL;
}

//條件查找
LNode* FindPos_Locate(LNode* list, ElemType val,
	bool(*compare)(ElemType, ElemType))
{
	LNode* p = list->next;
	while (p != NULL)
	{
		if (compare(val, 10))
		{
			return p;
		}
		p = p->next;
	}

	return NULL;
}

// 逆置
void Reserve_List(LNode* list)
{
	LNode* q = NULL, * s = NULL;
	LNode* p = list->next;	//從第一個節點開始遍歷至鏈表結尾,依次改變每一個節點的next指向
	while (p != NULL)
	{
		q = p->next;	//暫存下一節點
		p ->next= s;	//改變當前節點指針指向,指向上一個節點
		s = p;			//暫存上一個節點
		p = q;			//遍歷下一個節點
	}
	//遍歷結束時,p指向空,s指向原鏈表的爲節點
	list->next = s;
}

// 清空
void Clear_List(LNode* list)
{
	LNode* p = NULL;
	while (list->next != NULL)
	{
		p = list->next;
		list->next = p->next;
		free(p);
	}
}

// 銷燬
void Destory_List(LNode* list)
{
	Clear_List(list);
}

main.cpp

#include "LinkList.h"
#include <stdio.h>
int main()
{
	LNode list;
	LinkList_Init(&list);
	//添加元素
	Insert_Head(&list, 0);
	Insert_Tail(&list, 6);
	for (int i = 1; i < 6; ++i)
	{
		Insert_Pos(&list, i, i);
	}
	Show(&list);
	/*                       刪除                     */
	printf("\n頭刪\t");
	Delete_Head(&list);
	Show(&list);
	printf("\n尾刪\t");
	Delete_tail(&list);
	Show(&list);
	/*                      逆置                        */
	printf("\n逆置\t");
	Reserve_List(&list);
	Show(&list);
	/*                    查找                                */
	LNode* p = NULL;
	printf("\n查找 \"3\"\t");
	p = FindPos_Element(&list,3);
	if (p != NULL)
		printf("%d\n", p->data);
	else	printf("找不到\n");

	printf("\n查找 \"6\"\t");
	p = FindPos_Element(&list, 6);
	if (p != NULL)
		printf("%d\n", p->data);
	else	printf("找不到\n");


	Clear_List(&list);
	return 0;
}



回到標題

雙向鏈表 DuLinkList

DuLinkList.h

#pragma once

typedef int ElemType;

typedef struct _DuList
{
	ElemType data;
	struct _DuList* next;
	struct _DuList* prior;
}DuList;


//初始化
DuList* CreateDuList();
//創建一個節點
DuList* CreateNode(ElemType data);
//頭插
void InsertHead(DuList* head, ElemType data);
//尾插
void InsertTail(DuList* head, ElemType data);
//任意位置插
void InsertPos(DuList* head, ElemType data, int pos);
//刪頭
void DeleteHead(DuList* head);
//刪尾
void DeleteTail(DuList* head);
//按位置刪
void DeletePos(DuList* head, int pos);
//按位置尋找
void SearchPos(DuList* head, int pos);
//清空
void ClearList(DuList* head);
//釋放
void FreeList(DuList* head);
//顯示
void ShowList(DuList* head);

DuLinkList.cpp

#include "DuLinkList.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>


static bool Isempty(DuList* head)
{
	return head->next == NULL;
}

static int GetLength(DuList* head)//長度
{
	if (Isempty(head))	return 0;

	int num = 0;
	DuList* p = head->next;
	while (p != NULL)
	{
		num++;
		p = p->next;
	}
	return num;
}

DuList* CreateDuList()//創建鏈表
{
	DuList* head = (DuList*)malloc(sizeof(DuList));
	assert(head != NULL);
	head->next = head->prior = NULL;
	return head;
}

DuList* CreateNode(ElemType data)//創建新的節點
{
	DuList* node = (DuList*)malloc(sizeof(DuList));
	assert(node != NULL);
	node->data = data;
	node->next = node->prior = NULL;
	return node;
}

void InsertHead(DuList* head, ElemType data)//頭插法
{
	InsertPos(head,data,0);
}

void InsertTail(DuList* head, ElemType data)//尾插法
{
	InsertPos(head, data, GetLength(head));
}

void InsertPos(DuList* head, ElemType data, int pos)//任意位置插
{
	if (pos<0 || pos>GetLength(head))	return;

	//前驅
	DuList* p = head;
	while (pos)
	{
		p = p->next;
		pos--;
	}

	DuList* newnode = CreateNode(data);
	newnode->next = p->next;
	p->next = newnode;
	newnode->prior = p;
	if (newnode->next != NULL)
	{
		newnode->next->prior = newnode;
	}
}

void DeleteHead(DuList* head)
{
	DeletePos(head,0);
}

void DeleteTail(DuList* head)
{
	DeletePos(head,GetLength(head)-1);
}

void DeletePos(DuList* head, int pos)
{
	if (pos < 0 || pos >= GetLength(head))	return;

	DuList* p = head;
	while (pos)
	{
		p = p->next;
		pos--;
	}
	DuList* q = p->next;
	p->next = q->next;
	if (p->next != NULL)
	{
		p->next->prior = p;
	}

	free(q);
}

void SearchPos(DuList* head, int pos)
{
	if (pos < 0 || pos >= GetLength(head))	return;
	DuList* p = head->next;

	while (pos)
	{
		p = p->next;
		pos--;
	}
	printf("查找成功,值爲:%d\n", p->data);
}

void ClearList(DuList* head)
{
	if (Isempty(head))	return;

	DuList* p = head->next;
	DuList* q;
	while (p != NULL)
	{
		q = p->next;
		free(p);
		p = q;
	}
	head->next = NULL;
}

void FreeList(DuList* head)
{
	if (head == NULL)
	{
		return;
	}
	DuList* q;
	while (head != NULL)
	{
		q = head->next;
		free(head);
		head = q;
	}
}

void ShowList(DuList* head)
{
	if (Isempty(head))	return;
	DuList* p = head->next;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

main.cpp

#include "DuLinkList.h"
#include <stdio.h>

int main()
{
	DuList* head = CreateDuList();
	for (int i = 10; i >= 0; --i)
	{
		InsertHead( head, i);
	}
	ShowList( head);

	DeletePos(head, 0);
	ShowList(head);

	SearchPos(head, 5);

	ClearList(head);

	InsertPos(head, 1, 0);
	ShowList(head);
	DeletePos(head, 0);
	ShowList(head);
	return 0;
}


回到標題

循環鏈表 CList

CList.h

#pragma once

typedef int ElemType;

typedef struct _CList
{
	ElemType data;
	struct _CList* next;
}CList;

CList* InitCList();//初始化循環鏈表

CList* CreateNode(ElemType data);//創建一個節點

void InsertByHead(CList* headCList, ElemType data);//頭插法

void InsertByTail(CList* headCList, ElemType data);//尾插法

void InsertByPos(CList* headCList, ElemType data, int pos);//按位置插

void DeleteByHead(CList* headCList);//刪頭

void DeleteByTail(CList* headCList);//刪尾

void DeleteByPos(CList* headCList, int pos);//按位置刪

void  ClearList(CList* headCList);//清空鏈表

void FreeList(CList* headCList);//銷燬鏈表

void Reverse(CList* headCList);//逆置

void ShowList(CList* headCList);

CList.cpp

#include "CList.h"
#include <stdio.h>
#include <malloc.h>
#include <assert.h>

static bool Isempty(CList* head)//判空
{
	return head->next == head;
}

static int GetLength(CList* head)//獲取該鏈表的長度,
{
	if (Isempty(head))	return 0;
	int num = 0;
	CList* p = head->next;
	while (p != head)
	{
		num++;
		p = p->next;
	}
	return num;//鏈表中的實際長度,不是最大的那個下標
}

CList* InitCList()//創建鏈表
{
	CList* head = (CList*)malloc(sizeof(CList));
	head->next = head;
	return head;
}

CList* CreateCList(ElemType data)//創建節點
{
	CList* newList = (CList*)malloc(sizeof(CList));
	newList->data = data;
	newList->next = NULL;//先讓這個節點指向空,後續再做處理
	return newList;
}

void InsertByPos(CList* head, ElemType data, int pos)//按位置插
{
	if (pos < 0 || pos >= GetLength(head))
	{
		return;
	}

	CList* p = head;
	while (pos)
	{
		p = p->next;
		--pos;
	}
	//此時p是要插入位置的前一個節點
	CList* newCList = CreateCList(data);
	newCList->next = p->next;
	p->next = newCList;
}

void InsertByHead(CList* head, ElemType data)//頭插法
{
	CList* newCList = CreateCList(data);
	newCList->next = head->next;
	head->next = newCList;
	//InsertInPos(head,data,0);
}

void InsertByTail(CList* head, ElemType data)//尾插法
{

	CList* p = head;
	while (p->next != head)
	{
		p = p->next;
	}//此時p爲最後一個節點

	CList* newCList = CreateCList(data);
	p->next = newCList;
	newCList->next = head;

	//InsertInPos(head,data,GetLength(head));
}

void DeleteByPos(CList* head, int pos)//按位置刪
{
	if (pos < 0 || pos >= GetLength(head))
	{
		return;
	}

	CList* posCList = head->next;//要刪除的節點
	CList* posfrontCList = head;//要刪除節點的前一個節點

	assert(posCList != NULL);
	if (posCList == NULL)
	{
		printf("數據爲空,無法刪除!\n");
		return;
	}

	while (pos)
	{
		posfrontCList = posfrontCList->next;
		posCList = posCList->next;
		pos--;
	}
	posfrontCList->next = posCList->next;
	free(posCList);
	printf("刪除成功!\n");
}

void DeleteByHead(CList* head)
{
	CList* p = head->next;//頭結點
	head->next = p->next;
	free(p);
	//DeleteInPos(head,0);
}

void DeleteByTail(CList* head)
{
	CList* p = head;
	while (p->next->next != head)
	{
		p = p->next;
	}//此時p是要刪除位置的前一個節點

	CList* q = p->next;//q是要刪除位置的節點
	p->next = head;
	free(q);
	//DeleteInPos(head,GetLength(CList)-1);
}

void  ClearList(CList* head)
{
	if (Isempty(head))		return;

	CList* p;
	CList* q;
	p = head->next;
	while (p != head)
	{
		q = p->next;
		free(p);
		p = q;
	}
	head->next = head;
}

void FreeList(CList* head)
{
	if (head == NULL)
	{
		return;
	}

	CList* p = head->next;//第一個節點
	CList* q;
	while (p != head)
	{
		q = p->next;
		free(p);
		p = q;
	}
	free(head);
}

void Reverse(CList* head)
{
	CList* p = head->next;
	CList* q = NULL;
	CList* s = head;
	while (p != head)
	{
		q = p->next;
		p->next = s;
		s = p;
		p = q;
	}
	head->next = s;
}

void ShowList(CList* head)
{
	CList* p = head->next;
	while (p != head)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

main.cpp

#include "CList.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
	CList* head = InitCList();
	for (int i = 0; i < 10; i++)
	{
		InsertByHead(head, i + 1);
	}
	ShowList(head);
	
	Reverse(head);
	ShowList(head);

	DeleteByPos(head, 5);
	ShowList(head);

	return 0;
}


回到標題

順序棧 SeqStack

SeqStack.h

#pragma once

typedef int ElemType;//存儲數據的類型

#define SIZE 10

typedef struct Stack
{
	ElemType data[SIZE];
	int num;
}Stack;

bool InitStack(Stack* headnode);//創建棧

bool Push(Stack* headnode, ElemType val);//插入(尾)

bool Pop(Stack* headnode);//出棧(尾)

ElemType Top(Stack* headnode);//獲取棧頂元素

bool Clear(Stack* headnode);//清空棧

SeqStack.cpp

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<assert.h>
#include"SeqStack.h"

static bool IsFull(Stack* headnode)
{
	return headnode->num == SIZE;
}

bool InitStack(Stack* headnode)
{
	if (headnode == NULL)	return false;

	headnode->num = 0;
	return true;
}

bool Push(Stack* headnode, ElemType val)
{
	if (IsFull(headnode) || headnode == NULL)	return false;
	headnode->data[headnode->num] = val;
	(headnode->num)++;
	return true;
}

bool Pop(Stack* headnode)
{
	if (headnode == NULL || headnode->num == 0)	return false;
	(headnode->num)--;
	return true;
}

ElemType Top(Stack* headnode)
{
	if (headnode == NULL || headnode->num == 0)	return -1;
	return headnode->data[headnode->num];
}

bool Clear(Stack* headnode)
{
	if (headnode == NULL)	return false;
	headnode->num = 0;
	return true;
}

main.cpp

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<assert.h>
#include"SeqStack.h"

int main()
{
	Stack headNode;
	InitStack(&headNode);
	for (int i = 0; i < 10; i++)
	{
		Push(&headNode, i + 1);
	}
	for (int i = 0; i < 5; i++)
	{
		printf("%d\t", Top(&headNode));
		Pop(&headNode);
	}

	Clear(&headNode);
	printf("\n%d\n", Top(&headNode));

	return 0;
}


回到標題

鏈棧 LinkStack

LinkStack.h

#pragma once

typedef  int ElemType;

typedef struct Stack
{
	ElemType data;
	struct Stack* next;
}Stack;
//初始化鏈棧
Stack* InitStack();
//頭插入
bool Push(Stack* headNode, ElemType val);
//創建新的節點
Stack* CreateNode(ElemType val);
//頭出
bool Pop(Stack* headNode);
//獲取棧頂元素
ElemType Top(Stack* headNode);
//清空棧
bool Clear(Stack* headNode);

LinkStack.cpp


#include<stdio.h>
#include"LinkStack.h"
#include<malloc.h>
#include<assert.h>
#include<stdlib.h>

Stack* InitStack()
{
	Stack* headNode = (Stack*)malloc(sizeof(Stack));
	headNode->next = NULL;
	return headNode;
}

Stack* CreateNode(ElemType val)
{
	Stack* newnode = (Stack*)malloc(sizeof(Stack));
	if (newnode == NULL)	return NULL;
	newnode->data = val;
	newnode->next = NULL;
	return newnode;
}

bool Push(Stack* headNode, ElemType val)
{
	if (headNode == NULL)	return false;
	Stack* p = CreateNode(val);
	p->next = headNode->next;
	headNode->next = p;
	return true;
}

bool Pop(Stack* headNode)
{
	if (headNode == NULL || headNode->next == NULL)	return false;
	Stack* p = headNode->next;
	headNode->next = p->next;
	free(p);
}

ElemType Top(Stack* headNode)
{
	if (headNode == NULL || headNode->next == NULL) return -1;

	return headNode->next->data;
}

bool Clear(Stack* headNode)
{
	if (headNode == NULL)	return false;

	while (headNode->next != NULL)
	{
		Pop(headNode);
	}
	return true;
}

main.cpp

#include<stdio.h>
#include"LinkStack.h"
#include<malloc.h>
#include<assert.h>
#include<stdlib.h>

int main()
{
	Stack* headNode = InitStack();
	for (int i = 0; i < 10; i++)
	{
		Push(headNode, i+1);
	}
	for (int i = 0; i < 5; i++)
	{
		printf("%d\t",Top(headNode));
		Pop(headNode);
	}

	Clear(headNode);
	printf("\n%d\n", Top(headNode));
	return 0;
}


回到標題

順序隊列 SeqQueue

SeqQueue.h

#pragma once

typedef int ElemType;

#define SIZE 10

typedef struct Queue
{
	ElemType* data;
	int num;
}Queue;

bool InitQueue(Queue* headnode);//隊列的初始化

bool EnQueue(Queue* headnode, ElemType val);//入隊列,尾插

bool DeQueue(Queue* headnode);//出隊列,頭刪

ElemType Top(Queue* headnode);//獲取對頭元素

bool Clear(Queue* headnode);//清空

bool Free(Queue* headnode);//銷燬

SeqQueue.cpp

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
#include"SeqQueue.h"

static bool IsEmpty(Queue* headnode)
{
	return headnode->num == 0;
}

static bool IsFull(Queue* headnode)
{
	return headnode->num == SIZE;
}

bool InitQueue(Queue* headnode)
{
	if (headnode == NULL)	return false;

	headnode->data = (ElemType*)malloc(sizeof(ElemType) * SIZE);
	if (headnode->data == NULL)	return false;

	headnode->num = 0;
	return true;
}

bool EnQueue(Queue* headnode, ElemType val)
{
	if (headnode == NULL || IsFull(headnode))	return false;
	headnode->data[headnode->num] = val;
	headnode->num++;
	return true;
}

bool DeQueue(Queue* headnode)
{
	if (headnode == NULL || IsEmpty(headnode))	return false;
	for (int i = 0; i < headnode->num - 1; i++)
	{
		headnode->data[i] = headnode->data[i + 1];
	}
	headnode->num--;
	return true;
}

ElemType Top(Queue* headnode)
{
	if (headnode == NULL || IsEmpty(headnode))	return -1;

	return headnode->data[0];
}

bool Clear(Queue* headnode)
{
	if (headnode == NULL)	return false;
	headnode->num = 0;
	return true;
}

bool Free(Queue* headnode)
{
	if (headnode == NULL)	return false;

	free(headnode->data);//釋放掉原來申請的空間,此時*data還存在
	headnode->num = 0;//
	headnode->data = NULL;//
	return true;
}

main.cpp

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
#include"SeqQueue.h"

int main()
{
	Queue headnode;
	InitQueue(&headnode);

	for (int i = 0; i < 10; ++i)
	{
		EnQueue(&headnode, i);
	}

	for (int i = 0; i < 5; ++i)
	{
		printf("%d\t",Top(&headnode));
		DeQueue(&headnode);
	}

	Clear(&headnode);
	printf("\n%d", Top(&headnode));
	
	return 0;
}


回到標題

鏈式隊列 LinkQueue

LinkQueue.h

#pragma once

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node* next;
}Node;

typedef struct Queue
{
	Node* head;
	Node* tail;
}Queue;

bool	InitQueue(Queue* headnode);//初始化

bool EnQueue(Queue* headnode, ElemType val);//插入

bool DeQueue(Queue* headnode);//出隊

ElemType Top(Queue* headnode);//獲取對頭

bool Clear(Queue* headnode);//清空

LinkQueue.cpp

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include"LinkQueue.h"

bool	InitQueue(Queue* headnode)
{
	if (headnode == NULL)	return false;

	headnode->head = headnode->tail = NULL;
	return true;
}

bool EnQueue(Queue* headnode, ElemType val)
{
	if (headnode == NULL)	return false;

	Node* newnode = (Node*)malloc(sizeof(Node));
	if (newnode == NULL)	return false;

	newnode->data = val;
	newnode->next = NULL;

	if (headnode->tail == NULL)//插入的第一個節點
	{
		headnode->head = headnode->tail = newnode;
	}
	else
	{
		headnode->tail->next = newnode;
		headnode->tail = newnode;
	}
	return true;
}

bool DeQueue(Queue* headnode)
{
	if (headnode == NULL || headnode->head == NULL)	return false;

	Node* p = headnode->head;

	headnode->head = p->next;
	free(p);

	if (headnode->head == NULL)//如果只有一個節點,且當這個節點被刪除後
	{
		headnode->tail = NULL;
	}
	return true;
}

ElemType Top(Queue* headnode)
{
	if (headnode == NULL || headnode->head == NULL)	return -1;

	return headnode->head->data;
}

bool Clear(Queue* headnode)
{
	if (headnode == NULL)	return false;
	Node* p = headnode->head;
	Node* q;
	while (p != NULL)
	{
		q = p->next;
		free(p);
		p = q;
	}
	headnode->head = headnode->tail = NULL;
	return true;
}

main.cpp

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include"LinkQueue.h"

int main()
{
	Queue headnode;
	InitQueue(&headnode);

	for (int i = 0; i < 10; ++i)
	{
		EnQueue(&headnode, i);
	}

	for (int i = 0; i < 4; ++i)
	{
		printf("%d\t", Top(&headnode));
		DeQueue(&headnode);
	}

	Clear(&headnode);
	printf("\n%d", Top(&headnode));


	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章