c語言數據結構棧
『壹』 c語言數據結構 棧的初始化
不知道你寫的到底是什麼,看不懂!
#define MAXSIZE 60
main()
{
char zhan[MAXSIZE],ch,i;
int top=0;
scanf("%c",&ch);
while(ch!='#')
{zhan[top++]=ch; //入棧
scanf("%c",&ch);}
top--;
while(top>=0)
printf("%c",zhan[top--]); //出棧
getch();
}
以上為順序表存儲版!
鏈式存儲
定義 結構體棧元權素
struct vertype DNode
{ char data;
DNode *next;}Node;}
自己多想想就知道了,和上面的大同小異。
『貳』 數據結構實驗(用c語言寫) 棧的基本操作
//順序棧
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
typedef int ElemType;
int InitStack(SqStack &S) //為棧S分配存儲空間,並置S為空棧
{
int size = STACK_INIT_SIZE;
S.base=(int *)malloc(size*sizeof(ElemType));
if(!S.base)
return 0;
S.top=S.base; //置棧S為空棧
S.stacksize=STACK_INIT_SIZE;
return 1;
}
int GetTop(SqStack S,int &e) //若棧不空,則用e返回S的棧頂元素
{
if(S.top==S.base) return 0;
e=*(S.top-1);
return 1;
}
int Push(SqStack &S, int e) /*進棧函數,將e插入棧S中,並使之成為棧頂元素*/
{ if(S.top-S.base>=S.stacksize) /*棧滿,追加存儲空間*/
{
int stackinvrement = STACKINCREMENT;
S.base=(ElemType *) realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType));
if(!S.base)
return 0; /*存儲分配失敗*/
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return 1;
}
int Pop(SqStack &S,int &e)/*出棧函數,若棧S不空,則刪除S的棧頂元素,用e返回其值*/
{ if(S.top==S.base) return 0;
e=*--S.top;
return 1;
}
void OutputStack(SqStack &S)
{int *q;
q=S.top-1;
for(int i=0;i<S.top-S.base;i++)
{
printf("%3d ",*q);q--;}
}
void main()
{
int a,b,c ;
char m;
SqStack s;
InitStack(s);
printf("請輸入要進棧的元素個數是:");
scanf("%d",&a);
printf("\n請輸入要進棧的%d個元素:",a);
for(b=0;b<a;b++) {
scanf("%d",&c);
Push(s,c); }
do { printf("\n");
printf("*********** 1.輸出棧的元素**********\n");
printf("*********** 2.取棧頂元素************\n");
printf("*********** 3.刪除棧頂元素**********\n");
printf("*********** 4.退出程序**********\n");
printf("\n請選擇一個字元:");
getchar();
scanf("%c",&m);
switch(m) {
case '1': printf("\n輸出的棧為:");
OutputStack(s);
break;
case '2': GetTop(s,c);
printf("\n棧頂元素為:%d",c);
printf("\n輸出的棧為:");
OutputStack(s);
break;
case '3': Pop(s,c);
printf("\n刪除的棧頂元素:%d",c);
printf("\n輸出的棧為:");
OutputStack(s);
printf("\n");
break;
case '4':break;
default: printf("輸入的數字有錯,請重新選擇!\n"); break;
}
}while(m!='4');
}
//鏈棧
#include<stdio.h>
#include<stdlib.h>
typedef struct SNode
{
int data;
struct SNode *next;
}SNode,*LinkStack;
LinkStack top;
LinkStack PushStack(LinkStack top,int x) //入棧
{
LinkStack s;
s=(LinkStack)malloc(sizeof(SNode));
s->data=x;
s->next=top;
top=s;
return top;
}
LinkStack PopStack(LinkStack top) //退棧
{
LinkStack p;
if(top!=NULL)
{
p=top;
top=top->next;
free(p);
printf("退棧已完成\n");
return top;
}
else printf("棧是空的,無法退棧!\n"); return 0;
}
int GetStackTop(LinkStack top) //取棧頂元素
{
return top->data;
}
bool IsEmpty()//bool取值false和true,是0和1的區別,bool只有一個位元組,BOOL為int型,bool為布爾型
{
return top==NULL ? true:false;
}
void Print()
{
SNode *p;
p=top;
if(IsEmpty())
{
printf("The stack is empty!\n");
return;
}
while(p)
{
printf("%d ", p->data);
p=p->next;
}
printf("\n");
}
void main()
{
int x,a,b;
char m;
do { printf("\n");
printf("###############鏈棧的基本操作##################\n");
printf("××××××××1.置空棧××××××××××\n");
printf("××××××××2.進棧×××××××××××\n");
printf("××××××××3.退棧×××××××××××\n");
printf("××××××××4.取棧頂元素××××××××\n");
printf("××××××××5.退出程序×××××××××\n");
printf("##############################################\n");
printf("\n請選擇一個字元:");
scanf("%c",&m);
switch(m){
case '1':
top=NULL;
printf("\n棧已置空!");
break;
case '2':
printf("\n請輸入要進棧的元素個數是:");
scanf("%d",&a);
printf("\n請輸入要進棧的%d個元素:",a);
for(b=0;b<a;b++) {
scanf("%d",&x);
top=PushStack(top,x); }
printf("進棧已完成!\n");
printf("\n輸出棧為:");
Print();
break;
case '3':
printf("\n操作之前的輸出棧為:");
Print();
top=PopStack(top);
printf("\n操作過後的輸出棧為:");
Print();
break;
case '4':
printf("\n輸出棧為:");
Print();
if(top!=NULL)
printf("\n棧頂元素是:%d\n",GetStackTop(top));
else
printf("\n棧是空的,沒有元素!");
break;
case '5':break;
default:
printf("\n輸入的字元不對,請重新輸入!");
break;
}
getchar();
}while(m!='5');
}
『叄』 C語言數據結構實現鏈棧的入棧、出棧、刪除與插入
1、棧(stack)又名堆棧,它是一種運算受限的線性表。
其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧、入棧或壓棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素;從一個棧刪除元素又稱作出棧或退棧,它是把棧頂元素刪除掉,使其相鄰的元素成為新的棧頂元素。
2、常式:
#include<stdio.h>
#include<stdlib.h>
#defineMax100
typedefcharT;
typedefstructMyStack
{
Taa[Max];
unsignedintp;
}stack;
//創建空棧
stack*createEmptyStack()
{
stack*st=(stack*)malloc(sizeof(stack));
inti=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
returnst;
};
//棧判空
intisEmpty(conststack*st)
{
if(st->p==0)return1;
elsereturn0;
};
//求棧的大小
unsignedintsize(conststack*st)
{
returnst->p;
};
//push操作
voidpush(stack*st,constTa)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("棧滿 ");
st->p--;
return;
}
st->aa[st->p]=a;
};
//pop操作
Tpop(stack*st)
{
if(isEmpty(st))
{
printf("棧空");
returnNULL;
}
chart=st->aa[st->p];
st->p=st->p-1;
printf("%c",t);
returnt;
};
//棧銷毀
voiddestroy(stack*st)
{
free(st);
};
intmain()
{
stack*st=createEmptyStack();
if(isEmpty(st))printf("MyStackisempty ");
elseprintf("MyStackisnotempty ");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d ",size(st));
while(!isEmpty(st))pop(st);
destroy(st);
system("pause");
return0;
}
『肆』 C語言版的數據結構中,棧存儲結構是什麼
棧(stack)在計算機科學中是限定僅在表尾進行插入或刪除操作的線性表。
棧是一種回數據結構 ,是只能在答某一端插入和刪除的特殊線性表 。它按照後進先出的原則存儲數據,先進入的數據被壓入棧底,最後的數據在棧頂,需要讀數據的時候從棧頂開始彈出數據(最後一個數據被第一個讀出來)。
『伍』 c語言版數據結構 空棧的構造
棧的本意是一個數組,裡面存取數據的方式是先進後出。因此,你需要一個cusor來指定當前的棧頂(可能你使用top實現的),你可能還需要當前存放了多少數據進棧了,棧是否空、滿,因此你還需要一個int變數計算棧元素個數。沒push+1,沒pop -1。你完全不需要成員stacksize,還有你需要一個棧元素個數的計數器。另外你不需要將形參由引用該為指針,反而降低效率!
『陸』 數據結構C語言 棧
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct StackRecord;
typedef struct StackRecord *Stack;
typedef char ElementType;
#define EmptyTOS -1
#define MinStackSize 5
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack== S->Capacity;
}
int IsEmpty(Stack S)
{
return S->TopOfStack==EmptyTOS;
}
Stack CreateStack(int MaxElements )
{
Stack S;
/* 1 */ if(MaxElements<MinStackSize)
/* 2 */ {
printf("Stack size is too small");
exit(0);
}
/* 3 */ S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */ if(S==NULL)
/* 5 */ {
printf("Out of space!!l");
exit(0);
}
/* 6 */ S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */ if(S->Array==NULL)
/* 8 */ {
printf("Out of space!!l");
exit(0);
}
/* 9 */ S->Capacity=MaxElements;
/*10*/ MakeEmpty(S);
/*11*/ return S;
}
void DisposeStack(Stack S)
{
if(S!=NULL)
{
free(S->Array);
free(S);
}
}
void Push(ElementType X, Stack S)
{
if( IsFull(S) )
{
printf("Full stack");
exit(0);
}
else
S->Array[S->TopOfStack++]=X;//將元素X放入棧中
}
ElementType Top(Stack S)
{
if(!IsEmpty(S) )
return 0;//返回棧頂元素
/* Return value used to avoid warning */
}
void Pop(Stack S)
{
if(IsEmpty(S) )
{
printf("Empty stack");
exit(0);
}
else
S->TopOfStack--;
}
ElementType TopAndPop(Stack S)
{
if(!IsEmpty(S) )
return S->Array[S->TopOfStack--];
}
void main(void)
{
char c1,c2;
Stack S;
int i,MAX;
scanf("%d",&MAX);
fflush(stdin);
S = CreateStack(MAX);//這個函數都沒有調用過!
for(i=0;i<MAX;i++)
{
scanf("%c",&c1);//通過鍵盤輸入為變數賦值
Push(c1,S);//將c1壓入棧中
}
printf("S->TopOfStack: "); //輸出棧頂元素
Pop(S); //棧頂元素出棧
for(i=0;i<MAX;i++)
printf("%c",TopAndPop(S));// 輸出棧序列
}
這個棧寫的不規范,pop那個函數都沒有元素返回只是做了簡單的把指針減一
改的這個可以運行了,但是運行結果就有點問題!
不太習慣你這個設計棧的方法,你有了個pop為什麼還弄個TopAndPop(Stack S)
『柒』 數據結構 用C語言編程實現進棧出棧
希望如下對你有用:
/*棧的基本操作*/
#
define
stacksize
100
/*定義棧的最大存儲空間*/
#
define
LEN
sizeof(struct
stack)
static
size=0;
struct
stack
{
int
data;
int
*top[stacksize];
};
struct
stack
*sqstack;
struct
stack
*s;
static
e;
int
push()
/*將元素壓入棧*/
{
if
(size<=stacksize)
*
sqstack->top[size++]=e;
else
printf
("
the
stack
is
full:");
}
int
pop(struct
stack
*sqstack,int
location)
/*元素出棧*/
{
e=*(sqstack->top[location]);
return
(e);
}
main()
{
int
n,i,t,x=0;
int
element;
printf
(
"\n
create
the
stack
first
:");
scanf
("%d
",&n);
for
(i=1;i<=n;i++)
{
scanf
("%d",&e);
push
(e);
}
s=sqstack;
t=size;
printf
("\n
after
pushed
,
the
sqstack
is
:");
while
(t>=0)
{
*s->top[t]=*sqstack->top[t];
t--;
}
t=size;
while
(t!=0)
{
t--;
e=pop(s,t);
printf
("
%d->",e);
}
printf
("\n
which
element
you
want
to
pop
:");
scanf
("%d",&element);
while
(size!=0)
{
e=pop(sqstack,size--);
if
(element==e)
{
printf
("\n
%d
is
poped",element);
x=1;
}
}
if(x==0)
printf
("\n
%d
is
not
found
in
the
sqstack.\n",element);
}
『捌』 數據結構 C語言版,《棧的實現》
#include"head.h"
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstruct
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack&S)
{
S.base=newSElemType[STACK_INIT_SIZE];
if(!S.base)returnOVERFLOW;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusDestroyStack(SqStack&S)
{
S.top=NULL;
S.base=NULL;
delete[]S.base;
S.stacksize=0;
returnOK;
}
intStackEmpty(SqStacks)
{
if(s.top==s.base)return1;
elsereturn0;
}
StatusGetTop(SqStack&s,SElemType&e)
{
if(s.top==s.base)returnERROR;
e=*(s.top-1);
returnOK;
}
intStackLength(SqStacks)
{
returns.top-s.base;
}
StatusClearStack(SqStack&S)
{
S.top=S.base;
returnOK;
}
StatusPush(SqStack&s,SElemTypee)
{
if(s.top-s.base>=s.stacksize)
returnOVERFLOW;
*s.top++=e;
returnOK;
}
StatusPop(SqStack&s,SElemType&e)
{
if(s.top==s.base)returnERROR;
e=*--s.top;
returnOK;
}
StatusStackTraverse(SqStacks,Status(*visit)(SElemTypec))//這個函數最好不要用,因為它已經破壞了棧的特性
{
while(s.top>s.base)
visit(*s.base++);
cout<<endl;
returnOK;
}
Statusvisit(SElemTypec)
{
cout<<c<<"";
returnOK;
}
voidmain()//為進制轉換的函數
{
SqStackS;
SElemTypeN;
SElemTypee;
InitStack(S);
cout<<"inputthenumber:";
cin>>N;
while(N)
{
Push(S,N%8);
N=N/8;轉換為八進制的數
}
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e;
}
cout<<endl;
『玖』 表達式求值(C語言 數據結構棧)急求。。。。。
其實不用這樣子的,要將表達式的值賦值就行了。
『拾』 C語言數據結構關於棧的題
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;
typedef struct node{
DataType data;
struct node * next;
}Stack;
Stack* CreateStack(); //創建棧
void StackEmpty(Stack* ); //清空棧
void DestoryStack(Stack*); //撤銷(刪除)棧
int IsEmpty(Stack*); //判空
int PushStack(Stack*, DataType); //入棧
int PopStack(Stack*); //出棧
DataType GetTopElement(Stack*); //取棧頂元素
Stack* CreateStack()
{
Stack *stack = (Stack*)malloc(sizeof(Stack));
if(NULL != stack)
{
stack->next = NULL;
return stack;
}
printf("out of place.\n");
return NULL;
}
//清空棧
void StackEmpty(Stack* stack)
{
while(!IsEmpty(stack))
{
PopStack(stack);
}
printf("now stack is empty. \n");
}
//撤銷棧
void DestoryStack(Stack* stack)
{
free(stack);
exit(0);
}
int IsEmpty(Stack* stack)
{
return (stack->next == 0);
}
//入棧,成功返回1,失敗返回0, 把元素 data 存入 棧 stack 中
int PushStack(Stack* stack, DataType data)
{
Stack* newst = (Stack*)malloc(sizeof(Stack));
if(NULL != newst)
{
newst->data = data;
newst->next = stack->next; //s->next = NULL;
stack->next = newst;
return 1;
}
printf("out of place PushStack.\n");
return 0;
}
/*
出棧,成功返回1,失敗返回0,出棧不取出元素值,只是刪除棧頂元素。
如出棧要實現,取出元素值,並釋放空間,可結合取棧頂元素函數做修改,這里不再給出。
*/
int PopStack(Stack* stack)
{
Stack* tmpst;
if(!IsEmpty(stack))
{
tmpst = stack->next;
stack->next = tmpst->next;
free(tmpst);
return 1;
}
return 0;
}
//取棧頂元素,僅取出棧頂元素的值,取出之後,該元素,任然存在棧中。成功返回元素值,失敗輸出提示信息,並返回 -1
DataType GetTopElement(Stack* stack)
{
if(!IsEmpty(stack))
{
return stack->next->data;
}
printf("stack is empty GetTopElement.\n");
return -1;
}
int main()
{
Stack* stack = CreateStack();
char str[200];
printf("請輸入字元串:");
scanf("%s", str);
int num = 0;
for (int i = 0; i < strlen(str); i++) {
if (!IsEmpty(stack) && GetTopElement(stack) == '(' && str[i] == ')') {
PopStack(stack);
num++;
} else {
PushStack(stack, str[i]);
}
}
if (!IsEmpty(stack)) {
num = -1;
}
printf("匹配結果:%d", num);
DestoryStack(stack);
return 1;
}