顺序表、链表、栈和队列、二叉树练习

顺序表Linux打印现象:

顺序表代码:

//							顺序表
#define N 6
#define random srand((unsigned)time(NULL))
typedef int datatype;
typedef struct
{
	datatype arr[N];
	int pos;
}seq;


seq* initial(void);
void insert(seq *p0);
void deleteseq(seq *p0);
void removalseq(seq *p0);
void delespecify(seq *p0,int n);

int main()
{
	seq* p0 = NULL;
	p0 = initial();

	insert(p0);

	deleteseq(p0);

	removalseq(p0);

	free(p0);
	p0 = NULL;
	return 0;
}



seq* initial()
{
	seq* p = (seq*)malloc(sizeof(seq));
	if (p == NULL)return NULL;

	p->pos = 0;
	memset(p->arr, 0, sizeof(p->arr));


	//printf("%d",p->arr[3]);//检查是否全置零
	return p;
}


void insert(seq* p0)
{
	random;
	if (p0->pos < N && p0->pos >= 0)
	{
		for (int i = 0; i < N; i++)
		{
			p0->arr[i] = (unsigned int)rand() % 10;
			p0->pos++;
		}
		printf("添加数据如下:\n");
		for (int j = 0; j < p0->pos; j++)
		{
			printf("%d  ", p0->arr[j]);
		}
		printf("pos = %d\n", p0->pos);
	}
	return;
}

void deleteseq(seq* p0)
{
	if (p0->pos <= N && p0->pos > 0)
	{
		p0->pos--;
		datatype storage = p0->pos;

		printf("删除数据:%d,现有数据如下:\n", p0->arr[storage]);
		for (int i = 0; i < p0->pos; i++)
		{
			printf("%d  ", p0->arr[i]);
		}
		printf("pos = %d\n", p0->pos);

		p0->arr[storage] = 0;
	}
	else printf("没有数据可以删除!\n");

	return;
}

void removalseq(seq* p0)
{
	for (int i = 0; i < p0->pos; i++)
	{
		for (int j = i+1; j < p0->pos; j++)
		{
			if (p0->arr[i] == p0->arr[j])
			{
				delespecify(p0,j);
			}
		}
	}

	printf("去重结果如下:\n");
	for (int k = 0; k < p0->pos; k++)
	{
		printf("%d  ", p0->arr[k]);
	}
	printf("pos = %d\n", p0->pos);

	return;
}


void delespecify(seq* p0,int n)
{
	if (p0->pos <= N && p0->pos > 0)
	{
		for (int i = n; i < p0->pos; i++)
		{
			p0->arr[i] = p0->arr[i + 1];
		}

		p0->arr[p0->pos] = 0;
		p0->pos--;
	}
	return;
}

链表Linux打印现象:

双向链表代码:

//							双向链表

#define N 3//加3次
#define random srand((unsigned)time(NULL))

typedef int datatype;
typedef struct Link
{
	int L;
	datatype num;
	struct Link* next;
	struct Link* pre;
}link;

link *creatlink(void);
void insertlink(link *p0);
void showlink(link *p0);
void deletelink(link *p0);

int main()
{
	random;
	link* p0 = NULL;
	p0 = creatlink();

	for(int i = 0; i < N; i++)
	{
		insertlink(p0);
	}
	showlink(p0);

	deletelink(p0);
	showlink(p0);

	return 0;
}

link* creatlink(void)
{
	link* head = (link*)malloc(sizeof(link));
	if (head == NULL) return NULL;

	head->L = 0;
	head->next = head;
	head->pre = head;

	return head;
}

void insertlink(link* p0)
{
	link* p1 = p0, * o = (link*)malloc(sizeof(link));
	if (o == NULL) return NULL;

	o->num = (unsigned int)rand() % 10;
	o->next = p1->next;
	p1->next = o;
	
	o->pre = p0;
	if (o->next != NULL)
	{
		o->next->pre = o;
	}

	p0->L++;
	return;
}

void showlink(link* p0)
{
	link* p1 = p0->next;
	printf("正向:\n");
	for (; p1 != p0;)
	{
		printf("%d  ",p1->num);
		p1 = p1->next;
	}
	putchar(10);

	link* p2 = p0->pre;
	printf("逆向:\n");
	for (; p2 != p0;)
	{
		printf("%d  ", p2->num);
		p2 = p2->pre;
	}
	printf("表长:%d\n",p0->L);
	putchar(10);

	return;
}

void deletelink(link* p0)
{
	if (p0->L > 0)
	{
		link* p = p0,*storage1 = NULL;
		storage1 = p->next;
		p->next = storage1->next;//头删
		printf("删除数据:%d\n",storage1->num);

		storage1->next->pre =p;

		p0->L--;
		free(storage1);
		storage1 = NULL;
	}
	return;
}

