棧的基本操作
棧的儲存結構有兩種:一種是線性棧,一種是鏈式棧。下面分別是這兩種儲存結構的實現。
線性棧:
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct SqStack
{
ElemType *base;
int top;
int stacklength;
}SqStack;
void InitSqStack(SqStack *S)
{
S->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if (!S->base)
exit(1);
S->top = 0;
S->stacklength = STACK_INIT_SIZE;
}
void Push_SqStack(SqStack *S,ElemType e)
{
if (S->top == S->stacklength){
S->base = (ElemType*)realloc(S->base,(S->stacklength
+STACK_REALLOCATION)*sizeof(ElemType));
if (!S->base)
exit(1);
S->stacklength += STACK_REALLOCATION;
}
S->base[S->top] = e;
++S->top;
}
int Pop_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
--S->top;
*e = S->base[S->top];
return 1;
}
int GetTopElem_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
*e = S->base[S->top-1];
return 1;
}
int isSqStackEmpty(SqStack S)
{
if (S.top == 0)
return 1;
else
return 0;
}
void SqStack_test()
{
SqStack S;
InitSqStack(&S);
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_SqStack(&S,e);
printf("The out stack sequence is:\n");
while (!isSqStackEmpty(S)){
Pop_SqStack(&S,&e);
printf("%d ",e);
}
printf("\n");
}
int main()
{
SqStack_test();
return 0;
}
鏈式棧:
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct LinkStack
{
ElemType data;
struct LinkStack *next;
}StackNode,*StackPNode;
StackPNode InitLinkStack()
{
StackPNode head = (StackPNode)malloc(sizeof(StackNode));
if (!head)
exit(1);
head->next = NULL;
head->data = 0;
return head;
}
void Push_LinkStack(StackPNode head, ElemType e)
{
StackPNode node = (StackPNode)malloc(sizeof(StackNode));
if (!node)
exit(1);
node->data = e;
node->next = NULL;
node->next = head->next;
head->next = node;
}
int Pop_LinkStack(StackPNode head, ElemType *e)
{
StackPNode p;
if (!head->next)
return 0;
//StackPNode p;
//StackPNode p;
//struct LinkStack *p;
p= head->next;
*e=p->data;
head->next = p->next;
free(p);
return 1;
}
int isLinkStackEmpty(StackPNode head)
{
if (head->next)
return 0;
else
return 1;
}
int getTopElem_LinkStack(StackPNode head,ElemType *e)
{
if (head->next)
return 0;
*e = head->next->data;
return 1;
}
void LinkStack_test()
{
StackPNode head = InitLinkStack();
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_LinkStack(head,e);
printf("The out stack sequence is:\n");
while (!isLinkStackEmpty(head)){
Pop_LinkStack(head, &e);
printf("%d ", e);
}
printf("\n");
}
int main()
{
LinkQueueTest();
return 0;
}
相關文章
- 實驗四 棧和佇列的基本操作佇列
- Go 操作 Redis 的基本操作GoRedis
- 資料結構:棧的基本概念、順序棧、共享棧以及鏈棧資料結構
- Docker的基本操作Docker
- MySQL的基本操作MySql
- git的基本操作Git
- JAVA棧操作 Stack——不可不知的操作Java
- JS — 物件的基本操作JS物件
- Spring Boot的基本操作Spring Boot
- Docker映象的基本操作Docker
- Hbase shell的基本操作
- react的基本操作(1)React
- Vim命令的基本操作
- Numpy的基本操作(五)
- 陣列的基本操作陣列
- Hive表的基本操作Hive
- Python 全棧系列44 - pymongo的基本使用Python全棧Go
- MongoDB基本操作MongoDB
- webpack 基本操作Web
- mongo基本操作Go
- SQL基本操作SQL
- ElasticSearch - 基本操作Elasticsearch
- candance 基本操作
- svn基本操作
- oracle基本操作Oracle
- Hash基本操作
- python基本操作Python
- FFMPEG基本操作
- dos 基本操作
- 基本操作題
- Laravel 基本操作Laravel
- Git基本操作Git
- VSCode基本操作VSCode
- Docker基本操作Docker
- redis基本操作Redis
- linux基本操作Linux
- ElasticSearch基本操作Elasticsearch
- HBase 基本操作
- 活動(Activity)的基本操作