数据结构-双向链表——栈和队列

(1)双向链表

【1】头插法

#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Node
{
	ElemType data;
	Node *prev,*next;
};
Node* initList(){
	Node* head=new Node;
	head->data=0;
	head->next=NULL;
	return head;
}
int insertHead(Node *L,ElemType e){
	Node *p=new Node;
	p->data=e;
	p->prev=L;
	p->next=L->next;
	if(L->next!=NULL){
		
		L->next->prev=p;
	}
	L->next=p;
	return true;
}
void listNode(Node* L)
{	
	Node *p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int main()
{
	Node* list=initList();
	insertHead(list,10);
	insertHead(list,20);
	insertHead(list,30);//头插法 
	listNode(list);
}

【2】尾插法

#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Node
{
	ElemType data;
	Node *prev,*next;
};
Node* initList(){
	Node* head=new Node;
	head->data=0;
	head->next=NULL;
	return head;
}
int insertHead(Node *L,ElemType e){
	Node *p=new Node;
	p->data=e;
	p->prev=L;
	p->next=L->next;
	if(L->next!=NULL){
		
		L->next->prev=p;
	}
	L->next=p;
	return true;
}
Node* get_tail(Node*L){
	Node *p=L;
	while(p->next!=NULL){
		p=p->next;
	}
	return p;
}
Node* insertTail(Node *tail,ElemType e)
{
	Node *p=new Node;
	p->data=e;
	p->prev=tail;
	tail->next=p;
	p->next=NULL;
	return p;
}
void listNode(Node* L)
{	
	Node *p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int main()
{
	Node* list=initList();
	Node* tail=get_tail(list);//头插法 
	tail=insertTail(tail,40);
	tail=insertTail(tail,30);//尾插法 
	tail=insertTail(tail,20);
	tail=insertTail(tail,10);
	listNode(list);
}

【3】在指定位置插入数据

#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Node
{
	ElemType data;
	Node *prev,*next;
};
Node* initList(){
	Node* head=new Node;
	head->data=0;
	head->next=NULL;
	return head;
}
int insertHead(Node *L,ElemType e){
	Node *p=new Node;
	p->data=e;
	p->prev=L;
	p->next=L->next;
	if(L->next!=NULL){
		
		L->next->prev=p;
	}
	L->next=p;
	return true;
}
Node* get_tail(Node*L){
	Node *p=L;
	while(p->next!=NULL){
		p=p->next;
	}
	return p;
}
Node* insertTail(Node *tail,ElemType e)
{
	Node *p=new Node;
	p->data=e;
	p->prev=tail;
	tail->next=p;
	p->next=NULL;
	return p;
}
void listNode(Node* L)
{	
	Node *p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int insertNode(Node *L,int pos,ElemType e)
{
	Node *p=L;
	int i=0;
	while(i<pos-1){
		p=p->next;
		i++;
		if(p==NULL){
			return 0;
		}
	} 
	Node *q=new Node;
	q->data=e;
	q->prev=p;
	q->next=p->next;
	p->next=q;
	p->next->prev=q;
	return true;
}
int main()
{
	Node* list=initList();
	Node* tail=get_tail(list);//头插法 
	tail=insertTail(tail,40);
	tail=insertTail(tail,30);//尾插法 
	tail=insertTail(tail,20);
	tail=insertTail(tail,10);
	insertNode(list,2,55);//在指定位置插入数据 
	listNode(list);
}

【4】删除节点

#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Node
{
	ElemType data;
	Node *prev,*next;
};
Node* initList(){
	Node* head=new Node;
	head->data=0;
	head->next=NULL;
	return head;
}
int insertHead(Node *L,ElemType e){
	Node *p=new Node;
	p->data=e;
	p->prev=L;
	p->next=L->next;
	if(L->next!=NULL){
		
		L->next->prev=p;
	}
	L->next=p;
	return true;
}
Node* get_tail(Node*L){
	Node *p=L;
	while(p->next!=NULL){
		p=p->next;
	}
	return p;
}
Node* insertTail(Node *tail,ElemType e)
{
	Node *p=new Node;
	p->data=e;
	p->prev=tail;
	tail->next=p;
	p->next=NULL;
	return p;
}
void listNode(Node* L)
{	
	Node *p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int insertNode(Node *L,int pos,ElemType e)
{
	Node *p=L;
	int i=0;
	while(i<pos-1){
		p=p->next;
		i++;
		if(p==NULL){
			return 0;
		}
	} 
	Node *q=new Node;
	q->data=e;
	q->prev=p;
	q->next=p->next;
	p->next=q;
	p->next->prev=q;
	return true;
}
void freeList(Node *L)
{
	Node *p=L->next;
	Node *q;
	while(p!=NULL){
		q=p->next;
		delete p;
		p=q;
	}
	L->next=NULL;	
}
int deleteNode(Node* L,int pos)
{
	Node *p=L;
	int i=0;
	while(i<pos-1){
		p=p->next;
		i++;
		if(p==NULL){
			return 0;
		}
	}
	if(p->next==NULL){
		cout<<"删除错误"<<endl;
		return 0;
	}
	Node *q=p->next;
	p->next=q->next;
	q->next->prev=p;
	delete q;
	return true;
}
int main()
{
	Node* list=initList();
	Node* tail=get_tail(list);//头插法 
	tail=insertTail(tail,40);
	tail=insertTail(tail,30);//尾插法 
	tail=insertTail(tail,20);
	tail=insertTail(tail,10);
	insertNode(list,2,55);//在指定位置插入数据 
	deleteNode(list,3);//删除指定位置的节点 
	listNode(list);
}

【5】其他的都和单项链表一样

【6】栈的顺序结构实现

#define MAXSIZE 100
#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Stack{
	ElemType data[MAXSIZE];
	int top;
};
void initStack(Stack *s){
	s->top=-1;
}
int isEmpty(Stack *s)
{
	if(s->top==-1){
		cout<<"空的"<<endl;
		return true;
	}
	else
	{
		return false;
	}
}
int push(Stack *s,ElemType e)
{
	if(s->top>=MAXSIZE-1){
		cout<<"满了"<<endl;
		return false;
	}
	s->top++;
	s->data[s->top]=e;
	return true;
}
ElemType pop(Stack *s,ElemType *e)
{
	if(s->top==-1)
	{
		cout<<"空的"<<endl;
		return false;
	}
	*e=s->data[s->top];
	s->top--;
	return true; 
}
int getTop(Stack *s,ElemType *e)
{
	if(s->top==-1){
		cout<<"空的"<<endl;
		return 0;
	} 
	*e=s->data[s->top];
	return 0;
}
int main()
{
	Stack s;
	initStack(&s);//初始化 
	push(&s,90);
	push(&s,80);
	push(&s,70);//压栈 
	ElemType e;
	pop(&s,&e);//出栈 
	cout<<e<<endl;
	getTop(&s,&e);//获得栈顶元素 
	cout<<e<<endl;
}

【7】栈的顺序结构-动态内存分配

只有初始化和结构体改变了

#define MAXSIZE 100
#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Stack{
	ElemType *data;
	int top;
};
Stack* initStack(){
	Stack *s=new Stack;
	s->data=new ElemType[MAXSIZE];
	s->top=-1;
	return s;
}
int isEmpty(Stack *s)
{
	if(s->top==-1){
		cout<<"空的"<<endl;
		return true;
	}
	else
	{
		return false;
	}
}
int push(Stack *s,ElemType e)
{
	if(s->top>=MAXSIZE-1){
		cout<<"满了"<<endl;
		return false;
	}
	s->top++;
	s->data[s->top]=e;
	return true;
}
ElemType pop(Stack *s,ElemType *e)
{
	if(s->top==-1)
	{
		cout<<"空的"<<endl;
		return false;
	}
	*e=s->data[s->top];
	s->top--;
	return true; 
}
int getTop(Stack *s,ElemType *e)
{
	if(s->top==-1){
		cout<<"空的"<<endl;
		return 0;
	} 
	*e=s->data[s->top];
	return 0;
}
int main()
{
	Stack *s=initStack();//初始化 
	push(s,90);
	push(s,80);
	push(s,70);//压栈 
	ElemType e;
	pop(s,&e);//出栈 
	cout<<e<<endl;
	getTop(s,&e);//获得栈顶元素 
	cout<<e<<endl;
}

【8】栈的链式结构

#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Stack{
	ElemType data;
	Stack *next; 
};
Stack* initStack()
{
	Stack *s=new Stack;
	s->data=0;
	s->next=NULL;
	return s;
}
int isEmpty(Stack *s)
{
	if(s->next==NULL)
	{
		cout<<"空的"<<endl;
		return 1;
	}
	else
	{
		return 0;
	}
}
int push(Stack *s,ElemType e)
{
	Stack *p=new Stack;
	p->data=e;
	p->next=s->next;
	s->next=p;
	return true;
}
int pop(Stack *s,ElemType *e)
{
	if(s->next==NULL)
	{
		cout<<"空的"<<endl;
		return 0;
	}
	*e=s->next->data;
	Stack *q=s->next;
	s->next=q->next;
	delete q;
	return true;
}
int getTop(Stack *s,ElemType *e)
{
	if(s->next==NULL)
	{
		cout<<"空的"<<endl;
		return 0;
	}
	*e=s->next->data;
	return 1; 
}
int main()
{
	Stack *s=initStack();
	push(s,10);
	push(s,20);
	push(s,30);
	ElemType e;
	pop(s,&e);
	cout<<e<<endl;
	getTop(s,&e);
	cout<<e<<endl;
	
}

【9】队列顺序结构

(1)栈内存中的形式

#define MAXSIZE 100
#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Queue{
	ElemType data[MAXSIZE];
	int front;
	int rear;
};
void initQueue(Queue *Q){
	Q->front=0;
	Q->rear=0;
}
int isEmpty(Queue *Q){
	if(Q->front==Q->rear){
		cout<<"空的"<<endl;;
		return true;
	}
	else
	{
		return false;
	}
}
ElemType dequeue(Queue *Q)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0;
	}
	ElemType e=Q->data[Q->front];
	Q->front++;
	return e;
}
int queueFull(Queue *Q)
{
	if(Q->front>0)
	{
		int step=Q->front;
		for(int i=Q->front;i<=Q->rear;i++)
		{
			Q->data[i-step]=Q->data[i];
		}
		Q->front=0;
		Q->rear=Q->rear-step;
		return true;
	}
	else
	{
		cout<<"真的满了"<<endl;
		return false;
	}
}
int equeue(Queue *Q,ElemType e)
{
	if(Q->rear>=MAXSIZE)
	{
		if(!queueFull(Q))
		{
			return 0;
		}
	}
	Q->data[Q->rear]=e;
	Q->rear++;
	return 1; 
}

