【锐格】数据结构-栈和队列_Do1phln的博客-程序员秘密

技术标签: 大学课程  1024程序员节  # 锐格  数据结构  

欢迎加入NEFU计算机编程交流群:523539085

顺序栈

4909

#include<iostream>
using namespace std;

struct Node {
    
    int data;
	Node *next;
};

class SeqStack {
    
private:
	int *array;
	int maxSize;
	int top;
	void stackFull();
public:
	SeqStack(int sz=0);
	void Push(const int &x);
	bool Pop(int &x);
	bool getTop(int &x);
	bool isEmpty() const
	{
    
		return (top==-1)?true:false;
	}
	bool isFull() const
	{
    
		return (top==maxSize-1)?true:false;
	}
	int getSize() const
	{
    
		return top+1;
	}
};

SeqStack::SeqStack(int sz):top(-1),maxSize(sz)
{
    
	array = new int[maxSize];
}

void SeqStack::Push(const int &x)
{
    
	if (this->isFull())
		this->stackFull();
	array[++top] = x;
}

bool SeqStack::Pop(int &x)
{
    
	if (this->isEmpty())
		return false;
	x = array[top--];
	return true;
}

bool SeqStack::getTop(int &x)
{
    
	if (this->isEmpty())
		return false;
	x = array[top];
	return true;
}

void SeqStack::stackFull()
{
    
	maxSize = maxSize*2;
	int *temp = new int[maxSize];
	int i;
	for (i=0;i<=top;i++)
		temp[i] = array[i];
	delete []array;
	array = temp;
}

class LinkList {
    
private:
	Node *first;
public:
	LinkList();
	void Insert(const int &x);
	int Length();
	void Reverse();
	void output();
};

LinkList::LinkList()
{
    
	first = new Node();
	first->next = NULL;
}

void LinkList::Insert(const int &x)
{
    
	Node *t = first;
	while (t->next!=NULL)
		t = t->next;
	Node *n = new Node();
	n->data = x;
	n->next = t->next;
	t->next = n;
}

int LinkList::Length()
{
    
	int count=0;
	Node *t = first;
	while (t->next!=NULL) {
    
		t = t->next;
		count++;
	}
	return count;
}

void LinkList::Reverse()
{
    
	//write your code here
    SeqStack S(Length());
    Node *p=first->next;
    int x;
    //output();
    first->next=NULL;
    //output();
    while(p!=NULL)
    {
    
        S.Push(p->data);
        p=p->next;
    }
    //output();
    while(!S.isEmpty())
    {
    
        S.Pop(x);
        Insert(x);
    }
}

void LinkList::output()
{
    
	Node *t = first;
	while (t->next!=NULL) {
    
		t=t->next;
		cout << t->data << " ";
	}
	cout << endl;
}

int main()
{
    
	LinkList l;
	int x;
	cin >> x;
	while (x!=-1) {
    
		l.Insert(x);
		cin >> x;
	}
	l.output();
	l.Reverse();
	l.output();
    return 0;
}

4908

从上一道题cv就行

#include<iostream>
using namespace std;

class SeqStack {
    
private:
    int *array;
	int maxSize;
	int top;
	void stackFull();
public:
	SeqStack(int sz=0);
	void Push(const int &x);
	bool Pop(int &x);
	bool getTop(int &x);
	bool isEmpty() const
	{
    
		return (top==-1)?true:false;
	}
	bool isFull() const
	{
    
		return (top==maxSize-1)?true:false;
	}
	int getSize() const
	{
    
		return top+1;
	}
	int getMaxSize() const
	{
    
		return maxSize;
	}
	void MakeEmpty()
	{
    
		top = -1;
	}
};

SeqStack::SeqStack(int sz):top(-1),maxSize(sz)
{
    
	//write your code here
    array=new int[maxSize];
}

void SeqStack::Push(const int &x)
{
    
	//write your code here
    if(this->isFull())
        this->stackFull();
    array[++top]=x;
}

bool SeqStack::Pop(int &x)
{
    
	//write your code here
    if(this->isEmpty())
        return false;
    x=array[top--];
    return true;
}

bool SeqStack::getTop(int &x)
{
    
	//write your code here
    if(this->isEmpty())
        return false;
    x=array[top];
    return true;
}

void SeqStack::stackFull()
{
    
    //write your code here
    maxSize*=2;
    int *tmp=new int[maxSize];
    for(int i=0;i<=top;i++)
        tmp[i]=array[i];
    delete []array;
    array=tmp;
}

