服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++利用链表写一个简单的栈实例详解

C++利用链表写一个简单的栈实例详解

2021-05-12 16:30huplion C/C++

这篇文章主要介绍了C++利用链表写一个简单的栈实例详解的相关资料,需要的朋友可以参考下

C++中其实有stack的模板类。功能更为强大。

自己写一个栈能让我们对栈这种数据结构更加熟悉。这个栈有一个不足之处就是里面存放的元素类型只能为int。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;
class Stack
{
private:
  struct Node
  {
    int data;
    Node *next;
  };
  Node *head;
  Node *p;
  int length;
 
public:
  Stack()
  {
    head = NULL;
    length = 0;
  }
  void push(int n)//入栈
  {
    Node *q = new Node;
    q->data = n;
    if (head == NULL)
    {
      q->next = head;
      head = q;
      p = q;
    }
    else
    {
      q->next = p;
      p = q;
    }
    length ++;
  }
 
  int pop()//出栈并且将出栈的元素返回
  {
    if (length <= 0)
    {
      abort();
    }
    Node *q;
    int data;
    q = p;
    data = p->data;
    p = p->next;
    delete(q);
    length --;
    return data;
  }
  int size()//返回元素个数
  {
    return length;
  }
  int top()//返回栈顶元素
  {
    return p->data;
  }
  bool isEmpty()//判断栈是不是空的
  {
    if (length == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  void clear()//清空栈中的所有元素
  {
    if (length > 0)
    {
      pop();
    }
  }
};
int main()
{
  //以下为测试代码
  Stack s;
  s.push(1);
  s.push(2);
  s.push(3);
  while(!s.isEmpty())
  {
    cout<<s.pop()<<endl;
  }
  return 0;
}

对这段代码稍加修改,这个栈就能存放其他类型的元素

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;
template<class T>class Stack
{
private:
  struct Node
  {
    T data;
    Node *next;
  };
  Node *head;
  Node *p;
  int length;
 
public:
  Stack()
  {
    head = NULL;
    length = 0;
  }
  void push(T n)//入栈
  {
    Node *q = new Node;
    q->data = n;
    if (head == NULL)
    {
      q->next = head;
      head = q;
      p = q;
    }
    else
    {
      q->next = p;
      p = q;
    }
    length ++;
  }
 
  T pop()//出栈并且将出栈的元素返回
  {
    if (length <= 0)
    {
      abort();
    }
    Node *q;
    int data;
    q = p;
    data = p->data;
    p = p->next;
    delete(q);
    length --;
    return data;
  }
  int size()//返回元素个数
  {
    return length;
  }
  T top()//返回栈顶元素
  {
    return p->data;
  }
  bool isEmpty()//判断栈是不是空的
  {
    if (length == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  void clear()//清空栈中的所有元素
  {
    while(length > 0)
    {
      pop();
    }
  }
};
int main()
{
  Stack<char> s;
  s.push('a');
  s.push('b');
  s.push('c');
  while(!s.isEmpty())
  {
    cout<<s.pop()<<endl;
  }
  return 0;
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

延伸 · 阅读

精彩推荐