顺序栈,链栈,队列的区别;

1,顺序栈是静态的,就像数组一样,申请就申请一大块,链栈是动态的,就像链表一样,需要多少申请多少;

2,栈是先进后出,队列是先进先出;

3,链栈是后申请的节点的“屁股”对着原有的栈元素,队列是先申请的节点的“屁股”对着新申请的节点。

4,顺序栈的空间可以等元素都弹出去,然后单独用一个函数去销毁它,但是链表栈是弹出一个元素就把上面那个节点空间给释放了比较好。

顺序栈代码:

/***************************************************
##filename : sequence.c
##author : GYZ

##create time : 2018-10-15 16:24:29
##last modified : 2018-10-16 14:33:30
##description : NA
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10

typedef struct Stack
{
int *top;
int *bottom;
int stacksize;
} Stack;

void createStack(Stack *p_st,int size)
{
p_st->bottom = (int *)malloc(size * sizeof(int));
if(NULL == p_st->bottom)
{
perror("malloc"),exit(-1);
}
p_st->top = p_st->bottom;
p_st->stacksize = SIZE;
printf("create a stack successfully!\n");
}

void pushStack(Stack *p_st,int ele)
{
*(p_st->top) = ele;
p_st->top ++;
printf("push the element 'ele' : %d\n",ele);
}

void traverseStack(Stack *p_st)
{
if(p_st->bottom == p_st->top)
{
printf("this is an empty stack!\n");
}
else
{
int *temp = p_st->top;
while(temp > p_st->bottom)
{
temp --;
printf("elements of stack : %d\n",*temp);
}
}
}

void popStack(Stack *p_st)
{
if(p_st->top == p_st->bottom)
{
printf("this is an empty stack\n");
}
else
{
p_st->top--;
printf("pop an element: %d\n",*(p_st->top));
}
}
void destroyStack(Stack *p_st)
{
free(p_st->bottom);
p_st->bottom = NULL;
p_st->top = NULL;
p_st->stacksize = 0;
printf("destroy stack successfully!\n");
}

int main(int argc,char *argv[])
{
Stack ST;
createStack(&ST,SIZE);
pushStack(&ST,1);
pushStack(&ST,2);
pushStack(&ST,3);
pushStack(&ST,4);
pushStack(&ST,5);
pushStack(&ST,6);
pushStack(&ST,7);

traverseStack(&ST);

popStack(&ST);
popStack(&ST);
popStack(&ST);
popStack(&ST);
popStack(&ST);
popStack(&ST);
popStack(&ST);

destroyStack(&ST);
return 0;
}

链栈代码:

/***************************************************
##filename : link.c
##author : GYZ

##create time : 2018-10-16 13:04:14
##last modified : 2018-10-16 15:43:49
##description : NA
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
int data;
struct Node *next;
} Node;

typedef struct LinkStack
{
Node *top;
int count;
} LinkStack;

void initLink(LinkStack *ls)
{
ls->top = NULL;
ls->count = 0;
printf("init an new link stack !\n");
}
void pushLink(LinkStack *ls,int ele)
{
Node *temp = (Node*)malloc(sizeof(Node));
if(NULL == temp)
{
perror("malloc"),exit(-1);
}
else
{
temp->data = ele;
temp->next = ls->top;
ls->top = temp;
ls->count ++;
printf("push an element : %d\n",ele);
}
}
void traverseLink(LinkStack *ls)
{
Node *temp = ls->top;
while(NULL != temp)
{
printf("from top to bottom : %d\n",temp->data);
temp = temp->next;
}
}
void popLink(LinkStack *ls)
{
if(NULL == ls->top)
{
printf("this is an empty!\n");
}
else
{
Node *temp = ls->top;
int data = temp->data;
ls->top = temp->next;
printf("get the top element : %d\n",data);

printf("destroy the top node!\n");
free(temp);
temp = NULL;
}
}

int main(int argc,char *argv[])
{
LinkStack LS;
initLink(&LS);

pushLink(&LS,1);
pushLink(&LS,2);
pushLink(&LS,3);
pushLink(&LS,4);
pushLink(&LS,5);
pushLink(&LS,6);
pushLink(&LS,7);

traverseLink(&LS);

popLink(&LS);
popLink(&LS);
popLink(&LS);
popLink(&LS);
popLink(&LS);
popLink(&LS);
popLink(&LS);
return 0;
}