當前位置:首頁 » 編程語言 » c語言如何構建一個空棧
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言如何構建一個空棧

發布時間: 2022-09-14 11:57:46

c語言版數據結構 空棧的構造

#include<stdio.h>
#include<stdlib.h>
typedefstructnode
{
intdata;
structnode*next;
}SNODE;
voidpush(SNODE*top,intx);
intpop(SNODE*top);
intmain()
{
inta,b,c,d;
SNODE*top;
top=(SNODE*)malloc(sizeof(SNODE));
top->next=top;
printf("pleaseinputtheenterelement:a,b,c,d ");
scanf("%d%d%d%d",&a,&b,&c,&d);
push(top,a);
push(top,b);
printf("outelementis:n");
printf("%d ",pop(top));
printf("%d ",pop(top));
push(top,c);
push(top,d);
printf("%d ",pop(top));
printf("%d",pop(top));
return0;
}
voidpush(SNODE*top,intx)
{
SNODE*new;
new=(SNODE*)malloc(sizeof(SNODE));
if(new==NULL)
{
printf("thestackisfull ");
exit(1);
}
else
{
new->data=x;
new->next=top->next;
top->next=new;
}
}
intpop(SNODE*top)
{
intx;
SNODE*p;
if(top->next==top)
{
printf("thestackisempty! ");
exit(1);
}
else
{
p=top->next;
x=p->data;
top->next=p->next;
free(p);
}
//printf("%d ",x);
returnx;
}

② c語言版數據結構 空棧的構造

棧的本意是一個數組,裡面存取數據的方式是先進後出。因此,你需要一個cusor來指定當前的棧頂(可能你使用top實現的),你可能還需要當前存放了多少數據進棧了,棧是否空、滿,因此你還需要一個int變數計算棧元素個數。沒push+1,沒pop
-1。你完全不需要成員stacksize,還有你需要一個棧元素個數的計數器。另外你不需要將形參由引用該為指針,反而降低效率!

③ 用C語言實現棧的操作,包括創建空棧,PUSH,和POP。用標准C,就是能在TC上運行的。

#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef char SElemtype;
typedef struct
{
SElemtype *base;
SElemtype *top;
int stacksize;
}SqStack;

int InitStack(SqStack &S);
int Push(SqStack &S, SElemtype e);
int GetTop(SqStack S, SElemtype &e);
int Pop(SqStack &S, SElemtype &e);

int main()
{
SElemtype e, f = 0, g = 0;
int c;

SqStack S;
if (!InitStack(S))
return 0;

printf("棧已初始化好,輸入一個數據入棧.\n");
scanf("%c", &e);
if (!Push(S, e))
return 0;

if (!GetTop(S, f))
return 0;
printf("棧頂元素是:%c\n", f);

if (!Pop(S, g))
return 0;
printf("棧頂元素出棧,它是:%c\n", g);

scanf("%d", &c);
return 1;
}

int InitStack(SqStack &S)
{
S.base = (SElemtype *)malloc(STACK_INIT_SIZE * sizeof (SElemtype));
if (!S.base)
return 0;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return 1;
}

