栈:一种抽象的数据结构,一种只允许在一端进行插入和删除的线性表,在该表中只允许进行插入和删除的一端称为栈顶(top),
另一端称为栈底(bottom)。
栈的插入操作称为入栈(push)
栈的删除操作称为出栈(pop)
栈中没有元素时,称为空栈
有如下特性:
1)只能从栈的顶端访问数据
2)数据以后进先出(Last In, First Out, LIFO)的策略进行数据的访问
二、使用链表结构实现栈
代码输出如下:
另一端称为栈底(bottom)。
栈的插入操作称为入栈(push)
栈的删除操作称为出栈(pop)
栈中没有元素时,称为空栈
有如下特性:
1)只能从栈的顶端访问数据
2)数据以后进先出(Last In, First Out, LIFO)的策略进行数据的访问
创建栈有如下两种方法,一种是使用数组结构,另一种是使用链表结构,下面分别介绍其实现
以下以扑克牌洗牌和发牌为例来说明栈的出栈和入栈的概念。
一副扑克牌,有52张,分为黑桃、红心、方块和梅花四种花色。将该副牌重新洗牌后入栈,然后出栈分给四个人。以下通过两种方法实现
一、使用数组结构实现栈
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXSTACK 100
typedef struct stack
{
int data[MAXSTACK];
int top;
}stack_array;
//stack_array* S;
//初始化栈
int init_stack(stack_array * S)
{
S->top = -1;
return 0;
}
//入栈
int push(stack_array* S, int value)
{
if(S->top >= MAXSTACK)
{
printf("栈满!\n");
return -1;
}
S->top++;
S->data[S->top] = value;
return 0;
}
//出栈
int pop(stack_array* S)
{
int temp;
if(S->top < 0)
{
printf("栈空!\n");
return -1;
}
temp = S->data[S->top];
S->top--;
return temp;
}
//判断栈是否空
int empty(stack_array* S)
{
if(S->top == -1)
return -1;
else
return 0;
}
//主函数
int main()
{
stack_array* S = (stack_array*)malloc(sizeof(stack_array));
int card[52];
int pos;
int i, temp;
time_t temptime;
srand(time(&temptime) % 60);
for(i = 0; i < 52; i++)
{
card[i] = 0;
}
init_stack(S);
//如下这段程序是洗牌
i = 0;
while(i != 52)
{
pos = rand() % 52;
if(card[pos] == 0)
{
push(S, pos);
card[pos] = 1;
i++;
}
}
//如下这段程序是发牌给四个人
printf("1 2 3 4\n");
printf("======================\n");
for(i = 0; i < 13; i++)
{
temp = pop(S);
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop(S);
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop(S);
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop(S);
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
printf("\n");
}
system("pause");
return 0;
}
二、使用链表结构实现栈
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct stack
{
int data;
struct stack* next;
}stack_list;
stack_list* S;
//初始化栈
void init_stack(stack_list* S)
{
S = NULL;
}
//入栈
int push(int value)
{
stack_list* new_node = (stack_list*)malloc(sizeof(stack_list));
if(!new_node)
{
printf("memory allocation fail!\n");
return -1;
}
new_node->data = value;
new_node->next = S;
S = new_node;
return 0;
}
//出栈
int pop()
{
stack_list* top;
int temp;
if(S != NULL)
{
top = S;
S = S->next;
temp = top->data;
free(top);
return temp;
}
else
return -1;
}
int empty()
{
if(S == NULL)
return 1;
else
return 0;
}
int main()
{
int card[52];
int pos;
int i, temp;
time_t temptime;
srand(time(&temptime) % 60);
printf("%ld\n", temptime);
init_stack(S);
for(i = 0; i < 52; i++)
{
card[i] = 0;
}
//如下这段程序是洗牌
i = 0;
while(i != 52)
{
pos = rand() % 52;
if(card[pos] == 0)
{
push(pos);
card[pos] = 1;
i++;
}
}
//如下这段程序是发牌给四个人
printf("1 2 3 4\n");
printf("======================\n");
for(i = 0; i < 13; i++)
{
temp = pop();
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop();
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop();
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
temp = pop();
printf("[%c%2d] ", temp / 13 + 3, temp % 13 + 1);
printf("\n");
}
system("pause");
return 0;
}
代码输出如下:
注:
标准C库中函数rand()可以生成0~RAND_MAX之间的一个随机数,其中RAND_MAX 是stdlib.h 中定义的一个整数,它与系统有关。
rand()函数没有输入参数,直接通过表达式rand()来引用;例如可以用下面的语句来打印两个随机数:
printf("Random numbers are: %i %i\n",rand(),rand());
因为rand()函数是按指定的顺序来产生整数,因此每次执行上面的语句都打印相同的两个值,所以说C语言的随机并不是真正意义上的随机。
为了使程序在每次执行时都能生成一个新序列的随机值,我们通常通过为随机数生成器提供一粒新的随机种子。函数 srand()(来自stdlib.h)可以为随机数生成器播散种子。只要种子不同rand()函数就会产生不同的随机数序列。srand()称为随机数生成器的初始化器。引自于:http://baike.baidu.com/view/3048977.htm