int main()
{
    
    int x;
    SeqStack ss(4);
	ss.Push(1);
	ss.Push(2);
	ss.Push(3);
	ss.Push(4);
	ss.Push(5);
    ss.Pop(x);
    cout << x << endl;
	cout << ss.getSize() << endl;
	cout << ss.getMaxSize() << endl;
	return 0;
}

4873待解决

4870

#include<stdio.h>

#define maxsize 10000

typedef struct
{
    
    int data[maxsize];      //存放栈中元素
    int top;                //栈顶指针
}SqStack;

//初始化栈
void initStack(SqStack &st)
{
    
	st.top = -1;
}

//进栈算法
int Push(SqStack &st, int x)
{
    
	if(st.top == maxsize - 1)
		return 0;
	++(st.top);  //先移动指针再进栈
	st.data[st.top] = x;
	return 1;
}

//出栈算法
int Pop(SqStack &st , int &x)
{
    
	if(st.top == -1)
		return 0;
	x = st.data[st.top];
	--(st.top);
	return 1;
}

void isEmpty(SqStack st)
{
    
 //write your own codes
 if(st.top==-1)
    printf("Stack is empty\n");
 else
    printf("Stack is not empty\n");
}

int main()
{
    
	int n , i , e;
	SqStack S;
	initStack(S);
	isEmpty(S);              //需要写的函数
	scanf("%d",&n);
	for(i = 0 ; i < n; ++i)
	{
    
		scanf("%d",&e);
		Push(S,e);
	}
	isEmpty(S);
	scanf("%d",&n);
	for(int i = 0 ;i < n ; ++i)
		Pop(S,e);
	isEmpty(S);
	return 0;
}

4869

cv大法好

#include<stdio.h>

#define maxsize 10000

typedef struct
{
    
    int data[maxsize];      //存放栈中元素
	int top;                //栈顶指针
}SqStack;

//初始化栈
void initStack(SqStack &st)
{
    
	st.top = -1;
}

//进栈算法
int Push(SqStack &st, int x)
{
    
	if(st.top == maxsize - 1)
		return 0;
	++(st.top);  //先移动指针再进栈
	st.data[st.top] = x;
	return 1;
}

//出栈算法
int Pop(SqStack &st , int &x)
{
    
	if(st.top == -1)
		return 0;
	x = st.data[st.top];
	--(st.top);
	return 1;
}

bool isEmpty(SqStack st)
{
    
 //write your own codes
 if(st.top==-1)
    return true;
 return false;
}

//输出栈中的元素
int print_S(SqStack st)
{
    
	if( isEmpty(st) )
	{
    
		printf("Stack is Empty");
		return 0;
	}
	int iPointer = st.top;
	while(iPointer != -1)
	{
    
		printf("%d ",st.data[iPointer]);
		--iPointer;
	}
	printf("\n");
	return 1;
}

int main()
{
    
	int n , i , e;
	SqStack S;
	initStack(S);
	scanf("%d",&n);
	for(i = 0 ; i < n; ++i)
	{
    
		scanf("%d",&e);
		Push(S,e);                          //要写的函数:进栈操作
	}
	print_S(S);
	scanf("%d",&n);
	for(int i = 0 ;i < n ; ++i)
	{
    
		Pop(S,e);                          //要写的函数:出栈操作
		printf("%d ",e);
	}
	printf("\n");
	print_S(S);
	return 0;
}

4868

cvcv

#include<stdio.h>

#define maxsize 10000

typedef struct
{
    
    int data[maxsize];      //存放栈中元素
	int top;                //栈顶指针
}SqStack;

//初始化栈
void initStack(SqStack &st)
{
    
	st.top = -1;
}

//进栈算法
int Push(SqStack &st, int x)
{
    
	if(st.top == maxsize - 1)
		return 0;
	++(st.top);  //先移动指针再进栈
	st.data[st.top] = x;
	return 1;
}

bool isEmpty(SqStack st)
{
    
 //write your own codes
 if(st.top==-1)
    return true;
 return false;
}

//输出栈中的元素
int print_S(SqStack st)
{
    
	if( isEmpty(st) )
	{
    
		printf("Stack is Empty");
		return 0;
	}
	int iPointer = st.top;
	while(iPointer != -1)
	{
    
		printf("%d ",st.data[iPointer]);
		--iPointer;
	}
	printf("\n");
	return 1;
}

int main()
{
    
	int n , i , e;
	SqStack S;
	initStack(S);
	scanf("%d",&n);
	for(i = 0 ; i < n; ++i)
	{
    
		scanf("%d",&e);
		Push(S,e);
	}
	print_S(S);    //需要写出的函数:打印栈
	return 0;
}

链式栈

4225

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0
#define s top

typedef int Status;

