顺序栈和链式栈的实现
程序员文章站
2022-06-05 14:46:20
...
stack_seq.h
#pragma once
#define MaxStack 1000
typedef char TypeStack;
typedef struct SeqStack
{
TypeStack data[MaxStack];
TypeStack *top;
size_t stacksize;
}SeqStack;
void InitSeqStack(SeqStack* seqstack);
void DestoryStack(SeqStack* seqstack);
void SeqStackPush(SeqStack* seqstack, TypeStack value);
void SeqStackPop(SeqStack* seqstack);
TypeStack SeqStackTop(SeqStack* seqstack);
stack_seq.c#include<stdio.h>
#include<stdlib.h>
#include"stack_seq.h"
#define TEST_HEADER printf("===============%s=================\n",__FUNCTION__);
void InitSeqStack(SeqStack* seqstack)
{
seqstack->size=0;
seqstack->capacity=1000;
seqstack->data=(TypeStack*)malloc(seqstack->capacity*sizeof(TypeStack));
}
void DestoryStack(SeqStack* seqstack)
{
seqstack->size=0;
free(seqstack->data);
}
void increase_capacity(SeqStack* seqstack)
{
seqstack->capacity = seqstack->capacity*2+1;
TypeStack* newptr=(TypeStack*)malloc(seqstack->capacity*sizeof(TypeStack));
size_t i;
for(i=0;i<seqstack->size;i++)
{
newptr[i]=seqstack->data[i];
}
free(seqstack->data);
seqstack->data=newptr;
}
void SeqStackPush(SeqStack* seqstack, TypeStack value)
{
if(seqstack==NULL)
{
return;
}
if(seqstack->size >= seqstack->capacity)
{
increase_capacity(seqstack);
}
seqstack->data[seqstack->size]=value;
++seqstack->size;
}
void SeqStackPop(SeqStack* seqstack)
{
if(seqstack==NULL)
{
return;
}
if(seqstack->size==0)
{
printf("No Data to be Pop.\n");
return;
}
--seqstack->size;
}
int SeqStackTop(SeqStack* seqstack,TypeStack* value)
{
if(seqstack==NULL)
{
return 0;
}
if(seqstack->size==0)
{
return 0;
}
*value=seqstack->data[seqstack->size-1];
return 1;
}
void ShowStruct(SeqStack* seqstack)
{
printf("Stack->top:%c \n",seqstack->data[seqstack->size-1]);
printf("Stack->size:%d \n",seqstack->size);
printf("Stack->capacity:%d \n",seqstack->capacity);
}
void TestInit()
{
TEST_HEADER;
SeqStack seq;
InitSeqStack(&seq);
ShowStruct(&seq);
}
void TestDestory()
{
TEST_HEADER;
SeqStack seq;
InitSeqStack(&seq);
DestoryStack(&seq);
ShowStruct(&seq);
}
void TestPush()
{
TEST_HEADER;
SeqStack seq;
InitSeqStack(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
size_t i=0;
for(i=0;i<1000;i++)
{
SeqStackPush(&seq,'d');
}
ShowStruct(&seq);
}
void TestPop()
{
TEST_HEADER;
SeqStack seq;
InitSeqStack(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
size_t i=0;
for(i=0;i<1000;i++)
{
SeqStackPush(&seq,'d');
}
for(i=0;i<1004;i++)
{
SeqStackPop(&seq);
}
ShowStruct(&seq);
}
void TestTop()
{
TEST_HEADER;
SeqStack seq;
InitSeqStack(&seq);
SeqStackPush(&seq,'a');
SeqStackPush(&seq,'b');
SeqStackPush(&seq,'c');
SeqStackPush(&seq,'d');
TypeStack value;
int i=0;
i = SeqStackTop(&seq,&value);
printf("i:%d \n",i);
printf("value:%c \n",value);
}
int main()
{
TestInit();
TestDestory();
TestPush();
TestPop();
TestTop();
return 0;
}
stack_link.h
#pragma once
typedef char TypeStack;
typedef struct Stack{
TypeStack data;
struct Stack* next;
}Stack;
Stack* CreatStackNode(TypeStack value);
void InitStack(Stack** top);
void DestoryStack(Stack** top);
void StackPush(Stack** top,TypeStack value);
void StackPop(Stack** top);
TypeStack StackTop(Stack* top);
stack_link.c#include<stdio.h>
#include<stdlib.h>
#include"stack_link.h"
Stack* CreatStackNode(TypeStack value)
{
Stack* tmp=(Stack*)malloc(sizeof(Stack));
tmp->data=value;
tmp->next=NULL;
return tmp;
}
void InitStack(Stack** top)
{
if(top==NULL)
{
return;
}
*top=NULL;
}
void DestoryStack(Stack** top)
{
if(top==NULL)
{
return;
}
if(*top==NULL)
{
return;
}
Stack* to_delete=NULL;
while(*top)
{
to_delete=*top;
(*top)=(*top)->next;
free(to_delete);
}
}
void StackPush(Stack** top,TypeStack value)
{
if(top==NULL)
{
return;
}
Stack*tmp = CreatStackNode(value);
tmp->next=*top;
(*top)=tmp;
}
void StackPop(Stack** top)
{
if(top==NULL)
{
return;
}
if(*top==NULL)
{
return;
}
Stack* tmp=*top;
(*top)=(*top)->next;
free(tmp);
}
TypeStack StackTop(Stack* top)
{
if(top==NULL)
{
return 0;
}
return top->data;
}
上一篇: Flash怎么创建五边形按钮元件?