3)链栈和链队列

链栈:

 1 #include<iostream>
 2 using namespace std;
 3 struct node{
 4     int data;
 5     node *next;
 6 };
 7 enum error{underflow,overflow,success,fail};
 8 class stack{
 9 public:
10     stack();//初始化
11     ~stack();//定义析构函数,以在需要时自动释放链栈空间
12     bool empty() const;//判断为空
13     bool full() const;//判断为满
14     int get_top(int &x)const;//取栈顶元素
15     int push(const int x);//入栈
16     int pop();//出栈
17 private:
18     int count;//栈中数据的个数
19     int data;//栈中数据
20     node *top;
21 };
22 /*初始化:栈顶指针置为空,计数变量设置为0*/
23 stack::stack(){
24     count=0;
25     top=NULL;
26 }
27 
28 /*
29 *判断栈是否为空:count=0||top=NULL
30 */
31 bool stack::empty()const{
32     return count==0;//return top==NULL;
33 }
34 
35 /*
36 *取栈顶元素的实现,若栈不为空,返回栈顶元素的值,否则返回出错信息
37 */
38 int stack::get_top(int &x)const{
39     if(empty())return underflow;
40     x=top->data;
41     return success;
42 }
43 
44 /*
45 *入栈
46 */
47 int stack::push(const int x){
48     node *s=new node;
49     s->data=x;
50     s->next=top;
51     top=s;
52     count++;
53     return success;
54 }
55 
56 /*
57 出栈
58 */
59 int stack::pop(){
60     if(empty())return underflow;
61     node *u=new node;
62     u=top;
63     top=u->next;
64     delete u;
65     count--;
66     return success;
67 }
68 
69 stack::~stack(){
70     while(!empty())pop();
71 }
72 
73 int xchg(int n,stack s){
74     cout<<"十进制:["<<n<<"]->8进制:";
75     int mod,x;
76     while(n!=0){
77         mod = n % 8;
78         s.push(mod);
79         n/=8;
80     }
81     while(s.empty()!=true){
82         s.get_top(x);
83         cout<<x;
84         s.pop();
85     }
86     cout<<endl;
87     return 0;
88 }
89 int main()
90 {
91     stack s;
92     xchg(100,s);
93     return 0;
94 }
95  

链队列:

  1 #include<iostream>
  2 #include<iomanip>
  3 using namespace std;
  4 
  5 enum error{underflow,overflow,success};
  6 
  7 struct node{
  8     int data;
  9     node *next;
 10 };
 11 
 12 class queue{
 13 public:
 14     queue();
 15     ~queue();//定义析构函数,以在需要时自动释放链队列空间
 16     bool empty() const;//判断为空
 17     bool full() const;//判断为满
 18     int get_front(int &x )const;//取队头元素
 19     int append(const int x);//入队
 20     int serve();//出队
 21 private:
 22     int count;
 23     node *front,*rear;
 24 };
 25 
 26 queue::queue(){//初始化队列
 27     front =new node;
 28     rear =new node;
 29     //node *rear = new node;
 30     count=0;
 31     rear = front;
 32     front->next=NULL;
 33     rear->next=NULL;
 34 }
 35 
 36 bool queue::empty()const{//判断为空 
 37     if(count==0)return true;
 38     else return false;
 39 //    return count==0;
 40 }
 41 
 42 int queue::get_front(int &x)const{//取队头元素
 43     if(empty())return underflow;
 44     else{
 45         x=front->next->data;
 46         return success;
 47     }
 48 }
 49 
 50 int queue::append(const int x){//入队
 51     node *s= new node;
 52     s->data=x;
 53     s->next=NULL;
 54     rear->next=s;
 55     rear=s;
 56     count++;
 57     return success;
 58 }
 59 
 60 int queue::serve(){//出队
 61     node *u =new node;
 62     if(empty())return underflow;
 63     else{
 64         u=front->next;
 65         front->next=u->next;
 66         delete u;
 67         count--;
 68     }
 69     if(front->next==NULL)rear=front;//如果删除的是最后一个结点,尾指针rear指向了一个已经删除的节点
 70     return success;
 71 }
 72 
 73 queue::~queue(){
 74     while(!empty())  serve();
 75     delete front;//最后释放头结点
 76 }
 77 
 78 int main(){
 79     queue q;
 80     int n;
 81     cout<<"please input 杨辉三角要打印的行数:";
 82     cin>>n;
 83     int s1,s2;
 84     for(int i=1;i<n;i++)cout<<"  ";
 85     cout<<1<<endl;//输出第一行上的1
 86     q.append(1);//所输出1入队
 87     for(int i=2;i<=n;i++){//逐行计算并输出2~N行上的数据
 88         s1=0;//存放前一个入队数
 89         for(int k=1;k<=n-i;k++ )cout<<"  ";
 90         for(int j=1;j<=i-1;j++){//先计算并输出n-1个数
 91             q.get_front(s2);//取队头元素并出队
 92             q.serve();
 93             cout<<s1+s2<<setw(4);
 94             q.append(s1+s2);//所输出的当行中的元素入队
 95             s1=s2;
 96         }
 97         cout<<1<<endl;//输出当行中的子最后一个元素1并换行
 98         q.append(1);
 99     }
100     return 0;
101 }
原文地址:https://www.cnblogs.com/minmsy/p/5022031.html