【题目要求】输入若干串字符,每次遇到换行符’\n’时,则输出本行当前处理结果。输入时以“#”字符作为结束符,当遇到这个符号后,意味着字符串输入结束。(在输入过程中如果输入“<”,则表示回退一格;在输入过程中如果包含了“@”,则表示回退到本行行首。)
提示:输入字符时不要采用cin>>ch;的方式接收字符串,因为cin会屏蔽换行符,不能达到预期的效果。可以考虑采用ch=getchar();语句接收字符。在处理字符串时,要分别对’<’,’@’,’#’,’\n’进行不同的处理。
【用顺序栈来实现】
1.顺序栈利用连续的储存单元依次存放栈底到栈顶的数据,需要提前设置好栈的大小;
Stack::Stack()//解析函数(无参构造)
{
top = -1;
size = MAX_SIZE;
data = new char(size);//缺省构造函数分配最大内存空间
}
Stack::Stack(int s)//(有参构造)根据要求初始化大小
{
top = -1;
size = s;
data = new char(size);
}
Stack::~Stack()//(析构函数)可以利用它来释放构造函数分配的空间
{
delete []data;
}
2.每一次入栈和出栈的时候,我们需要判断是否是满栈或者是空栈,以防止发生“上溢”或者“下溢”,可以写一个简单的函数来判断;
//判断栈是否为空
bool Stack::isEmpty()
{
if (top == -1)
return true;
else
return false;
}
//判断栈是否已经满了
bool Stack::isFull()
{
if (top == size)
return true;
else
return false;
}
完整的:
#include<iostream>
#include<string.h>
using namespace std;
const int MAX_SIZE = 100;
class Stack
{
private:
char* data;
int size;
int top;
public:
Stack();
Stack(int s);
~Stack();
void push(char ch);
void pop();
char getTop();
bool isEmpty();
bool isFull();
void setNull();
void reverseDisplay();
};
Stack::Stack()
{
top = -1;
size = MAX_SIZE;
data = new char(size);
}
Stack::Stack(int s)
{
top = -1;
size = s;
data = new char(size);
}
Stack::~Stack()
{
delete[]data;
}
//获得栈顶元素
char Stack::getTop()
{
char x;
if (isEmpty())
{
cout << "栈已空,无法提取栈顶元素" << endl;
}
else
{
x = data[top];
return x;
}
}
//栈设置为空
void Stack::setNull()
{
top = -1;
//cout << "已经将栈设置为空" << endl;
}
//判断栈是否为空
bool Stack::isEmpty()
{
if (top == -1)
return true;
else
return false;
}
//判断栈是否已经满了
bool Stack::isFull()
{
if (top == size)
return true;
else
return false;
}
//进栈
void Stack::push(char ch) {
if (top <= size)
{
top++;
data[top] = ch;
}
else if (isFull())
{
cout << "栈已经满了" << endl;
}
}
//出栈
void Stack::pop()
{
if (top >= 0)
{
//char x;
//x = data[top];
top--;
}
/*else if (isEmpty())
{
cout << "栈为空!" << endl;
}*/
}
//从栈底到栈顶显示数据
void Stack::reverseDisplay()
{
int top1 = 0;
while (!isEmpty())
{
char ch1;
if (top1 <= top)
{
ch1 = data[top1];
top1++;
cout << ch1;
}
else
break;
}
}
//例题
int main()
{
int a = 1;
Stack s1;
char c;
int flag = 1;
while (a == 1)
{
c = getchar();
switch (c)
{
case'<':
if (!s1.isEmpty())
{
s1.pop();
} flag = 0; break;
case'@':
s1.setNull(); flag = 0; break;
case'#':
a = 0; flag = 0; break;
case'\n':
a = 0; flag = 0; break;
}
if (flag == 1 && !s1.isFull())
s1.push(c);
else if (flag == 0)
{
flag = 1;
}
else
{
cout << "栈已满" << endl;
a = 0;
}
}
if (!s1.isEmpty())
{
s1.reverseDisplay();
}
else
cout << "栈已空" << endl;
system("pause");
return 0;
}
结果:
【用简单的链栈来实现】只在上面的代码里做部分改动;
1.定义一个数据结构Node,两个成员一个是数据,一个是指针
struct Node
{
char data;
struct Node *next;
};
2.初始化链表
//初始化链栈为空
void Stack::initStack()//如果这里不做初始化的话,就在输入有@ :将链表置空会出现问题
{
top = new struct Node;
top->next = NULL;
}
3.链栈中我们需在出栈的时候注意链栈是否为空
//出栈
void Stack::pop()
{
struct Node* p = top;
if (top != NULL)
{
top = top->next;
delete p;
p = top;
}
else
cout << "栈已空,无法出栈!" << endl;
}
可以不考虑栈是否是满栈
4.设置链栈为空
//栈设置为空
void Stack::setNull()
{
struct Node *p;
while (top != NULL)
{
p = top;
top = top->next;
delete p;
}
//cout << "已经将栈设置为空" << endl;
}
5.元素输出
//从栈顶到栈底显示数据
void Stack::reverseDisplay()
{
struct Node* p;
p = top;
while (p!=NULL)
{
char ch1;
ch1 = p->data;
cout << p->data;
p = p->next;
}
}
完整的:?
#include<iostream>
#include<string.h>
using namespace std;
const int MAX_SIZE = 100;
struct Node
{
char data;
struct Node *next;
};
class Stack
{
private:
struct Node *top;
public:
void initStack();
void push(char ch);
void pop();
char getTop();
bool isEmpty();
void setNull();
void reverseDisplay();
};
//初始化链栈为空
void Stack::initStack()
{
top = new struct Node;
top->next = NULL;
}
//进栈
void Stack::push(char ch) {
struct Node* s = new struct Node;
s->data = ch;
s->next = top;
top = s;
}
//出栈
void Stack::pop()
{
struct Node* p = top;
if (top != NULL)
{
top = top->next;
delete p;
p = top;
}
else
cout << "栈已空,无法出栈!" << endl;
}
//获得栈顶元素
char Stack::getTop()
{
char x;
if (isEmpty())
{
cout << "栈为空,无法提取栈顶元素" << endl;
return 1;
}
else
{
x = top->data;
return x;
}
}
//栈设置为空
void Stack::setNull()
{
struct Node *p;
while (top != NULL)
{
p = top;
top = top->next;
delete p;
}
//cout << "已经将栈设置为空" << endl;
}
//判断栈是否为空
bool Stack::isEmpty()
{
if (top == NULL)
return true;
else
return false;
}
//从栈顶到栈底显示数据
void Stack::reverseDisplay()
{
struct Node* p;
p = top;
while (p!=NULL)
{
char ch1;
ch1 = p->data;
cout << p->data;
p = p->next;
}
}
//例题
int main()
{
int a = 1;
Stack s1;
s1.initStack();
char c;
int flag = 1;
while (a == 1)
{
c = getchar();
switch (c)
{
case'<':
if (!s1.isEmpty())
{
s1.pop();
}
flag = 0;
break;
case'@':
s1.setNull();
flag = 0;
break;
case'#':
a = 0;
flag = 0;
break;
case'\n':
a = 0;
flag = 0;
break;
}
if (flag == 1)//flag 用于记录是否是特殊符号 进行标记
s1.push(c);
else if (flag == 0)
{
flag = 1;
}
}
if (!s1.isEmpty())
{
s1.reverseDisplay();
}
else
cout << "栈已空" << endl;
s1.setNull();//手动清除链栈内容
system("pause");
return 0;
}
结果:?
仅仅是个人练习的结果,如有错误,还望指出;
?
?
|