int getHead(Queue *Q,ElemType *e)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0; 
	}
	*e=Q->data[Q->front];
	return 1;
}
int main()
{
	Queue q;
	initQueue(&q); 
	equeue(&q,10);
	equeue(&q,20);
	equeue(&q,30);
	equeue(&q,40);//队列入队 
	cout<<dequeue(&q)<<endl;
	cout<<dequeue(&q)<<endl;//出队 
	ElemType e;
	getHead(&q,&e);//获取队头元素 
	cout<<e<<endl;
}

(2)堆内存的形式

#define MAXSIZE 100
#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Queue{
	ElemType *data;
	int front;
	int rear;
};
Queue * initQueue(){
	Queue *q=new Queue;
	q->data=new ElemType[MAXSIZE];
	q->front=0;
	q->rear=0;
	return q; 
}
int isEmpty(Queue *Q){
	if(Q->front==Q->rear){
		cout<<"空的"<<endl;;
		return true;
	}
	else
	{
		return false;
	}
}
ElemType dequeue(Queue *Q)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0;
	}
	ElemType e=Q->data[Q->front];
	Q->front++;
	return e;
}
int queueFull(Queue *Q)
{
	if(Q->front>0)
	{
		int step=Q->front;
		for(int i=Q->front;i<=Q->rear;i++)
		{
			Q->data[i-step]=Q->data[i];
		}
		Q->front=0;
		Q->rear=Q->rear-step;
		return true;
	}
	else
	{
		cout<<"真的满了"<<endl;
		return false;
	}
}
int equeue(Queue *Q,ElemType e)
{
	if(Q->rear>=MAXSIZE)
	{
		if(!queueFull(Q))
		{
			return 0;
		}
	}
	Q->data[Q->rear]=e;
	Q->rear++;
	return 1; 
}

