数据结构记录

线性表的顺序存储结构

***单纯作为记录过程
#include <stdio.h>
#include <stdbool.h>
#define SIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct
{
	Elemtype data[SIZE];
	int length;
}Node;
//使用前必须得将length这个记录数据长度的值初始化为零
void init(Node* p)
{
	for (int i = 0; i < SIZE; i++)
	{
		p->data[i] = 0;
	}
	p->length = 0;
	return;
}
bool listempty(Node* p)
{
	if (p->length == 0)
		return true;
	else
		return false;
}
void Clearlist(Node* p)
{
	p->length = 0;
	return 0;
}
Status GetElem(Node* p,int i)
{
	if (i<1 || i>p->length)
		return exit(-1);
	return p->data[i - 1];
}
void Insert(Node* p, int i, Elemtype e)
{
	if (p->length == SIZE)
		return;
	if (i<1 || i>p->length + 1)
		return;
	if (i <= p->length)
	{
		Elemtype data;
		for (int j = p->length - 1; j >= i; j--)
		{
			p->data[j + 1] = p->data[j];
		}
	}
	p->data[i - 1] = e;
	p->length++;
	return;
}
Status Delete(Node* p, int i)
{
	if (i<1 || i>p->length)
	{
		return exit(-1);
	}
	Elemtype data = p->data[i - 1];
	if (i < p->length)
	{
		
		for (int j = i - 1; j < p->length; j++)
		{
			p->data[j] = p->data[j + 1];
		}
	}
	p->length--;
	return data;
}
int main()
{
	Node p;
	init(&p);
	for (int i = 1; i <= SIZE; i++)
	{
		Insert(&p, i, i);
	}
	printf("%d\n", p.length);
	for (int i = 1; i <= SIZE; i++)
	{
		printf("%d ", GetElem(&p, i));
	}
	putchar('\n');
	for (int i = SIZE; i >= 1; i--)
	{
		printf("%d ", Delete(&p, i));
	}
	printf("\n%d", p.length);
	return 0;
}
//反正很简单







## 链表

