當前位置:首頁 » 編程語言 » 棧的實現c語言

棧的實現c語言

發布時間: 2022-06-11 22:01:08

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語言),高手速來!!

/*程序錯誤太多*/ #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');

}
}

⑶ 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語言實現隊列和棧的方法

#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 20
typedef char SElemType;
typedef int Status;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;#include<stdio.h>
#include<stdlib.h>Status CreatStack(SqStack &S){
//創建棧
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//CreatStackStatus Push(SqStack &S,SElemType e){
//插入e為新的棧頂元素
if(S.top-S.base>=S.stacksize){//棧滿,追加存儲空間
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);//存儲空間分配失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//PushStatus Pop(SqStack &S ,SElemType &e){
//若棧不空,刪除棧頂元素,並用e返回其值
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QNodePtr;typedef struct{
QNodePtr front;
QNodePtr rear;
}LinkQueue;Status CreatQueue(LinkQueue &Q){
//建立一個空的鏈式棧
Q.front=Q.rear=(QNodePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}Status EnQueue(LinkQueue &Q,QElemType e){ QNodePtr p;
p=(QNodePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}Status DeQueue(LinkQueue &Q,QElemType &e){QNodePtr p;<br>if(Q.front==Q.rear) return ERROR;<br>p=Q.front->next; e=p->data;<br>Q.front->next=p->next;<br>if(Q.rear==p) Q.rear=Q.front;<br>free(p);<br>return OK;<br>}int stackPalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("棧練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
SqStack S;
CreatStack(S);
char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
Push(S,b[count]); //進棧
count++;
}
int i = 0;
while(i < count)
{
Pop(S,a);
if(a!=b[i])
return ERROR;
i++;
}
return OK;}//stackPalindrom_Test int queuePalindrom_Test()//判別輸入的字元串是否迴文序列,是則返回1,否則返回0
{
printf("隊列練習,請輸入要判斷的字元串以#作為結束符,不要超過二十個字元\n");
LinkQueue Q;
CreatQueue(Q); char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
EnQueue(Q,b[count]);; //入列
count++;
} while(count-- > 0)
{
DeQueue(Q,a);
if(a!=b[count])
return ERROR;
}
return OK;
}//queuePalindrom_Test Status main(){ if(stackPalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n"); if(queuePalindrom_Test()==1)
printf("是迴文\n");
else printf("不是迴文\n");
return OK;
}

⑸ 在C語言中該怎樣建立棧具體代碼是什麼

1.棧空間(stack段)用來存放函數中的局部變數和函數調用時的上下文。
2.
全局變數和靜態變數存放於進程的數據段。
3.
windows下進程的棧空間會自動增長,一般不會出現空間不足的問題;
4。如果變數實在太大,甚至大於棧可增長的范圍,如數百兆,則會編譯出錯。

⑹ 棧的操作演算法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);

}

⑺ 棧的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;
}

⑻ c語言中怎樣用這兩個個結構體實現棧的功能

typedef
struct
node
{
int
data;//每個節點中存放的數據
PNode
next;//下一節點
}Node;
/*定義棧*/
typedef
struct
stack
{
PNode
top;//棧頂指針
int
Size;//棧的大小
}Stack;
#include"Stack.h"
#include<malloc.h>
#include<stdlib.h>
/*構造一個空棧*/
Stack
*InitStack()
{
Stack
*ps
=
(Stack
*)malloc(sizeof(Stack));
if(ps!=NULL)
{
ps->top
=
NULL;
ps->size
=
0;
}
return
ps;
}
/*判定是否為空棧*/
int
IsEmpty(Stack
*ps)
{
if(ps->top
==
NULL
&&
ps->size
==
0)
return
1;
else
return
0;
}
/*返回棧大小*/
int
GetSize(Stack
*ps)
{
return
ps->size;
}
/*元素入棧*/
PNode
Push(Stack
*ps,int
data)
{
PNode
pnode
=
(PNode)malloc(sizeof(Node));
if(pnode
!=
NULL)
{
pnode->data
=
data;
pnode->next
=
GetTop(ps,NULL);
ps->size++;
ps->top
=
pnode;
}
return
pnode;
}
/*返回棧頂元素*/
PNode
GetTop(Stack
*ps,int
*data)
{
if(IsEmpty(ps)!=1&&data!=NULL)
{
*data=
ps->top->data;
}
return
ps->top;
}
/*元素出棧*/
PNode
Pop(Stack
*ps,int
*data)
{
PNode
p
=
ps->top;
if(IsEmpty(ps)!=1&&p!=NULL)
{
if(int!=NULL)
*int
=
p->data;
ps->size--;
ps->top
=
ps->top->next;
free(p);
}
return
ps->top;
}
/*銷毀一個棧*/
void
DestroyStack(Stack
*ps)
{
if(IsEmpty(ps)!=1)
ClearStack(ps);
free(ps);
}
/*把棧置空*/
void
ClearStack(Stack
*ps)
{
while(IsEmpty(ps)!=1)
{
Pop(ps,NULL);
}
}
/*遍歷棧並訪問visit函數
*/
void
StackTraverse(Stack
*ps,void
(*visit)())
{
PNode
p
=
ps->top;
int
i
=
ps->size;
while(i--)
{
visit(p->data);
p
=
p->next;
}
}

⑼ 用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;
}
}

熱點內容
android載入界面 發布:2025-02-07 19:55:28 瀏覽:870
好礦雲伺服器 發布:2025-02-07 19:54:31 瀏覽:947
java電話簿 發布:2025-02-07 19:49:26 瀏覽:795
超級腳本製作 發布:2025-02-07 19:31:30 瀏覽:486
怎麼查看支付寶的賬號密碼 發布:2025-02-07 19:26:48 瀏覽:16
惠普伺服器查看ip指令 發布:2025-02-07 19:26:47 瀏覽:434
演算法設計模式 發布:2025-02-07 19:15:52 瀏覽:745
伺服器1u能連接幾台電腦 發布:2025-02-07 18:50:02 瀏覽:154
立人編譯 發布:2025-02-07 18:48:32 瀏覽:766
日產途達四驅的有哪些配置 發布:2025-02-07 18:42:02 瀏覽:832