typedef struct Node
{
    
    char data;
    struct Node *next;
}Node;

Node *top;

const Status isEmpty()
{
    
    return (top==NULL)?TRUE:FALSE;
}

void Push(const char x)
{
    
    Node *n = (struct Node *)malloc(sizeof(struct Node));
	n->data = x;
	n->next = top;
	top = n;
}

Status Pop(char *x)
{
    
	if (isEmpty())
		return FALSE;
	*x = top->data;
	Node *t = top;
	top = top->next;
	free(t);
	return TRUE;
}

Status getTop(char *x)
{
    
	if (isEmpty())
		return FALSE;
	*x = top->data;
	return TRUE;
}

Status match(char f[]) {
    
	char* p = f;
	char ch;
	while (*p != '\0') {
    
		if (*p == 39) {
    //单引号
			++p;
			while (*p != 39)//小括号
				++p;
			++p;
		}
		else if (*p == 34) {
    //双引号
			++p;
			while (*p != 34)//小括号
				++p;
			++p;
		}
		else {
    
			switch (*p) {
    
			case '(':
			case '{':
			case '[':Push(*p); break;
			case ')':getTop(&ch);
				if (ch == '(')
					Pop(&ch);
				else return 0;
				break;
			case '}':getTop(&ch);
				if (ch == '{')
					Pop(&ch);
				else return 0;
				break;
			case ']':getTop(&ch);
				if (ch == '[')
					Pop(&ch);
				else return 0;
			}
			++p;
		}
	}
	if (isEmpty())//如果栈为空,表示括号都已经匹配
		return 1;
	else
		return 0;
}

int main()
{
    
	top = NULL;
	char str[100];
	gets(str);
	if (match(str))
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}

栈与递归

4227

#include<stdio.h>

// write your code here
int sum(int a[],int x)
{
    
    if(x<1)    return 0;
    return a[x-1]+sum(a,--x);
}

int main()
{
    
	int a[6],i;
	for (i=0;i<6;i++)
		scanf("%d", a+i);
	printf("%d\n", sum(a, 6));
	return 0;
}

4226

#include<stdio.h>

// write your code here
int max(int a[], int x)
{
    
    if(x<1) return -1;
    int tmp=max(a, x-1);
    return a[x-1]>tmp?a[x-1]:tmp;
}

int main()
{
    
	int a[6],i;
	for (i=0;i<6;i++)
		scanf("%d", a+i);
	printf("%d\n", max(a, 6));
	return 0;
}

4228

#include <stdio.h>

// write your code here
float average(float a[],int n,int y)
{
    
    if (n == 1)
        return a[0];
    else
        return ((n - 1) * average(a, n - 1, y) + a[n - 1]) / n;
}

int main()
{
    
	float a[6];
	int i;
	for (i=0;i<6;i++)
		scanf("%f", a+i);
	printf("%.2f\n", average(a, 6, 6));
	return 0;
}

队列

4236

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0

typedef int Status;

typedef struct Node {
    
        int data;
        struct Node *next;
}Node, *pNode;

pNode p;

void InitQueue()
{
    
	p = (struct Node *)malloc(sizeof(struct Node));
	p->next = p;
}

void EnQueue(const int x)
{
    
    // write your code here
    Node *s;
    s = (Node *)malloc(sizeof(Node));
    s->data = x;
    if (p == NULL)
    {
    
        p = s;
        p->next = p;
    }
    else
    {
    
        s->next = p->next; //将s作为*p之后的结点
        p->next = s;
        p = s; //*p指向*s
    }
}

const Status isEmpty()
{
    
    // write your code here

}

Status DeQueue(int *x)
{
    
    // write your code here
    Node *s;
    if (p == NULL)
        return 0;
    if (p->next == NULL)
    {
     //只有一个结点的情况
        *x = p->data;
        free(p);
        p = NULL;
    }
    else
    {
     //将*p之后结点的data域值赋给x,然后删除之
        s = p->next;
        *x = s->data;
        p->next = s->next;
        free(s);
    }
    return 1;
}

const void Print()
{
    
	Node *t = p->next;
	while (t->next!=p->next) {
    
		printf("%d ", t->next->data);
		t = t->next;
	}
	printf("\n");
}

int main()
{
    
	InitQueue();
	int x;
	EnQueue(1);
	EnQueue(2);
	EnQueue(3);
	DeQueue(&x);
	EnQueue(4);
	DeQueue(&x);
	DeQueue(&x);
	DeQueue(&x);
	EnQueue(5);
	Print();
	return 0;
}

4915

#include<iostream>
using namespace std;

struct Node {
    
    int data;
	Node *next;
};