链式栈打印现象:

代码:

//						链 式 栈
#define N 6//容量
#define n  3//数量
#define random srand((unsigned)time(NULL))

typedef int datatype;
typedef struct Stack
{
	datatype arr[N];
	int pos;
	struct Stack* next;
}st;

st *creatstack(void);
void insertstack(st *p0);
void showstack(st *p0);
void Tdeletest(st *p0);

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<主函数
int main()
{
	random;
	st* p0 = NULL;
	p0 = creatstack();

	insertstack(p0);
	showstack(p0);

	Tdeletest(p0);
	showstack(p0);

	return 0;
}

st* creatstack(void)
{
	st* head = (st*)malloc(sizeof(st));
	if (head == NULL) return NULL;

	head->pos = 0;
	head->next = NULL;
	memset(head->arr, 0, sizeof(head->arr));

	return head;
}

void insertstack(st* p0)
{
	st* p = p0;

	p->next = NULL;
	for (int i = 0; i < n; i++)
	{
		st* o = (st*)malloc(sizeof(st));
		if (o == NULL) return NULL;
		for (int j = 0; j < N; j++)
		{
			o->arr[j] = (unsigned int)rand() % 10;
		}

		o->next = p->next;
		p->next = o;

		p0->pos++;
	}

	return;
}

void showstack(st* p0)
{
	putchar(10);
	st* p = p0->next;
	printf("现有数据如下:\n");
	while (p != NULL)
	{
		for (int i = 0; i < N; i++)
		{
			printf("%d  ",p->arr[i]);
		}
		p = p->next;
		putchar(10);
	}
	return;
}

void Tdeletest(st* p0)
{
	if (p0->pos > 0)
	{
		st* p = p0->next,*storage = NULL;
		while (p->next->next != NULL)
		{
			p = p->next;
		}
		storage = p->next;
		p->next = NULL;
		printf("删除数组:\n");
		for (int i = 0; i < N; i++)
		{
			printf("%d  ", storage->arr[i]);
		}
		p0->pos--;
		free(storage);
		storage = NULL;
	}
	return;
}

链式队列打印现象:

代码:

//							链 式 队 列
#define N 6
#define random srand((unsigned)time(NULL))
typedef int datatype;
typedef struct Linkue
{
	union
	{
		int len;
		datatype data;
	}un;
	struct Linkue* next;
}linkue;

typedef struct
{
	linkue* front;
	linkue* rear;
}pointue;

pointue* creatue(void);
void Tinsertue(pointue *o);
pointue *judgmentempty(pointue *o);
void outue(pointue *o);
void showueue(pointue *o);

int main()
{
	random;
	pointue* o = NULL;
	o = creatue();

	for(int i = 0; i < N; i++)
	{
		printf("%d)  ",i);
		Tinsertue(o);
	}

	showueue(o);

	for (int i = 0; i < N; i++)
	{
		printf("%d)  ", i);
		outue(o);
	}
	outue(o);
	return 0;
}

pointue* creatue(void)
{
	pointue* p0 = (pointue*)malloc(sizeof(pointue));
	if (p0 == NULL) return NULL;

	p0->front = (linkue*)malloc(sizeof(linkue));
	if (p0->front == NULL) return NULL;

	
	p0->front->un.len = 0;
	p0->front->next = NULL;
	p0->rear = p0->front;

	return p0;
}

void Tinsertue(pointue* o)
{
	linkue* p = (linkue*)malloc(sizeof(linkue));
	if (p == NULL)return NULL;

	p->un.data = (unsigned int)rand() % 10;
	printf("%d入队\n",p->un.data);

	p->next = o->rear->next;
	o->rear->next = p;
	o->rear = p;

	o->front->un.len++;
	return;
}

pointue* judgmentempty(pointue* o)
{
	return o->front == o->rear?1:0;
}

void outue(pointue* o)
{
	if (judgmentempty(o)) {
		printf("空的!\n"); return (datatype)-1;
	}

	linkue* p = o->front->next;
	o->front->next = p->next;
	printf("%d出队\n",p->un.data);

	free(p);
	p = NULL;

	if (o->front->next == NULL)
	{
		o->rear = o->front;
	}

	o->front->un.len--;
	return;
}

void showueue(pointue* o)
{
	pointue* u = o->front;
	printf("排队  ");
	while (o->front != o->rear)
	{
		printf("%d ",o->front->next->un.data);
		o->front = o->front->next;
	}
	o->front = u;

	putchar(10);
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值