```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int Status;
typedef int Elemtype;
typedef struct
{
	Elemtype data;
	struct Node* next;
}Node, * Sqlist;
void init(Sqlist p)
{
	if (!p)
		return;
	p->data = -1;
	p->next = NULL;
	return;
}
int length(Sqlist p)
{
	int j = 0;
	Sqlist s1 = p->next;
	while (s1)
	{
		++j;
		s1 = s1->next;
	}
	return j;
}
void Creat_1(Sqlist p, int n)
{
	if (p->next)
		return;
	Sqlist s1, s2 = p;
	srand((unsigned)time(0));
	for (int i = 0; i < n; i++)
	{
		s1 = (Sqlist)malloc(sizeof(Node));
		if (!s1)
			return;
		s1->data = i;//rand() % 100 + 1;
		s1->next = s2->next;
		s2->next = s1;
	}
}
void Creat_2(Sqlist p, int n)
{
	if (p->next)
		return;
	Sqlist s1, s2 = p;
	srand((unsigned)time(0));
	for (int i = 0; i < n; i++)
	{
		s1 = (Sqlist)malloc(sizeof(Node));
		if (!s1)
			return;
		s1->data = i;
		s2->next = s1;
		s2 = s1;
	}
	s2->next = NULL;
	return;
}
Status GetElem(Sqlist p, int i)
{
	int j = 1;
	Sqlist s1;
	s1 = p->next;
	while (s1 && j < i)
	{
		s1 = s1->next;
		++j;
	}
	if (!s1 || j > i)
		return -1;
	return s1->data;
}
void Insert(Sqlist p, int i, Elemtype e)
{
	int j = 1;
	Sqlist s1 = p, s2;
	while (s1 && j < i)
	{
		s1 = s1->next;
		++j;
	}
	if (!s1 || j > i)
		return;
	s2 = (Sqlist)malloc(sizeof(Node));
	if (!s2)
		return;
	s2->data = e;
	s1->next = s2->next;
	s1->next = s2;
	return;
}
void Delete(Sqlist p, int i)
{
	int j = 1;
	Sqlist s1 = p, s2;
	while (s1->next && j < i)
	{
		++j;
		s1 = s1->next;
	}
	if (!s1->next || j > i)
		return;
	s2 = s1->next;
	s1->next = s2->next;
	free(s2);
	return;
}
void clean(Sqlist p)
{
	Sqlist s1, s2;
	s1 = p->next;
	while (s1)
	{
		s2 = s1->next;
		printf("%d ", s1->data);
		free(s1);
		s1 = s2;
	}
	p->next = NULL;
	return;
}
int main()
{
	Node p1, p2;
	init(&p1);
	init(&p2);
	Creat_1(&p1, 20);
	Creat_2(&p2, 20);
	printf("%d\n", length(&p1));
	printf("%d\n", length(&p2));
	for (int i = 1; i <= 20; i++)
	{
		printf("%d ", GetElem(&p1, i));
	}
	putchar('\n');
	for (int i = 1; i <= 20; i++)
	{
		printf("%d ", GetElem(&p1, i));
	}
	Delete(&p1, 5);
	Delete(&p1, 15);
	Delete(&p2, 5);
	Delete(&p2, 19);
	Delete(&p2, 19);
	putchar('\n');
	clean(&p1);
	putchar('\n');
	clean(&p2);
	return 0;
}

总结:每次用指针过渡存储数据都要检测此指针是否是空指针
不然会错的非常离谱
还有和c++不同的是 struct里面嵌套struct必须加struct申明。。

简单的栈的顺序存储结构实现
就设个top(表头指针)表示位置

#include <stdio.h>
#define SIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct
{
	Elemtype data[SIZE];
	int top;
}Stack;
void init(Stack* p)
{
	p->top = 0;
	return;
}
void Push(Stack* p, Elemtype e)
{	
	++p->top;
	p->data[p->top-1] = e;
	return;
}
Status Pop(Stack* p)
{
	--p->top;
	return p->data[p->top];
}
void Clear(Stack* p)
{
	p->top = 0;
	return;
}
int main()
{
	Stack p;
	init(&p);
	for (int i = 0; i < 20; i++)
	{
		Push(&p, i);
	}
	printf("\n%d\n", p.top);
	
	for (int i = 0; i < 15; i++)
	{
		printf("%d ", Pop(&p));
	}
	printf("\n%d\n", p.top);
	return 0;
}

栈的链式存储结构的实现
要加一个头指针和一个尾指针来表示位置防止越界
还有要注意将第一个Push进去的元素滴next指针指向NULL,
就是检查计数cout的值,如计数为0插入则将插入的元素next指向NULL。

#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef int Elemtype;
typedef struct
{
	Elemtype data;
	struct Stack* next;
}Stack;
typedef struct
{
	Stack* top;
	int cout;
}Link;
void init(Link *p)
{
	p->cout = 0;
}
void Push(Link* p, Elemtype e)
{
	Stack* s = (Stack*)malloc(sizeof(Stack));
	if (!s)
		return;
	s->data = e;
	s->next = p->top;
	p->top = s;
	if (!p->cout)
		p->top->next = NULL;
	++p->cout;
}
Status Pop(Link* p)
{
	Stack* s;
	if (!p->top)
		return;
	Elemtype data = p->top->data;
	s = p->top;
	p->top = p->top->next;
	free(s);
	--p->cout;
	return data;
}
void Clear(Link* p)
{
	Stack* s1 = p->top;
	Stack* s2;
	while (s1)
	{
		s2 = s1->next;
		printf("%d ", s1->data);
		free(s1);
		s1 = s2;
	}
	p->top = s1;
	p->cout = 0;
}
int main()
{
	Link p;
	init(&p);
	for (int i = 0; i < 20; i++)
	{
		Push(&p, i);
	}
	for (int i = 0; i < 15; i++)
	{
		printf("%d\n", Pop(&p));
	}
	putchar('\n');
	Clear(&p);
	return 0;
}

俩栈共享空间

#include <stdio.h>
#define MAXSIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct
{
	Elemtype data[MAXSIZE];
	int top1;
	int top2;
	int cout;
}Stack;
void init(Stack* p)
{
	p->cout = 0;
	p->top1 = 0;
	p->top2 = MAXSIZE;
	return;
}
void Push(Stack* p,Elemtype e,int select)
{
	if (p->cout == MAXSIZE)
		return;
	if (select != 1 && select != 2)
	{
		perror("选择错误");
		return;
	}
	if (select == 1)
	{
		p->data[p->top1] = e;
		p->top1++;
		p->cout++;
		return;
	}
	if (select == 2)
	{
		p->data[p->top2-1] = e;
		p->top2--;
		p->cout++;
		return;
	}
}
Status Pop(Stack* p, int select)
{
	if (!p->cout)
		return;
	if (select != 1 && select != 2)
	{
		perror("select of Error!");
		return;
	}
	if (select == 1)
	{
		Elemtype data = p->data[p->top1 - 1];
		p->cout--;
		p->top1--;
		return data;
	}
	if (select == 2)
	{
		Elemtype data = p->data[p->top2-1];
		p->cout--;
		p->top1++;
		return data;
	}
}
void Clear(Stack* p)
{
	for (int i = 0; i < p->top1;i++)
	{
		printf("%d ", p->data[i]);
	}
	for (int i = p->top2; i <MAXSIZE; i++)
	{
		printf("%d ", p->data[i]);
	}
}
int main()
{
	Stack p;
	Stack q;
	init(&q);
	Push(&q, 10, 3);
	Push(&q, 199, 1);
	Pop(&q, 4);
	init(&p);
	for (int i = 0; i < 10; i++)
	{
		Push(&p, i, 1);
	}
	for (int i= MAXSIZE-1; i>=10; i--)
	{
		Push(&p, i , 2);
	}
	Clear(&p);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值