當前位置:首頁 » 編程語言 » c語言循環實現棧的簡單操作
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言循環實現棧的簡單操作

發布時間: 2022-09-12 02:52:46

1. 急!用c語言編寫個使用棧的程序,簡單點的,包含入棧,出棧等幾個基本操作就行。

就用這堆函數就可以了,不懂再追問
#include <string.h>

#define MaxSize 100
int mystack[MaxSize];/* 第0個單元保存現在的長度 */

/* 初始化函數 */
void init_stack(int* stack){
memset(stack,0,sizeof(stack));
}

/* 入棧函數 */
void push_back(int* stack,int& num){
if(stack[0]<MaxSize-1){
++stack[0];
stack[ stack[0] ] = num;
}
else{
printf("ERORR!\n");
}
}

/* 返回棧空間 */
int size(int* stack){
return stack[0];
}

/* 返回棧頂函數 */
int top(int* stack){
if(stack[0]>0){
return stack[ stack[0] ];
}
else{
printf("ERORR!\n");
return -1;
}
}

/* 出棧函數 */
void pop(int* stack){
if(stack[0]>0){
--stack[0];
}
else{
printf("ERORR!\n");
}
}

2. 用C語言實現棧的基本操作(數制的轉換)

//順序棧以及基本操作如下:

#include<iostream.h>
enum
{
MAX_SIZE=20
};

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

void InitStack(SqStack& S)
{
S.base=new int[MAX_SIZE];
S.top=S.base;
S.stacksize=MAX_SIZE;
}

bool Push(SqStack& S,int e)
{
if(S.top-S.base>=S.stacksize)
return false;
*S.top=e;
S.top++;
return true;
}

bool Pop(SqStack& S,int& e)
{
if(S.top==S.base)
return false;
S.top--;
e=*S.top;
return true;
}

void DestroyStack(SqStack& S)
{
if(S.base)
delete S.base;
S.top=S.base=NULL;
S.stacksize=0;
}

bool StackEmpty(SqStack S)
{
return (S.top==S.base);
}

void Print(SqStack S)
{
int i=0;

while(i<S.top-S.base)
{

cout<<S.base[i++]<<endl;
}
}

3. 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;
}

4. 急!用c語言實現鏈棧的操作

typedef struct node
{ ElemType data;
struct node *next;
} LinkStack;
⑴置空棧
void InitLinkStack( LinkStack * & s)
{ s=NULL;
}
⑵判棧空
int IsEmptyLinkStack(LinkStack *s )
{ if(s==NULL)
return 1;
else
return 0;
}
⑶ 入棧/*將元素x插入鏈棧top的棧頂*/
void PushLinkStack(LinkStack* &s , ElemType x)
{ LinkStack *p;
p=malloc(sizeof(LinkStack)); /*生成新結點*s */
p->data=x;
p->next=s;
s=p;

}
⑷出棧/*刪除鏈棧top的棧頂結點*/
int PopLinkStack (LinkStack* & s, ElemType &x)
{ LinkStack *p;
if(s==NULL) return 0;
x = s->data; /*將棧頂數據存入*x */
p = s; /*保存棧頂結點地址*/
s = s->next; /*刪除原棧頂結點*/
free (p); /*釋放原棧頂結點*/
return 1; /*返回新棧頂指針*/
}
(5) 取棧頂元素
int GetLinkStackTop (LinkStack* s, ElemType &x)
{
if(s==NULL) return 0;
x = s->data; /*將棧頂數據存入*x */
return 1; /*返回新棧頂指針*/
}
主函數怎麼寫吧

5. 如何採用循環的方式入棧呢用c語言

用數組的話:							
voidPush(structDStack*PtrS,ElementTypeitem,intTag)
{/*Tag作為區分兩個堆棧的標志,取值為1和2*/
if(PtrS->Top2–PtrS->Top1==1){/*堆棧滿*/printf(「堆棧滿」);return;
}
if(Tag==1)/*對第一個堆棧操作*/
PtrS->Data[++(PtrS->Top1)]=item;
else/*對第二個堆棧操作*/
PtrS->Data[--(PtrS->Top2)]=item;
}
用鏈表的話:
voidPush(ElementTypeitem,LinkStack*S)
{/*將元素item壓入堆棧S*/
structNode*TmpCell;
TmpCell=(LinkStack*)malloc(sizeof(structNode));TmpCell->Element=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}

















6. 棧的操作演算法c語言實現

