头文件
#include <iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *Linklist;
Linklist aaaa();
Linklist bbbb();
#include <iostream>
using namespace std;
#define maxsize 50
typedef struct
{
int data[maxsize];
int front, rear;
} queue;
void init(queue &q);
bool enqueue(queue &q, int x);
int dequeue(queue &q);
#include <iostream>
using namespace std;
#define maxsize 50
typedef struct
{
char data[maxsize];
int top;
} stack;
void Init(stack &s);
bool push(stack &s, int x);
int pop(stack &s);
#include <iostream>
using namespace std;
#define maxsize 50
typedef struct
{
int data[maxsize];
int top[2];
} stack1;
#include"0_linklist.h"
Linklist aaaa()
{
LNode *L = new LNode;
int a;
cin >> a;
L->data = a;
LNode *p = L;
cin >> a;
while (a != 0)
{
LNode *q = new LNode;
q->data = a;
q->next = NULL;
p->next = q;
p = p->next;
cin >> a;
}
p->next = NULL;
return L;
}
Linklist bbbb()
{
LNode *L = new LNode;
LNode *p = L;
int a;
cin >> a;
while (a != 0)
{
LNode *q = new LNode;
q->data = a;
q->next = NULL;
p->next = q;
p = p->next;
cin >> a;
}
p->next = NULL;
return L;
}
#include"0_queue.h"
void init(queue &q)
{
q.rear = q.front = 0;
}
bool enqueue(queue &q, int x)
{
if ((q.rear + 1) % maxsize == q.front)
return false;
q.data[q.rear] = x;
q.rear = (q.rear + 1) % maxsize;
return true;
}
int dequeue(queue &q)
{
if (q.rear == q.front)
return -100;
int x = q.data[q.front];
q.front = (q.front + 1) % maxsize;
return x;
}
#include"0_stack.h"
void Init(stack &s)
{
s.top = -1;
}
bool push(stack &s, int x)
{
if (s.top == maxsize - 1)
return false;
s.data[++s.top] = x;
return true;
}
int pop(stack &s)
{
if (s.top == -1)
return false;
return s.data[s.top--];
}
Q是一个队列,S是一个空栈,编写算法使得队列中元素逆置
void reverse(stack &s, queue &q)
{
while (q.front != q.rear)
push(s, dequeue(q));
while (s.top != -1)
enqueue(q, pop(s));
}
int main01()
{
stack s;
Init(s);
queue q;
init(q);
int x;
cin >> x;
while (x != -1)
{
enqueue(q, x);
cin >> x;
}
reverse(s, q);
while (q.front != q.rear)
{
cout << q.data[q.front] << " ";
q.front = (q.front + 1) % maxsize;
}
return EXIT_SUCCESS;
}
判断单链表的全部n个字符是否中心对称 用栈来实现
#include <iostream>
#include"0_queue.h"
#include"0_stack.h"
#include"0_linklist.h"
using namespace std;
int fun(LNode *L)
{
int n = 0, j;
stack s;
Init(s);
LNode *p = L->next;
while (p != NULL)
{
++n;
p = p->next;
}
p = L->next;
for (j = 0; j < n / 2; ++j)
{
push(s, p->data);
p = p->next;
}
if (n % 2 != 0)
p = p->next;
while (p != NULL)
{
if (pop(s) == p->data)
p = p->next;
else
return 0;
}
return 1;
}
int main02()
{
stack s;
LNode *L = bbbb();
cout << fun(L) << endl;
return EXIT_SUCCESS;
}
两个栈s1,s2都采用顺序存储,并共享一个存储区[0…,maxsize-1]。采用栈顶相向,迎面增长的存储方式,设计s1,s2入栈和出栈的操作。
#include <iostream>
#include "0_stack[].h"
using namespace std;
void Init(stack1 &s)
{
s.top[0] = -1;
s.top[1] = maxsize;
}
bool push(stack1 &s, int i, int x)
{
if (i < 0 || i > 1)
return false;
if (s.top[1] - s.top[0] == 1)
return false;
switch (i)
{
case 0:
s.data[++s.top[0]] = x;
break;
case 1:
s.data[--s.top[1]] = x;
break;
}
return true;
}
int pop(stack1 &s, int i)
{
if (i < 0 || i > 1)
return -1;
switch (i)
{
case 0:
if (s.top[0] == -1)
return -1;
else
return s.data[s.top[0]--];
break;
case 1:
if (s.top[1] == maxsize)
return -1;
else
return s.data[s.top[1]++];
break;
}
}
int main03()
{
stack1 s;
Init(s);
push(s, 0, 15);
push(s, 0, 13);
push(s, 1, 10);
pop(s, 0);
cout << s.data[s.top[0]];
return EXIT_SUCCESS;
}
判断一个表达式中括号是否配对(假设只包含圆括号)
#include <iostream>
#include"0_stack.h"
using namespace std;
bool fun(stack &s, string str)
{
int i = 0;
while (str[i] != '\0')
{
switch (str[i])
{
case '(':
push(s, '(');
break;
case '{':
push(s, '{');
break;
case ')':
if (pop(s) != '(')
return false;
break;
case '}':
if (pop(s) != '{')
return false;
break;
default:
break;
}
++i;
}
if (s.top == -1)
return true;
else
return false;
}
int main04()
{
stack s;
Init(s);
string S = "(15+()))";
cout << "结果为:";
bool q = fun(s, S);
if (q == true)
cout << "括号匹配!" << endl;
else
cout << "括号不匹配!" << endl;
return EXIT_SUCCESS;
}
假设一个序列为HSSHHHS,运用栈的知识,编写算法将S全部提到H之前,即为SSSHHHH
#include <iostream>
#include "0_stack.h"
using namespace std;
void fun(string S)
{
stack s;
Init(s);
string newS = "0000000";
int i = 0, j = 0;
while (S[i] != '\0')
{
if (S[i] == 'H')
push(s, S[i++]);
else
newS[j++] = S[i++];
}
while (s.top != -1)
newS[j++] = pop(s);
i = 0;
while (newS[i] != '\0')
cout << newS[i++];
}
int main05()
{
string S = "HSSHHHS";
fun(S);
return EXIT_SUCCESS;
}
利用栈实现递归函数的非递归算法
#include <iostream>
using namespace std;
#define maxsize 50
double fun2(int n, double x) {
int top = n - 2;
double *p = new double[n];
double fv1 = 1;
double fv2 = 2 * x;
n = 2;
while (top > -1)
{
p[top] = 2 * x * fv2 - 2 * (n - 1) * fv1;
fv1 = fv2;
fv2 = p[top];
++n;
top--;
}
delete[] p;
if (n == 0)
return fv1;
return fv2;
}
int main06()
{
int n = 2;
cout << fun2(n, 5);
return EXIT_SUCCESS;
}