链式存储的栈的实现
2013-08-18 15:59:58
顺序存储的栈的缺点:
存储空间有限,当栈中元素个数超出定义的栈的MAXSIZE时,再进行插入,就会出现访问越界。当然,可以通过realloc重新分配内存以扩大容量,不过这样不但会增加复杂度,而且要求存储空间是连续的,当内存中没有这么大的连续空间时,就会发生内存溢出;
为了解决这个问题,链式存储的栈不失为好的办法,下面是用链表实现的栈。
用链式存储是实现栈时,注意几点:
- 将链表头结点作为栈顶,而且也不需要附加的头结点;
- 栈的结构只有一个链表指针;
- 栈的初始化,就是将链表头结点置为NULL;
- 栈的销毁,就是链表的销毁;
- 栈的push、pop,就是在链表的头部进行插入、删除的操作,在pop时,注意对空栈的处理,下面的代码中用assert(NULL != s.top);检查是否为空;
- 求栈的长度的操作,就是求链表长度;
- 获取栈顶元素,就是获取链表头结点元素,注意对空栈的处理,下面的代码中用assert(NULL != s.top);检查是否为空。
代码(测试暂未发现错误,欢迎交流指正!):
1 #include <iostream> 2 #include <cassert> 3 using namespace std; 4 5 typedef int DataType; 6 7 typedef struct node 8 { 9 DataType data; 10 struct node *next; 11 }LNode,*PLNode; 12 13 typedef struct stack 14 { 15 PLNode top; 16 }Stack; 17 18 //初始化栈 19 void InitStack(Stack &s) 20 { 21 s.top = NULL; //不需头结点 22 } 23 24 //初始化栈 25 void DestoryStack(Stack &s) 26 { 27 PLNode pPre = s.top; 28 PLNode pCur = pPre->next; 29 while (NULL != pCur) 30 { 31 delete pPre; 32 pPre = pCur; 33 pCur = pCur->next; 34 } 35 } 36 37 //出栈 38 void PushStack(Stack &s,DataType data) 39 { 40 PLNode pNew = new LNode; 41 pNew->data = data; 42 43 pNew->next = s.top; 44 s.top = pNew; 45 } 46 47 //出栈 48 DataType PopStack(Stack &s) 49 { 50 assert(NULL != s.top); 51 52 DataType topData = s.top->data; 53 54 PLNode headToDelete = s.top; 55 s.top = s.top->next; 56 delete headToDelete; 57 58 return topData; 59 } 60 61 //获取栈顶元素 62 DataType GetTopOfStack(Stack &s) 63 { 64 assert(NULL != s.top); 65 return ( s.top->data ); 66 } 67 68 //获取栈中元素个数 69 size_t GetLengthOfStack(Stack &s) 70 { 71 size_t lengthOfStack = 0; 72 PLNode pCur = s.top; 73 while (NULL != pCur) 74 { 75 ++lengthOfStack; 76 pCur = pCur->next; 77 } 78 79 return lengthOfStack; 80 } 81 82 void DisplayStack(Stack &s) 83 { 84 PLNode pCur = s.top; 85 while (NULL != pCur) 86 { 87 cout<<pCur->data<<"\t"; 88 pCur = pCur->next; 89 } 90 cout<<endl; 91 } 92 93 //栈测试 94 void TestStack() 95 { 96 Stack s; 97 InitStack(s); 98 size_t command = 0; 99 100 const size_t PUSH = 0; 101 const size_t POP = 1; 102 const size_t GETLENGTH = 2; 103 const size_t GETTOP = 3; 104 105 DataType data; 106 107 cout<<"Please enter the command ,end with ctrl+z : "<<endl; 108 while (cin>>command) 109 { 110 switch(command) 111 { 112 case(PUSH): 113 { 114 cout<<"Please enter z data to push : "<<endl; 115 cin>>data; 116 cout<<"push "<<data<<endl; 117 118 cout<<"the stack before push : "<<endl; 119 DisplayStack(s); 120 121 PushStack(s,data); 122 123 cout<<"the stack after push : "<<endl; 124 DisplayStack(s); 125 126 break; 127 } 128 129 case(POP): 130 { 131 cout<<"the stack before pop : "<<endl; 132 DisplayStack(s); 133 134 cout<<"pop "<<PopStack(s)<<endl; 135 136 cout<<"the stack after pop : "<<endl; 137 DisplayStack(s); 138 139 break; 140 } 141 142 case(GETLENGTH): 143 { 144 cout<<"the length of stack is : "<<GetLengthOfStack(s)<<endl; 145 146 break; 147 } 148 149 case(GETTOP): 150 { 151 cout<<"the top element of stack is : "<<GetTopOfStack(s)<<endl; 152 153 break; 154 } 155 default: 156 break; 157 } 158 cout<<"Please enter the command ,end with ctrl+z : "<<endl; 159 } 160 161 DestoryStack(s); //销毁栈 162 } 163 164 int main() 165 { 166 TestStack(); 167 return 0; 168 }
测试结果:
1 Please enter the command ,end with ctrl+z : 2 2 3 the length of stack is : 0 4 Please enter the command ,end with ctrl+z : 5 0 6 Please enter z data to push : 7 1 8 push 1 9 the stack before push : 10 11 the stack after push : 12 1 13 Please enter the command ,end with ctrl+z : 14 0 15 Please enter z data to push : 16 2 17 push 2 18 the stack before push : 19 1 20 the stack after push : 21 2 1 22 Please enter the command ,end with ctrl+z : 23 0 24 Please enter z data to push : 25 3 26 push 3 27 the stack before push : 28 2 1 29 the stack after push : 30 3 2 1 31 Please enter the command ,end with ctrl+z : 32 0 33 Please enter z data to push : 34 4 35 push 4 36 the stack before push : 37 3 2 1 38 the stack after push : 39 4 3 2 1 40 Please enter the command ,end with ctrl+z : 41 1 42 the stack before pop : 43 4 3 2 1 44 pop 4 45 the stack after pop : 46 3 2 1 47 Please enter the command ,end with ctrl+z : 48 1 49 the stack before pop : 50 3 2 1 51 pop 3 52 the stack after pop : 53 2 1 54 Please enter the command ,end with ctrl+z : 55 1 56 the stack before pop : 57 2 1 58 pop 2 59 the stack after pop : 60 1 61 Please enter the command ,end with ctrl+z : 62 2 63 the length of stack is : 1 64 Please enter the command ,end with ctrl+z : 65 2 66 the length of stack is : 1 67 Please enter the command ,end with ctrl+z : 68 1 69 the stack before pop : 70 1 71 pop 1 72 the stack after pop : 73 74 Please enter the command ,end with ctrl+z : 75 1 76 the stack before pop : 77 78 Assertion failed: NULL != s.top, file e:\visual studio 2010_projects\link_stack_ 79 2013_08_17\link_stack_2013_08_17\link_stack.cpp, line 50 80 请按任意键继续. . .