#include<iostream>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREASE 1ypedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack (SqStack &S)//構造空棧
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲分配失敗
S.top=S.base;
return 0;
}
int Push(SqStack &S,int e)//插入新的元素為新的棧頂元素
{
if(S.top-S.base>=S.stacksize)//z棧滿,追加存儲空間
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
*S.top++ =e;//認為是先賦值再加加?
return 0;
}
void Insert(SqStack &S,int e)
{
int *p;
int temp;
p=S.base;
if(S.top-S.base>=S.stacksize)//z棧滿,追加存儲空間
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
while(*p<=e)
p++;
while(p<S.top)
{
temp=*p;
*p=e;
e=temp;
p++;
}
}0
void Pop(SqStack S)
{
if(S.base==S.top)
cout<<"該棧是空棧"<<endl;
while(S.base<S.top)
{
cout<<*++S.base<<" ";
}
}
void main()
{
SqStack S;
int j,i;
InitStack(S);
for(i=1;i<=5;i++)
{
cout<<"請輸入"<<i<<"個棧內元素"<<endl;
cin>>j;
Push(S,j);
}
Pop(S);
cout<<"請輸入你要插入的元素"<<endl;
cin>>j;
Insert(S,j);
cout<<"輸出棧內元素"<<endl;
Pop(S);

}

7. 棧的基本操作的實現(c語言),高手速來!!

/*程序錯誤太多*/ #include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"malloc.h"
#define
STACK_INIT_SIZE
10
//棧容量 typedef
struct
SqStack
{

int
top;
//棧頂當前指針

int
*base;
//棧空間數組
}SqStack; void
InitStack(SqStack
&S);
//構造空棧S
int
Push(SqStack
&S,int
e);
//入棧(棧地址,入棧數據)
返回0對,-1錯
int
Pop(SqStack
&S);
//出棧
(棧地址)返回棧頂數據
int
StackLength(SqStack
S);
//返回站的元素個數,即求棧長
void
Print_S(SqStack
S);
//顯示棧內數據 int
main()
{

SqStack
S;

int
i=0;

int
a,e;

InitStack(S);

srand((unsigned)time(NULL));
//srand((unsigned)time(NULL))以time函數值(當前時間)作為種子
printf("隨機填充5個元素為:
");

while(
i<5)

{

a
=
rand()%100;

printf("%d
",
a);

Push(S,a);

i++;

}
Print_S(S);

printf("請輸入要插入棧頂的元素:");

scanf("%d",&e);

Push(S,e);

Print_S(S);

printf("再彈出的棧頂元素為:%d
\n",Pop(S));

printf("棧的長度為:%d
\n",StackLength(S));

Print_S(S);

return
0;
} void
InitStack(SqStack
&S)
//構造空棧S
{

S.base
=
(int
*)malloc(STACK_INIT_SIZE
*
sizeof(int));
//分配組數空間,長度STACK_INIT_SIZE

if
(S.base==NULL)

{

printf("內存分配失敗!\n");

return;

}

S.top=-1;
} int
Push(SqStack
&S,int
e)
{

if(S.top>=STACK_INIT_SIZE)

{

printf("棧空間已滿,入棧失敗!\n");

return
-1;

}

else

{

S.base[++S.top]=e;

return
0;

}
} int
Pop(SqStack
&S)
//返回棧頂數據
{

if
(S.top>=0)
//棧內有數據

{

return
S.base[S.top--];

}

else

{

printf("空棧,無數據彈出!\n");

return
-1;

}
} int
StackLength(SqStack
S)
{

return
S.top+1;
} void
Print_S(SqStack
S)
{

printf("\n出棧顯示:");

if(S.top
==
-1)
printf("棧內無數據!\n");

else

{

while(S.top>=0
)
printf("%d
",Pop(S));

putchar('\n');

}
}

8. 用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;
}

9. 數據結構實驗(用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');

}

10. 棧的c語言實現基本操作

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#definestack_init_size20
#defineincreasesize10;
typedefintElemType;
typedefstructnode{
ElemType*base;
ElemType*top;
intsize;
}stack;

voidcreat(stack*s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->size=stack_init_size;
}

voidpush(stack*s,ElemTypee)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+increasesize)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->size;
s->size+=increasesize;
}
*(s->top)=e;
s->top++;
}

voidpop(stack*s,ElemType*e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
intstacklen(stacks)
{
return(s.top-s.base);
}
intmain()
{
stacks;
intn;
ElemTypee1,e2,d;
inti=1,j=1;
push(&s,i);
push(&s,j);
scanf("%d",&n);
while(n--)
{
pop(&s,&e1);
pop(&s,&e2);
d=e1+e2;
push(&s,e2);
push(&s,d);
}
pop(&s,&d);
printf("%d",d);
return0;
}