栈数据结构和代码
用数组创建栈
1 //使用数组来创建栈 2 //这是一个简单的扑克牌游戏,计算机使用随机数和栈洗牌 3 //在洗好后,将牌发给四家,每家5张牌 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <time.h> 7 #define MAXSTACK 100 //最大栈容量 8 9 //栈数据的存入 10 int stack[MAXSTACK]; //栈的数组声明 11 int top = -1; //栈的顶端 12 13 int push(int value) 14 { 15 if(top >= MAXSTACK) //是否超过容量 16 { 17 printf("栈内容全满\n"); //存入失败 18 return -1; 19 } 20 top ++; //栈指针加1 21 stack[top] = value; //存入栈 22 23 } 24 25 //栈数据的取出 26 int pop() 27 { 28 int temp; 29 if(top < 0) //是否栈是空 30 { 31 printf("栈内容是空的\n"); 32 return -1; //取出失败 33 } 34 temp = stack[top]; //取出数据 35 top--; //栈指针减1 36 return temp; //栈取出 37 } 38 39 40 41 //主程序:洗牌后,将牌发给四个人 42 //红心:数组 0 ~ 12 43 //方块:数组 13 ~ 25 44 //梅花:数组 26 ~ 38 45 //黑桃:数组 39 ~ 51 46 int main() 47 { 48 int card[52]; //扑克牌数组 49 int pos; //牌代码 50 int i,temp; 51 long temptime; 52 srand(time(&temptime) % 60); //使用时间初始随机数 53 for(i = 0;i < 52; i++) 54 card[i] = 0; //清除扑克牌数组 55 i = 0; 56 while(i != 52) //洗牌循环 57 { 58 pos = rand() % 52; //随机数取值 0 ~ 51 59 if(card[pos] == 0) //是否未洗牌 60 { 61 push(pos); //存此张牌进栈 62 card[pos] = 1; //设置此张牌洗过 63 i++; //下一张牌 64 } 65 } 66 printf(" 1 2 3 4\n"); 67 printf("=============================\n"); 68 for(i = 0;i < 5;i++) //发牌给四人的循环 69 { 70 temp = pop(); // 取出栈数据 71 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 72 temp = pop(); 73 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 74 temp = pop(); 75 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 76 temp = pop(); 77 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 78 printf("\n"); 79 80 } 81 return 0; 82 }
用链表创建栈
1 #include <time.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 struct stack_node 6 { 7 int data; 8 struct stack_node *next; 9 }; 10 11 typedef struct stack_node stack_list; 12 typedef stack_list* link; 13 14 link stack = NULL; 15 16 int push(int value) 17 { 18 link new_node; 19 20 new_node = (link)malloc(sizeof(stack_list)); 21 if(!new_node) 22 { 23 printf("内存分配失败!\n"); 24 return -1; 25 } 26 new_node->data = value; 27 new_node->next = stack; 28 stack = new_node; 29 } 30 31 int pop() 32 { 33 link top; 34 int temp; 35 36 if(stack != NULL) 37 { 38 top = stack; 39 stack = stack->next; 40 temp = top->data; 41 free(top); 42 return temp; 43 } 44 else 45 return -1; 46 } 47 48 49 int empt() 50 { 51 if(stack == NULL) 52 { 53 return 1; 54 } 55 else 56 return 0; 57 } 58 59 int main() 60 { 61 int card[52]; 62 int pos; 63 int i,temp; 64 long temptime; 65 srand(time(&temptime)%60); 66 for(i = 0;i < 52;i++) 67 card[i] = 0; 68 i = 0; 69 while(i != 52) 70 { 71 pos = rand()%52; 72 if(card[pos] == 0) 73 { 74 push(pos); 75 card[pos] = 1; 76 i++; 77 } 78 } 79 printf(" 1 2 3 4\n"); 80 printf("=============================\n"); 81 for(i = 0;i < 5;i++) //发牌给四人的循环 82 { 83 temp = pop(); // 取出栈数据 84 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 85 temp = pop(); 86 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 87 temp = pop(); 88 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 89 temp = pop(); 90 printf(" [%c%2d] ",temp /13 + 3,temp % 13 +1); 91 printf("\n"); 92 93 } 94 return 0; 95 }
修改程序将栈指针当做参数输入栈操作函数push() 和pop(),可以同时处理两个以上的栈
1 //这个程序将两个数组的内容分别存入个别的栈, 2 //完成后将两个栈元素全部取出并打印 3 #include <time.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 struct stack_node 8 { 9 int data; 10 struct stack_node *next; 11 }; 12 13 typedef struct stack_node stack_list; 14 typedef stack_list* link; 15 16 link stack1 = NULL; 17 link stack2 = NULL; 18 19 link push(link stack,int value) 20 { 21 link new_node; 22 23 new_node = (link)malloc(sizeof(stack_list)); 24 if(!new_node) 25 { 26 printf("内存分配失败!\n"); 27 return NULL; 28 } 29 new_node->data = value; 30 new_node->next = stack; 31 stack = new_node; 32 } 33 34 link pop(link stack,int *value) 35 { 36 link top; 37 38 if(stack != NULL) 39 { 40 top = stack; 41 stack = stack->next; 42 *value = top->data; 43 free(top); 44 return stack; 45 } 46 else 47 *value = -1; 48 } 49 50 51 int empty(link stack) 52 { 53 if(stack == NULL) 54 { 55 return 1; 56 } 57 else 58 return 0; 59 } 60 61 int main() 62 { 63 int list1[6] = {1,2,3,4,5,6}; 64 int list2[6] = {7,6,9,4,3,0}; 65 int i,temp; 66 67 for(i = 0;i < 6;i++) 68 { 69 stack1 = push(stack1,list1[i]); 70 stack2 = push(stack2,list2[i]); 71 } 72 printf("原来的数组顺序(1): "); 73 for(i = 0;i < 6;i++) 74 printf("[%d]",list1[i]); 75 printf("\n"); 76 printf("栈取出的顺序(1): "); 77 while(!empty(stack1)) 78 { 79 stack1 = pop(stack1,&temp); 80 printf("[%d]",temp); 81 } 82 printf("\n"); 83 printf("原来的数组顺序(2): "); 84 for(i = 0;i < 6;i++) 85 printf("[%d]",list2[i]); 86 printf("\n"); 87 printf("栈取出的顺序(2): "); 88 while(!empty(stack2)) 89 { 90 stack2 = pop(stack2,&temp); 91 printf("[%d]",temp); 92 } 93 printf("\n"); 94 return 0; 95 }