考研打卡链表,栈,队列

1:链表

点击查看代码
#include <bits/stdc++.h>

using namespace std;
typedef struct node{
	int data;
	struct node *next;
}listnode,*list1;
typedef struct now{
	int data;
	struct now *next,*prve;
}listnow,*list2;
bool listn(list1 &L,int x,int y)
{
	if(x<1)
		return false;
	list1 p;
	int j = 0;
	p = L;
	while(p!=NULL&&j < x-1)
	{
		p = p->next;
		j ++;
	}
	cout << j << '\n';
	if(p==NULL)
	return false;
	list1 s = (node*)malloc(sizeof(node));
	s->data = y;
	s->next = p->next;
	p->next = s;
	return true;
}

bool delete1(list1 &L,int x)
{
	if(x < 1)
	return false;
	list1 p;
	int j = 0;
	p = L;
	while(p!=NULL&&j < x)
	{
		//cout << p->data<<'\n';
		p = p->next;
		j ++;
	}
	if(p==NULL)
	return false;
	if(p->next==NULL)
	return false;
	list1 q = p->next;
	p->next = q->next;
	p->data = q->data;
	//free(q);
}
bool cycle(list2 &L,int x,int y)
{
	if(x < 1)
	return false;
	list2 p;
	int j = 0;
	p = L;
	while(p!=NULL&&j < x-1)
	{
		p = p->next;
		j++;
	}
	if(p == NULL)
	return false;
	list2 s;
	s = (now*)malloc(sizeof(now));
	s->data = y;
	if(p->next == NULL)
	{
		s->next=p->next;
		s->prve = p;
		p->next = s;
	}
	else
	{
		s->next = p->next;
		p->next->prve = s;
		s->prve = p;
		p->next = s;	
	}
	return true;
}
bool delete2(list2 &L,int x)
{
	if(x < 1)
	return false;
	list2 p;
	int j = 0;
	p = L;
	while(p!=NULL&&j < x)
	{
		p = p->next;
		j++;
	}
	if(p == NULL)
	return false;
	if(p->next == NULL)
	return false;
	list2 s = p->next;
	p->next = s->next;
	s->next->prve =p;
	free(s); 
}
void lisnode(list1 &L)
{
	L = (node*)malloc(sizeof(node));
	L->next = NULL;
	L->data = 0;
}
void lisnow(list2 &L)
{
	L = (now*)malloc(sizeof(now));
	L->data = 1;
	L->next = NULL;
	L->prve = NULL;
}
int main()
{
	list1 L;
	lisnode(L);
	// list2 L;
	// lisnow(L);
	int n;
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
		int a,b;
		cin >> a >> b;
		listn(L,a,b);
	}
	while(L!=NULL)
	{
		cout << L->data << '\n';
		L = L->next;
		//L = L->prve;
	}
// 	
	// delete1(L,2);
	//cout << L->data << '\n';
	// for(int i = 1;i <= n;i ++)
	// {
		// int a,b;
		// cin >> a >> b;
		// cycle(L,a,b);
	// }
	// while(L!=NULL)
	// {
		// cout << L->data << '\n';
		// L = L->next;
		// //L = L->prve;
	// }
}

2:链栈

点击查看代码
#include <bits/stdc++.h>

using namespace std;

typedef struct node{
	int data;
	struct node *next,*prve;
}listnow,*list2;

void inputstack(list2 &L,int x)
{
	list2 s = (node*)malloc(sizeof(node));
	s->data = x;
	list2 r = L;
	s->next = r->next;
	s->prve = r;
	r->next = s;
	r = s;
}
bool outstack(list2 &L)
{
	list2 r = L;
	
	list2 s = (node*)malloc(sizeof(node));
	s = r->next;
	cout << s->data << '\n';
	r->next = s->next;
	if(s->next!=NULL)
	s->next->prve = r;
	free(s);
}
bool ifempty(list2 &L)
{
	if(L->next == NULL)
	{
		cout << "yes" << '\n';
		return true;
	}
	cout << "no" << '\n';
	return false;
}
int main()
{
	list2 L = (node*)malloc(sizeof(node));
	L->data = -1;
	L->next = NULL;
	L->prve = NULL;
	int n;
	cin >> n;
	
	for(int i = 1;i <= n;i ++)
	{
		int x;
		cin >> x;
		inputstack(L,x);
	}
	while(L->next!=NULL)
	{
		outstack(L);
		ifempty(L);
	}
}

3:链队列

点击查看代码
#include <bits/stdc++.h>

using namespace std;

typedef struct node{
	int data;
	struct node *next;
}linknode,*linknode1;
typedef struct{
	linknode *front,*rear;
}linkqueue;

void inputqueue(linkqueue &q,int x)
{
	linknode1 s = (node*)malloc(sizeof(node));
	s->data = x;
	s->next = NULL;
	q.rear->next = s;
	q.rear = s;
}

bool outqueue(linkqueue &q)
{
	if(q.front == q.rear)
		return false;
	linknode1 s = q.front->next;
	cout << s->data << '\n';
	q.front->next = s->next;
	if(q.rear == s)
		q.rear = q.front;
	free(s);
}
int main()
{
	linkqueue q;
	q.front = q.rear = (node*)malloc(sizeof(node));
	q.front->next = NULL;
	
	int n;
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
		int x;
		cin >> x;
		inputqueue(q,x);
	}
	while(q.rear!=q.front)
	outqueue(q);
}
posted @ 2024-05-03 17:08  清初  阅读(17)  评论(0)    收藏  举报