数组与链表和队列基本内容

线性表(数组) 通过下标查找方便
链表 空间分配灵活、插入删除操作方便 (重点!!!记下来)

在这里插入图片描述

使用指针与数字完成链表内容

输入数字组成链表

#include<iostream> 
#include<cstdio> 
#include<cstring> 
using namespace std;
typedef struct node{
	int data;
	int next;
}node;
node Lis[105]={0};
int main() {
	int n,h=0,t=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		
		Lis[x].data=x;
        Lis[x].next=-1;
        
		Lis[t].next=x;//基本操作
		t=x;
	}
	int p=Lis[h].next;
	while(p!=-1){
		cout<<Lis[p].data<<" ";
		p=Lis[p].next;
	}
	return 0;
}

输入其他组成链表

#include<iostream> 
#include<cstdio> 
#include<cstring> 
using namespace std;
typedef struct node{
	char data;
	int next;
}node;
node Lis[105]={0};
int main() {
	int n,h=0,t=0;
    Lis[h].next=-1;
	cin>>n;
	for(int i=1;i<=n;i++){
		char x;
		cin>>x;
		
		Lis[i].data=x;
		Lis[i].next=-1;
		
		Lis[t].next=i;//与上面进行区分
		t=i;
		
	}
	int p=Lis[h].next;
	while(p!=-1){
		cout<<Lis[p].data<<" ";
		p=Lis[p].next;
	}
	return 0;
}

双链表模拟

#include<iostream> 
#include<cstdio> 
#include<cstring> 
using namespace std;
typedef struct node{
	int data;
	int next;
	int pre;
}node;
node Lis[105]={0};
int main() {
	int n;
	int h=0,t=0;
	Lis[h].pre=Lis[h].next=-1;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		
		Lis[x].data=x;
		Lis[x].pre=Lis[x].next=-1;
		
		Lis[t].next=x;
		Lis[x].pre=t;
		
		t=Lis[t].next;//t=x;
	}
	
	int p=Lis[h].next;
	while(p!=-1){
		cout<<Lis[p].data<<" ";
		p=Lis[p].next;
	}
	cout<<endl;
	
	p=t;
	while(p!=h){
		cout<<Lis[p].data<<" ";
		p=Lis[p].pre;
	}
	return 0;
}

队列
queue

先进先出

push()入队

emoty() 判断队列空 为空返回true 不为空返回false

front() 队首元素

pop() 出队

#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include <queue>
using namespace std;
int main() {
	queue <int> Q;//创建队列方式
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		Q.push(x);//入队
	}
	
	while(!Q.empty()){//同上
		int t=Q.front();//t为队列最前的
		cout<<t<<" ";
		Q.pop();//出队
	}
	return 0;
}
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值