class LinkList {
    
private:
	Node *first;
public:
	LinkList();
	void Insert(const int &x);
	Node *First();
	int Length(Node *n);
};

LinkList::LinkList()
{
    
	first = new Node();
	first->next = NULL;
}

void LinkList::Insert(const int &x)
{
    
	Node *t = first;
	while (t->next!=NULL)
		t = t->next;
	Node *n = new Node();
	n->data = x;
	n->next = t->next;
	t->next = n;
}

Node *LinkList::First()
{
    
	return first;
}

//write your code here
int LinkList::Length(Node *n)
{
    
    if(n==NULL) return -1;
    return 1+Length(n->next);
}

int main()
{
    
	LinkList l;
	int x;
	cin >> x;
	while (x!=-1) {
    
		l.Insert(x);
		cin >> x;
	}
	//write your code here
    printf("%d\n", l.Length(l.First()));
	return 0;
}

4872

#include<stdio.h>
#include<stdlib.h>

typedef struct QNode
{
    
    int data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct{
    
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;


int InitQueue(LinkQueue &Q)
{
    
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
	if( !Q.front) exit(0);
	Q.front->next = NULL;
	return 1;
}

int EnQueue(LinkQueue &Q, int e)
{
    
	QNode* p=(QueuePtr)malloc(sizeof(QNode));
	//printf("--\n");
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return 1;
}

int DeQueue(LinkQueue &Q,int &e)
{
    
    QNode *p=Q.front->next;
    Q.front->next=p->next;
    if(p==Q.rear)   Q.rear==Q.front;
    e=p->data;
    free(p);
    return 1;
}
int main()
{
    
	int m,n;
	int i,e;
	LinkQueue Q;
	InitQueue(Q);
	scanf("%d",&n);
	for(i = 0 ; i < n ; ++i)
	{
    
		scanf("%d",&e);
		EnQueue(Q,e);
	}
	scanf("%d",&m);
	for(i = 0 ; i < m ; ++i)
	{
    
		DeQueue(Q,e);
		printf("%d ",e);
	}
	printf("\n");
	return 0;
}

4235

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0

typedef int Status;

int *q;
int maxSize;
int front;
int rear;
int tag;

const Status isFull()
{
    
    // write your code here
    if(rear%maxSize==front&&tag==1)
        return 1;
    return 0;
}

const Status isEmpty()
{
    
    // write your code here
    if(rear==front&&tag==0)
        return 1;
    return 0;
}

void InitStack(int sz)
{
    
    maxSize = sz;
	q = (int *)malloc(maxSize*sizeof(int));
	front = 0;
	rear = 0;
	tag = 0;
}

void output()
{
    
    int i;
	for (i=0;i<maxSize;i++)
		printf("%d ", q[i]);
}

Status EnQueue(int x)
{
    
    // write your code here
    if(isFull())    return 0;
    q[rear]=x;
    rear=(rear+1)%maxSize;
    if(rear==front) tag=1;
    return 1;
}

Status DeQueue(int x)
{
    
    // write your code here
    if(isEmpty())   return 0;
    x=q[front];
    front=(front+1)%maxSize;
    if(rear==front) tag==0;
    return 1;
}

int main()
{
    
	InitStack(4);
	int m;
	EnQueue(1);
	DeQueue(m);
	DeQueue(m);
	EnQueue(2);
	EnQueue(0);
	EnQueue(4);
	EnQueue(5);
	DeQueue(m);
	DeQueue(m);
	EnQueue(6);
	output();
	return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Do1phln/article/details/120939080

智能推荐

SLAM cartographer_cartographer carto_slam_武溪嵌人的博客-程序员秘密

http://blog.csdn.net/zyh821351004/article/details/52421005cartographer与karto的比较1. 两者采取的都是图优化框架。  采取的优化库不一致, karto采取的是spa(karto_slam)或g2o(nav2d), cartographer采取的是google的ceres构建problem优化。 kart

判断一个url地址是不是404状态(用curl函数)_chuiqingbang0209的博客-程序员秘密

&lt;?php  $url = "http://www.kxblogs.com/n/20161108/74429879.html";  $ch = curl_init ();   curl_setopt($ch, CURLOPT_URL, $url);   curl_setop...

功能强大的黑科技网站--10连_翊兮的博客-程序员秘密

运用工具,可以大大提高我们的工作和生活效率,节省时间,下面是一些阔以白嫖的良心在线工具网站。1.docsmall实用压缩工具推荐给经常需要压缩图片、GIF、PDF文件的小伙伴。除了压缩功能,网站还支持PDF合并和分割。网站 简单美观,体验感很nice。2.创客贴平面设计作图神器用来做封面图非常方便,通过这个网站你可以制作好看的海报、简历、新媒体文章的首页图等等,甚至很多免费且好看的 PPT插件可以直接用,良心工具呀~3.奶牛快传好用的网盘工具体验还不错的网

c++构造函数种类_cdljn2012的博客-程序员秘密

c++构造函数种类构造函数分四类:无参数构造函数、带参数构造函数、拷贝构造函数、默认构造函数。其中,普通构造函数,分带参数与不带参数。拷贝构造函数,用一个对象去初始化另外一个对象。默认构造函数,当类中没有定义构造函数时,编译器默认提供一个无参构造函数,并且其函数体为空。class String{public: String(const char *str = NULL); // 普通构造函数 String(const String &amp;other); // 拷贝构造函

element ui的table组件在鼠标滑动时边框线消失的解决_笑道三千的博客-程序员秘密

一,表现形式为了明显点,我改成红色边框线,可以看到在鼠标滑动之后,这个边框线会消失。(注:这个手机号是我瞎写的,别乱打,打通了我会被打的!)二,解决办法使用table时,加上这个样式::cell-style="{background:'#fff'}"...

.net mysql 工作流_Slickflow.NET 开源工作流引擎基础介绍-.NET Core2.0 版本实现介绍 (转)..._吕女士的博客-程序员秘密

前言:.NET Core是.NET Framework的新一代版本,是微软开发的第一个跨平台 (Windows、Mac OSX、Linux) 的应用程序开发框架(Application Framework),未来也将会支持FreeBSD与Alpine平台。.Net Core也是微软在一开始发展时就开源的软件平台,其开发目标是跨平台的 .NET 平台。.NET Core 平台的开发优势 :...

随便推点

Java以form表单形式提交(文件流和json数据)_java form表单提交_楚璃轩的博客-程序员秘密

目录HttpClientFormImpl层HttpClientFormimport com.alibaba.fastjson.JSONObject;import org.apache.http.Consts;import org.apache.http.HttpEntity;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.

️JavaScript系列6部曲:流程控制(万字长文)️_不吃西红柿丶的博客-程序员秘密

1.顺序结构:从上到下,从左到右执行的顺序,就叫做顺序结构;2.分支结构:if语句,if-else语句,if-else if-else if…语句,switch-case语句,三元表达式语句;3.循环结构:while循环,do-while循环,for循环,后期还有一个for-in循环;

系统设计之降低复杂性_木小丰~的博客-程序员秘密

人活着就是在对抗熵增定律,生命以负熵为生。——薛定谔一、熵增定律### 1、熵增定律熵的概念最早起源于物理学,用于度量一个热力学系统的无序程度。热力学第二定律,又称“熵增定律”,表明了在自然过程中,一个孤立的系统总是从最初的集中、有序的排列状态,趋向于分散、混乱和无序;当熵达到最大时,系统就会处于一种静寂状态。通俗的讲:系统的熵增过程,就是由原始到死亡的过程。“熵”是“活跃”的反义词,代表负能量。非生命,比如物质总是向着熵增演化,屋子不收拾会变乱,手机会越来越卡,耳机线会凌乱,热水会慢慢变凉,太阳会不断燃

Unity3D:CommandInvokationFailure: Gradle build failed._敬畏之心的博客-程序员秘密

问题:CommandInvokationFailure: Gradle build failed. C:\ProgramFiles\Java\jdk1.8.0_131\bin\java.exe-classpath &quot;D:\unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.0.1.ja...

207最新android书籍,[日更-2019.4.25]推荐一本适合“Android系统工程师”看的书_白一喵的博客-程序员秘密

就是它了 主要内容市面上以“深入理解AndroidXXX”为名的书“不计其数”,有的看着特别像相互抄袭... 然鹅,前两天在京东图书搞活动时(满100减50),我确实淘到了这本清新脱俗的讲Android系统架构的书:《最强Android书-架构大剖析》,其对于理解Android体系架构还是很有用的!此书讲解了Android个版本之间的差异及变迁,并以Linux的角度分析了Android分区和文件系...

fastapi-创建一个项目模板_fastapi 创建项目_Chise1的博客-程序员秘密

项目模板源码:步骤如下:安装poetry包管理工具为什么用的是这个,我也不知道…pip3 install poetry创建项目执行poetry new 项目名创建项目文件夹执行poetry install安装虚拟环境执行poetry shell启动虚拟环境搜索虚拟环境的python位置:which python将pycharm的settings的python改为4里面python的地址修改pyproject.toml里面的相关信息完成之后项目结构大概如下:fastapiStud

推荐文章

热门文章

相关标签