當前位置:首頁 » 編程語言 » c語言簡單的棧

c語言簡單的棧

發布時間: 2022-12-06 01:40:02

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");
}
}

❷ C語言 求棧的簡單例子

#include"iostream.h"
const int maxsize=6;
class stack{
float data[maxsize];
int top;
public:
stack(void);
~stack(void);
void push(float a);
bool empty(void);
float pop(void);
};
stack::stack(void){
top=0;
cout<<"stack initialized."<<endl;
}
stack::~stack(void){
cout<<"stack destoryed"<<endl;
}
bool stack::empty(void){
return top==0;
}
void stack::push(float a){
if(top==maxsize){
cout<<"Stack overflow!"<<endl;
return ;
}
data[top]=a;
top++;
}
float stack::pop(void){
if(top==0){
cout<<"An empty stack!"<<endl;
return 0;
}
top--;
return data[top];
}
int main()
{
stack s1,s2;
float i=0.0;
for(;i<=maxsize;i++)
s1.push(2*i);
for(i=1;i<=maxsize;i++)
cout<<s1.pop()<<" ";
for(i=1;i<=maxsize;i++)
s1.push(2.5*i);
for(i=1;i<=maxsize;i++)
s2.push(s1.pop());
cout<<endl;
do
cout<<s2.pop()<<" ";
while(!(s2.empty()));
cout<<endl;
return 0;
}

❸ 數據結構C語言 棧

肯定是你指針指向不對
太長不太想看,只給你指出一處錯誤

scanf("%d,&c1");//通過鍵盤輸入為變數c1賦值

這一句,看到啥錯誤了吧?還有你聲明了char幹嘛又用%d呢,如果用%c的時候注意這樣寫
scanf(" %c",&c1);

❹ 急!用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");
}
}

❺ 什麼是C語言中的棧

棧是個數據結構,指數據的後進先出的方法。c語言可以編寫這種數據結構。另外在c中參數的傳遞實際上也是入棧和出棧的過程。如果不明白,建議看一下數據結構中對棧的講解。

❻ C語言棧的簡單實現

#include<stdio.h>
#include<malloc.h>
//enum
bool
{false,true};
typedef
struct
Node{
int
a;
int
Number;
//在棧中的序號,棧底為0
struct
Node
*next;
}Node,*LpNode;
typedef
struct
SqStack{
Node
*top;
Node
*prev;
Node
*base;
int
length;
}*LpSqStack;
//將e的能容復制到S中並將e摧毀
bool
Node_evaluation(LpNode
S,LpNode
e,bool
M)
{
//賦值操作
//S->Number
=
e->Number;
if(M
==
true)
free(e);
return
true;
}
bool
InitStack(LpSqStack
S)
{
S->length
=
0;
S->base
=
(LpNode)malloc(sizeof(Node));
if(!S->base)
return
false;
S->top
=
S->base;
S->prev
=
S->base;
S->base->Number
=
0;
return
true;
}
bool
StackEmpty(LpSqStack
S)
{
if(S->top
!=
S->base)
return
false;
return
true;
}
bool
GetTop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
e
=
S->top;
return
true;
}
bool
Push(LpSqStack
S,LpNode
e)
{
if(!Node_evaluation(S->top,e,true))
return
false;
S->top->Number
=
S->prev->Number
+
1;
S->prev
=
S->top;
S->top
=
(LpNode)malloc(sizeof(Node));
S->prev->next
=
S->top;
S->top->next
=
NULL;
return
true;
}
bool
Pop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
if(!Node_evaluation(e,S->top,true))
return
false;
S->top
=
S->prev;
S->top->next
=
NULL;
return
true;
}
bool
Vistit(LpSqStack
S,LpNode
e,int
i)
{
LpNode
p;
p
=
S->base;
for(int
j
=
0;
j
=
i;
j++)
{
if(p->next
==
NULL)
return
false;
p
=
p->next;
}
if(!Node_evaluation(p,e,false))
return
false;
return
true;
}
int
main()
{
SqStack
a;
InitStack(&a);
LpNode
b=new
Node;
LpNode
c=new
Node;
LpNode
d=new
Node;
//free(&b);這free了你下面又賦值。。。
b->a=1;
Push(&a,c);
GetTop(&a,c);
printf("%d",c->a);
return
0;
}
棧里的內存是不能free的,你要free你就自己在堆里分配。

❼ 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<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語言,棧的實現~

你寫的太復雜,這個拿去用吧
// Stack node
struct Node
{
int data ;
Node* next ;
Node(int d, Node* p):data(d), next(p){}
};

class Stack
{
public:
Stack():top(NULL){}

void Push(int d)
{
top = new Node(d, top) ;
}

int Pop()
{
Node* temp = top ;
top = top->next ;
return temp->data ;
}

bool Empty()
{
return top == NULL ;
}

private:
Node* top ;
};

❿ c語言中,棧是具體應用方法和步驟

棧簡單的講就是一片存儲區域(存儲區的首地址即為棧頂)
你可以向棧中存入數據取出數據刪除數據
/* Note:Your choice is C IDE */
#include "stdio.h"
#define m 100
struct Mystack/*定義棧結構*/
{
char element[m];
int top;/*棧頂*/
};
void push(struct Mystack *s,char x) /*將x的值壓入棧頂*/
{
/* s->element[s->top]=x;
s->top++;*/
s->element[s->top]=x;
s->top++;
}
void pop(struct Mystack *s)
/*將棧頂元素刪除*/
{
s->top--;
}
int IsEmpty(struct Mystack *s)
/*判斷棧是否為空*/
{
if(s->top==0)
return 1;
else
return 0;
}
void Clearstack(struct Mystack *s)
/*清空棧元素*/
{
s->top=0;
}
void Displaystack(struct Mystack *s)
/*輸出棧元素*/
{
int i;
for(i=0;i<s->top;i++)
printf("%c",s->element[i]);
}

main()
{
struct Mystack st;
int i;
char ch;
for(i=0;i<100;i++)
st.element[i]='\0';
st.top=0;
printf("please write a string:\n");
ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '#':
if(!IsEmpty(&st))
pop(&st);
break;
case '@':
if(!IsEmpty(&st))
Clearstack(&st);
break;
default:
push(&st,ch);
}
ch=getchar();
}
printf("the string is :\n");
Displaystack(&st);
}

熱點內容
查ip地址伺服器數量 發布:2024-04-25 20:49:48 瀏覽:619
安卓手機單核性能為什麼不高 發布:2024-04-25 20:48:07 瀏覽:55
群暉php 發布:2024-04-25 20:00:35 瀏覽:883
怎麼查看我的wifi密碼 發布:2024-04-25 18:54:43 瀏覽:757
fckeditorforjava 發布:2024-04-25 18:50:27 瀏覽:624
優酷上傳視頻需要多久 發布:2024-04-25 18:33:05 瀏覽:675
inf12編譯器 發布:2024-04-25 18:15:39 瀏覽:99
撲克總督3安卓哪裡下載 發布:2024-04-25 18:10:02 瀏覽:395
什麼網站是php 發布:2024-04-25 18:03:42 瀏覽:221
java教程免費下載 發布:2024-04-25 18:02:01 瀏覽:443