#include<iostream>
using namespace std;
class node
{
private:
public:
char data;
node* next;
node(){
this->next = NULL;
}
// void setData(char c){this->data = c;}
// void setNext(node* p){this->next = p;}
// char getData(){return this->data;}
// node* getNext(){return this->next;}
};
int main(){
node* head = NULL;
node node1,node2,node3,node4,node5;
head = &node1;
node1.data='A';
node2.data='b';
node3.data='C';
node4.data='D';
node5.data='r';
head->next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;
//遍历链表
for(node* p = head;p->next!=NULL;){
cout<<p->data<<endl;
p = p->next;
}
system("pause");
}
上述没有使用类似Java的get/set方法的形式对数据与指针进行赋值。
下面给出get/set方法形式赋值的类定义
#include<iostream>
using namespace std;
class node
{
private:
char data;
node* next;
public:
node(){
this->next = NULL;
}
void setData(char c){this->data = c;}
void setNext(node* p){this->next = p;}
char getData(){return this->data;}
node* getNext(){return this->next;}
};
int main(){
node* head = NULL;
node node1,node2,node3,node4,node5;
head = &node1;
node1.setData('A');
node2.setData('b');
node3.setData('C');
node4.setData('D');
node5.setData('r');
head->setNext(&node2);
node2.setNext(&node3);
node3.setNext(&node4);
node4.setNext(&node5);
//遍历链表
for(node *p = head;p!=NULL;p = p->getNext()){
cout<<p->getData()<<endl;
}
system("pause");
}
然后使用链表构建一个链式栈。
#include<iostream>
using namespace std;
class node
{
private:
char data;
node* next;
public:
node(){
this->next = NULL;
}
void setData(char c);
void setNext(node* p);
char getData();
node* getNext();
};
void node::setData(char c){this->data = c;}
void node::setNext(node* p){this->next = p;}
char node::getData(){return this->data;}
node* node::getNext(){return this->next;}
//元素压栈
void push(node** headp,node* n,char c){
n->setData(c);
n->setNext(*headp);
*headp = n;
}
//元素出栈
char pop(node** headp){
char c = (*headp)->getData();
*headp = (*headp)->getNext();
return c;
}
int main(){
node* head = new node();
head->setData('A');
//压栈操作
push(&head,(new node()),'B');
push(&head,(new node()),'C');
push(&head,(new node()),'D');
//弹栈操作
cout<<pop(&head)<<endl;
cout<<pop(&head)<<endl;
cout<<pop(&head)<<endl;
cout<<pop(&head)<<endl;
system("pause");
}
然后用链栈实现判断括号组是否合法:
#include<iostream>
#include <cstdlib>
using namespace std;
class node
{
private:
char data;
node* next;
public:
node(){
this->next = NULL;
}
void setData(char c);
void setNext(node* p);
char getData();
node* getNext();
};
void node::setData(char c){this->data = c;}
void node::setNext(node* p){this->next = p;}
char node::getData(){return this->data;}
node* node::getNext(){return this->next;}
//元素压栈
void push(node** headp,node* n,char c){
n->setData(c);
n->setNext(*headp);
*headp = n;
}
//元素出栈
char pop(node** headp){
*headp = (*headp)->getNext();
char c = (*headp)->getData();
return c;
}
//判断两个元素是否适配
bool checkp(char c1,char c2){
if(c1=='{'&&c2=='}') return true;
if(c1=='('&&c2==')') return true;
if(c1=='['&&c2==']') return true;
return false;
}
//然后这里代码有错,我实在不想写了
bool check(char *k,int size){
bool result = false;
node* head = new node();
char jp = k[0];
//开始压栈
for(int i=0;i<size;i++){
if(i==0){
push(&head,new node(),k[i]);
continue;
}
if(checkp(jp,k[i])){ //期待得到满足,将一个元素出栈,执行下一轮循环
cout<<"元素"<<pop(&head)<<"出栈"<<endl;
continue;
}else //期待不满足,元素压栈
{
cout<<"元素"<<k[i]<<"压栈"<<endl;
node* n = new node();
push(&head,n,k[i]);
jp = k[i];
}
}
//判断栈是否为空
if(((head->getNext())==NULL)){
result = true;
}
return result;
}
int main(){
char s[6] = {'{','[','[',']',']','}'};
cout<<check(&s[0],6)<<endl;
system("pause");
}