2.1,2.2数据结构瑞格顺序栈链式栈

nefu瑞格顺序栈链式栈

–题目来源-nefu瑞格

顺序栈

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
    Node *p=first->next;
    SeqStack s;
    while(p!=NULL)
    {
        s.Push(p->data);
        p=p->next;
    }
    p=first->next;
      while(p!=NULL)
    {
        s.Pop(p->data);
        p=p->next;
    }

}

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

在这里插入图片描述
output
5
4
8

#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)
{
	//write your code here
    maxSize=sz;
    array=new int[maxSize];
    top=-1;
}

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())
	     x = array[top--];
}

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

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

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;
}

4900

在这里插入图片描述
D

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

在这里插入图片描述

#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)
{
     if(st.top==-1)
         return true;
     else
         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

在这里插入图片描述

#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)
{
     if(st.top==-1)
         return true;
     else
         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;
}

链式栈

4902

在这里插入图片描述
D

4901

在这里插入图片描述
A

4225

在这里插入图片描述

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

#define TRUE 1
#define FALSE 0

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 *str)
{
    // write your code here
    int i;
    char x;
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]=='('||str[i]=='['||str[i]=='{')
            Push(str[i]);
        if(str[i]==')'||str[i]==']'||str[i]=='}')
        {
            if(!isEmpty())
            {
               getTop(&x);
               if((x=='{'&&str[i]=='}')||(x=='['&&str[i]==']')||(x=='('&&str[i]==')'))
                   Pop(&x);
            else
                return 0;
            }
            else
                return 0;
        }
    }
    if(isEmpty())
        return 1;


}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值