int Push(SqStack &S, SElemtype e)
{
if (S.top - S.base == S.stacksize)
{
S.base = (SElemtype *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof (SElemtype));
if (!S.base)
return 0;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return 1;
}

int GetTop(SqStack S, SElemtype &e)
{
if (S.top == S.base)
return 0;
e = *(S.top - 1);
return 1;
}

int Pop(SqStack &S, SElemtype &e)
{
if (S.top == S.base)
return 0;
e = *--S.top;
return 1;
}

我學這本書時寫的,剛好給你。

針對補充問題:
&不是偽代碼,是C++的傳引用,你看的那本書上都是這樣用的。
樓上的順序棧實質就是一個數組。。
TC不能建C++項目嗎? 不能的話你還是裝個VC吧,你若聽了老師的話就應該知道在這里傳引用是什麼意思,且看下面:
int InitStack(SqStack &S);
int Push(SqStack &S, SElemtype e);
int GetTop(SqStack S, SElemtype &e);
int Pop(SqStack &S, SElemtype &e);
有沒有發現GetTop()的參數沒有使用&S,因為它對S只讀,不改變S。
如果不使用傳引用的話,那麼InitStack(),Push()等將要返回一個S,若要保存還要把它賦給另一個SqStack類型的變數S1,這樣做浪費時間還浪費空間,且之前的S也就沒用了,如果反復地調用Push()的話,這樣的浪費可想而知。
所以,為了節省時間跟空間,以及按大多數情況下的需要,我們還是得始終只用一個S保存我們要的數據。
至於傳引用是什麼意思,跟指針有點像,你若不想翻書就再補充問題吧。。

④ C語言構造空棧為什麼要在S前加一個&

這里涉及到C/C++中函數調用時的參數傳遞。一般來說,都是值得傳遞,形參的值的改變不影響實參的值。
但在某些情況下,希望函數調用中的形參改變,實參也跟著改變。就像構造一個空棧,此時表示這個棧的數據結構剛被建立起來,原來棧不存在,現在被建立了,改變了,而按通常的方式是不行的。
C++提供了一種被稱為引用的參數傳遞方式,就能實現形參改變實參也跟著改變的效果,也就是問題中說的前面加&的情況。

⑤ c語言 棧的操作

#include
#include

#define Max 100

typedef char T;

typedef struct MyStack
{
T aa[Max];
unsigned int p;

} stack;

//創建空棧
stack* createEmptyStack()
{
stack* st = (stack *)malloc(sizeof(stack));
int i=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
return st;
};

//棧判空
int isEmpty(const stack* st)
{
if(st->p==0) return 1;
else return 0;
};

//求棧的大小
unsigned int size(const stack* st)
{
return st->p;
};

//push操作
void push(stack* st,const T a)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("棧滿\n");
st->p--;
return;
}
st->aa[st->p]=a;
};

//pop操作
T pop(stack* st)
{
if(isEmpty(st))
{
printf("棧空");
return NULL;
}
char t=st->aa[st->p];
st->p=st->p-1;
printf("%c ",t);
return t;
};

//棧銷毀
void destroy(stack* st)
{
free(st);
};

int main()
{

stack* st = createEmptyStack();
if(isEmpty(st)) printf("MyStack is empty\n");
else printf("MyStack is not empty\n");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d\n",size(st));
while(!isEmpty(st)) pop(st);
destroy(st);
system("pause");
return 0;
}

⑥ 如何用C語言創建一個鏈棧,並進行操作

給題主一個包含建棧、入棧、出棧的代碼吧

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#defineelemTypeint/*元素類型*/
#definestatusint
#defineOVERFLOW-1
#defineERROR0
#defineOK1

typedefstructsNode{
elemTypedata;
structsNode*next;
}sNode;

typedefstruct{
sNode*top;/*棧頂指針*/
intlength;/*棧中元素個數*/
}*SLink;

/*初始化*/
/*操作結果:構造一個空的棧S*/
voidinitStack(SLink*S){
(*S)->top=NULL;/*棧頂指針的初始值為空*/
(*S)->length=0;/*棧中元素個數為0*/
}

/*入棧*/
/*操作結果:棧S插入元素e*/
statuspush(SLinkS,elemTypee){
sNode*p;
p=(sNode*)malloc(sizeof(structsNode));/*建立新節點*/
if(!p)/*內存分配失敗*/
exit(OVERFLOW);
p->data=e;
p->next=S->top;/*鏈接到原來的棧頂*/
S->top=p;/*移動棧頂指針*/
++S->length;/*棧的長度增加*/

returnOK;
}

/*出棧*/
/*操作結果:刪除棧S棧頂元素,並由e返回其值*/
statuspop(SLinkS,elemType*e){
sNode*q;
if(!S->top)
returnERROR;
else{
*e=S->top->data;/*返回棧頂元素*/
q=S->top;
S->top=S->top->next;/*修改棧頂指針*/
--S->length;/*棧的長度減少*/
free(q);/*釋放被刪除的元素空間*/
returnOK;
}
}

/*輸出棧*/
statusstackPrint(SLinkS){
sNode*p=S->top;/*p指向棧頂*/
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
putchar(' ');
returnOK;
}

intmain(void){
SLinkS;
elemTypee;
elemTypea=1,b=2,c=3,d=4;

initStack(&S);

/*入棧若干元素*/
push(S,a);
push(S,b);
push(S,c);
push(S,d);

puts("入棧4個元素,此時棧內容為:");
stackPrint(S);

/*出棧*/
pop(S,&e);

puts("出棧1個元素,此時棧內容為:");
stackPrint(S);

getch();/*屏幕暫留*/
return0;
}

