标签:ios nat 千万 tac links class 这一 http col
1 链栈通常用单链表表示,由于栈的主要操作是栈顶插入与删除,显然以链表的头部作为栈顶最方便,而且没必要为了操作方便附加一个头结点 2 //链式存储结构(将单链表头指针与栈顶合二为一) 3 typedef struct StackNode 4 { 5 ElemType data; 6 struct StackNode *next; 7 }StackNode,*LinkStackPtr; 8 typedef struct LinkStack 9 { 10 LinkStackPtr top; 11 int count; 12 } 13 Status Push(LinkStack *s,ElemType e) 14 { 15 LinkStackPtr p = (LinkStackPtr)malloc(sizeof(StackNode)); 16 p->data = e; 17 p->next = s->top; //新插入结点的指针域指向栈顶 18 s->top = p; //栈顶换成p 19 s->count++; 20 return OK; 21 } 22 23 Status Pop(LinkStack *s,ElemType *e) 24 { 25 LinkStackPtr p; 26 if(StackEmpty(*s)) 27 { 28 return ERROR; 29 } 30 *e = s->top->data; 31 p = s->top; 32 s->top = s->top->next; 33 free(p); 34 s->count--; 35 return OK; 36 } 37 38 39 可执行代码来自https://blog.csdn.net/sinat_35297665/article/details/79489822 40 #include <iostream> 41 42 using namespace std; 43 44 typedef struct node{ 45 46 int data; 47 48 struct node *next; 49 50 }Node; 51 52 53 54 typedef struct stack{ 55 56 Node *top; /**书本写法是:加一个bottom,个人感觉没什么用,反而加一个count用于表示节点数会更好*/ 57 58 int count; 59 60 }Link_Stack; 61 62 63 64 /**创建一个空栈*/ 65 66 Link_Stack * Creat_stack() 67 68 { 69 70 Link_Stack *p; 71 72 p = new Link_Stack; /**这一步不要忘!需要给p创建空间*/ 73 74 p->count = 0; 75 76 p->top = NULL; 77 78 79 80 return p; 81 82 } 83 84 85 86 /**入栈操作:push*/ 87 88 Link_Stack * Push_stack(Link_Stack *p,int elem) 89 90 { 91 92 if(NULL == p) 93 94 return NULL; 95 96 97 98 Node *temp; 99 100 temp = new Node; 101 102 temp->data = elem; 103 104 105 106 temp->next = p->top; /**注意这里和队列的入队操作不同,next指向不同,为了方便出栈操作*/ 107 108 p->top = temp; 109 110 111 112 p->count += 1; 113 114 115 116 return p; 117 118 } 119 120 121 122 /**出栈操作:pop*/ 123 124 Link_Stack * Pop_stack(Link_Stack *p) 125 126 { 127 128 Node *temp; 129 130 temp = p->top; 131 132 if(NULL == p->top) 133 134 { 135 136 cout << "The stack is empty." << endl; 137 138 return p; 139 140 } 141 142 else{ 143 144 p->top = p->top->next; /** temp = temp->next; 千万不能这么写,看看下一步是什么?*/ 145 146 delete temp; 147 148 149 150 p->count -= 1; 151 152 153 154 return p; 155 156 } 157 158 } 159 160 161 162 /**栈的遍历:输出栈*/ 163 164 int Show_stack(Link_Stack *p) 165 166 { 167 168 Node *temp; 169 170 temp = p->top; 171 172 173 174 if(NULL == p->top) 175 176 { 177 178 cout << "The stack is empty." << endl; 179 180 return 0; 181 182 } 183 184 while(NULL != temp) 185 186 { 187 188 cout << temp->data << ‘ ‘; 189 190 temp = temp->next; 191 192 } 193 194 cout << endl; 195 196 197 198 return 0; 199 200 } 201 202 203 204 int main() 205 206 { 207 208 int i = 5; 209 210 int elem; 211 212 Link_Stack *p; 213 214 p = Creat_stack(); 215 216 while(i--) 217 218 { 219 220 cin >> elem; 221 222 Push_stack(p,elem); 223 224 } 225 226 cout << "空栈插入5个元素后:" << endl; 227 228 Show_stack(p); 229 230 231 232 cout << "删除3个元素后:" << endl; 233 234 for(i = 3;i--;) 235 236 { 237 238 Pop_stack(p); 239 240 } 241 242 Show_stack(p); 243 244 cout << "count:" << p->count << endl; 245 246 247 248 cout << "删除2个元素后:" << endl; 249 250 for(i = 2;i--;) 251 252 { 253 254 Pop_stack(p); 255 256 } 257 258 Show_stack(p); 259 260 cout << "count:" << p->count << endl; 261 262 263 264 Push_stack(p,6); 265 266 cout << "插入元素6后:" << endl; 267 268 Show_stack(p); 269 270 cout << "count:" << p->count << endl; 271 272 273 274 return 0; 275 276 }
标签:ios nat 千万 tac links class 这一 http col
原文地址:https://www.cnblogs.com/ZhengLijie/p/12491305.html