To my CSDN

数据结构基础(线性表与栈与队列)


定长顺序表 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;
}


posted @ 2019-11-09 02:20  我叫RT  阅读(213)  评论(0编辑  收藏  举报