運行結果

⑦ C語言中如何初始化棧 每個元素均為空

<1>如果順序棧的定義如下
typedef struct
{datatype data[stacksize];
int top;
}SeqStack;
---則置空棧的操作如下
void InitStack(seqstack *s)
{s->top=-1;
}

<2>如果鏈棧的定義如下
typedef struct node
{datatype data;
struct node *next;
}LinkStack;
---則置空棧的操作如下
void InitStack(linkstack *s)
{s->top=null;
}

⑧ c語言建棧

#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0

typedef char ElemType;
typedef int Status;

#define STACK_INIT_SIZE 100 //存儲空間初始分配量
#define STACKINCREMENT 10 //存儲空間分配增量

typedef struct
{
ElemType *base; //在棧構造和銷毀之後,base的值為NULL
ElemType *top; //棧頂指針
int stacksize; //當前已分配的存儲空間,以元素為單位
}SqStack;

// 構造一個空棧S

Status InitStack(SqStack &S)
{
S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base) //存儲分配失敗
exit (OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack

/////////////////// 若棧不空,則用e返回S的棧頂元素,並返回OK;否則返回ERROR /////

Status GetTop(SqStack S,ElemType &e)
{
if(S.top==S.base)
return ERROR;
e=*(S.top-1);
return OK;
}//GetTop

////////////////// 插入元素e為新的棧頂元素 /////////////////////////////////////

Status Push(SqStack &S,ElemType e)
{
if(S.top-S.base>=S.stacksize) //棧滿,追加存儲空間
{
S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) * sizeof(ElemType));
if(!S.base)
exit (OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push

////////////////// 若棧不空,則刪除S的棧頂元素,用e返回其值,並返回OK;否則返回ERROR

Status Pop(SqStack &S,ElemType &e)
{
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}//Pop

////////// main() //////////////////////////////
void main()
{
int i;
char ch,e,c;
SqStack S;
InitStack(S);
printf("1.Push\t2.Pop\t3.GetTop\t4.exit\n");
while(1)
{
printf("請選擇:");
scanf("%d",&i);
c=getchar(); //*****接受回車符******
switch (i)
{
case 1:
printf("請輸入要插入的元素:");
scanf("%c",&ch);
Push(S,ch);
break;
case 2:
printf("彈出棧頂元素:");
Pop(S,e);
printf("%c\n",e);
break;
case 3:
printf("取棧頂元素:");
GetTop(S,e);
printf("%c\n",e);
break;
case 4:
exit(0);
default:
printf("ERROR!Please Reput A Number\n");
}
}
}

⑨ 求教:C語言怎麼申請一個棧,棧的容量有多大

棧可以直接用數組實現的,也可以用鏈表實現,本質上就是可以在一端點插入和刪除的數據結構
數組和鏈表都是非常適合的媒介
數組實現就用malloc申請一塊內存,當數組用即可
鏈表就是頭插和頭刪兩種操作,每次也用malloc申請一個節點的內存
(頻繁malloc是非常有失效率的,建議一般情況用數組版,或者合理使用內存池)

⑩ 用c語言實現棧的建立

#include <stdio.h>
#include <stdlib.h>

typedef struct _stack {
int size;
int* base;
int* sp;
} stack;

void init(stack* s, int n)
{
s->base = (int*)malloc(sizeof(int)*n);
s->size = n;
s->sp = s->base;
}

int push(stack* s, int val)
{
if(s->sp - s->base == s->size) {
puts("overflow");
exit(1);
}
return *s->sp++ = val;
}

int pop(stack* s)
{
if(s->sp == s->base) {
puts("underflow");
exit(2);
}
return *--s->sp;
}

int empty(stack* s)
{
return s->sp == s->base;
}

void clean(stack* s)
{
if(s->base)
free(s->base);
}

int main(void)
{
stack s;
int i;
init(&s, 100);
for(i = 0; i < 10; ++i)
printf("%d ", push(&s, i));
putchar('\n');
while(!empty(&s))
printf("%d ", pop(&s));
clean(&s);
return 0;
}