int getHead(Queue *Q,ElemType *e)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0; 
	}
	*e=Q->data[Q->front];
	return 1;
}
int main()
{
	Queue *q=initQueue();
	equeue(q,10);
	equeue(q,20);
	equeue(q,30);
	equeue(q,40);//队列入队 
	cout<<dequeue(q)<<endl;
	cout<<dequeue(q)<<endl;//出队 
	ElemType e;
	getHead(q,&e);//获取对头元素 
	cout<<e<<endl;
}

【10】循环队列(普通队列的效率太慢了,才引入循环队列)

(1)代码是存在bug的无论如何都会空出一个格,如图

alt

#define MAXSIZE 100
#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct Queue{
	ElemType *data;
	int front;
	int rear;
};
Queue * initQueue(){
	Queue *q=new Queue;
	q->data=new ElemType[MAXSIZE];
	q->front=0;
	q->rear=0;
	return q; 
}
int isEmpty(Queue *Q){
	if(Q->front==Q->rear){
		cout<<"空的"<<endl;;
		return true;
	}
	else
	{
		return false;
	}
}
ElemType dequeue(Queue *Q)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0;
	}
	ElemType e=Q->data[Q->front];
	Q->front=(Q->front+1)%MAXSIZE;
	return e;
}
int equeue(Queue *Q,ElemType e)
{
	if((Q->rear+1)%MAXSIZE==Q->front)
	{
		cout<<"满了"<<endl;
		return 0;
	}
	Q->data[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXSIZE;
	return 1; 
}

int getHead(Queue *Q,ElemType *e)
{
	if(Q->front==Q->rear)
	{
		cout<<"空的"<<endl;
		return 0; 
	}
	*e=Q->data[Q->front];
	return 1;
}
int main()
{
	Queue *q=initQueue();
	equeue(q,10);
	equeue(q,20);
	equeue(q,30);
	equeue(q,40);//队列入队 
	cout<<dequeue(q)<<endl;
	cout<<dequeue(q)<<endl;//出队 
	ElemType e;
	getHead(q,&e);//获取对头元素 
	cout<<e<<endl;
}
全部评论

相关推荐

咦哟,从去年八月份开始长跑,两处实习转正都失败了,风雨飘摇,终于拿到offer了更新一下面试记录:秋招:多部门反复面试然后挂掉然后复活,具体问了啥已经忘了,只是被反复煎炸,直至焦香😋春招:base北京抖音hr打来电话说再次复活,准备面试,gogogo北京抖音一面:六道笔试题:1.promise顺序2.定义域问题3.flat展开4.并发请求5.岛屿数量算法(力扣)深度,广度都写6.忘记了,好像也是算法,难度中等其他问题多是框架底层设计,实习项目重难点~~~秒过😇北京抖音二面:三道笔试题:(为什么只有三道是因为第三道没做出来,卡住了)1.中等难度算法(忘记啥题了,应该是个数组的)2.认识js的继承本质(手写继承模式,深入js的面相对象开发)3.手写vue的响应式(卡在了watch,导致挂掉)---后知后觉是我的注册副作用函数写得有问题,有点紧张了其他题目多是项目拷打,项目亮点,对实习项目的贡献~~~第二天,挂,but立马复活转战深圳客服当天约面深圳客服一面:六道笔试题,由于面过太多次字节,面试官叫我直接写,不用讲,快些写完😋,具体都是些继承,深拷贝(注意对数组对象分开处理,深层次对象,循环引用),加中等难度算法题~~~秒过深圳客服二面:口诉八股大战:大概囊括网络,浏览器渲染原理,动画优化,时间循环,任务队列等等(你能想到的简单八股通通拉出来鞭尸😋)算法题:笔试题6道:1:找出数组内重复的数,arr[0]-arr[n]内的数大小为[1-n],例如[1,2,2,3,3]返回[2,3],要求o(n),且不使用任何额外空间(做到了o(n),空间方面欠佳,给面试官说进入下一题,做不来了)2:原滋原味的继承(所以继承真滴很重要)3:力扣股票购买时机难度中等其他滴也忘记了,因为拿到offer后鼠鼠一下子就落地了,脑子自动过滤掉可能会攻击鼠鼠的记忆😷~~~秒过深圳客服三面:项目大战参与战斗的人员有:成员1:表单封装及其底层原理,使用成本的优化,声明式表单成员2:公司内部库生命周期管理成员3:第三方库和内部库冲突如何源码断点调试并打补丁解决成员4:埋点的艺术成员5:线上项目捷报频传如何查出内鬼成员6:大文件分片的风流趣事成员7:设计模式对对碰成员8:我构建hooks应对经理的新增的小需求的故事可能项目回答的比较流利,笔试题3道,都很简单,相信大家应该都可以手拿把掐😇~~~过过过无hr面后续煎熬等待几天直接hr打电话发offer了,希望大家也可以拿到自己心仪的offer
法力无边年:牛哇,你真是准备得充分,我对你没有嫉妒,都是实打实付出
查看19道真题和解析
点赞 评论 收藏
分享
03-28 19:11
铜陵学院 C++
有礼貌的山羊追赶太阳:太典了,连笔试都没有开始就因为HC满了而结束了,而且还卡你不让你再投其他部门的。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务