當前位置:首頁 » 編程語言 » c語言基於棧的應用
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言基於棧的應用

發布時間: 2022-05-23 19:39:05

A. c語言 利用棧實現數據分類

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

intconstMAX=20;

typedefstructstack{
int*data;
intsize;
}*STACK,*pStack;

STACKinitstack(intn){//建棧
STACKs=(STACK)malloc(sizeof(structstack));
s->data=(int*)malloc(n*sizeof(int));
s->size=0;
returns;
}

voidpush(STACKs,inte){//數據進棧
s->data[s->size]=e;
++s->size;
}

intpop(STACKs,int*e){//出棧
if(s->size>0){
--s->size;
*e=s->data[s->size];
return1;
}
return0;
}

intmain(){//主函數
intnum,i;
STACKodd=initstack(MAX);
STACKeven=initstack(MAX);
srand(time(0));
printf("原始序列: ");
for(i=0;i<MAX;i++){
num=rand()%100;
if(num%2)push(odd,num);
elsepush(even,num);
printf("%d",num);
}
printf(" 奇數棧: ");
while(pop(odd,&num))printf("%d",num);
printf(" 偶數棧: ");
while(pop(even,&num))printf("%d",num);
printf(" ");
return0;
}

B. 那位大神能講下C語言中棧的使用啊

堆棧就是先入後出的數據結構。
如果用c語言來實現的話用個struct
先定義一個棧的節點
struct
node;
typedef
strcut
node
*
position;
typedef
position
stack;
stack
creat();
void
push(int,stack);
int
pop(stack);
int
top();
struct
node
{
int
element;
position
next;
}
stack
creat()
{
stack
s;
s=malloc(sizeof(struct
node));
s->next==NULL;
}
void
push(int
a,stack
s)
{
position
temp;
temp=malloc(sizeof(struct
node));
temp->element=a;
temp->next=s->next;:
s->next=temp;
}
int
pop(stack
s)
{
int
res=s->next->element;
position
temp=s->next;
s->next=temp->next;
free
temp;
}
int
top(stack
s)
{
return
s->next->element;
}
好啦,先creat()一個棧,再進行push
pop等。程序中忽略了麻煩的錯誤檢測給出了重點,當然還可以添加其他操作。。對了,頭文件也要加上。本人還是習慣用c++弄成一個類^_^

C. C語言棧的應用

這段程序看得我濕噠噠的。。。。將#include "stdafx.h"改為#include "stdio.h"
加上#include<malloc.h>之後,出現死循環。。。從命名規則來看,照抄書上的程序吧?
。。。自己寫一個吧,這個改起來還不如自己寫

D. c語言 棧是怎麼應用的 哪位大神能說的好理解點

棧分為出棧和入棧,入棧是為了保護你剛剛正在進行的程序,把它放進指定的空閑位置,出棧是你執行完另一件事後把之前保存入棧的東西在從存放的地方拿出來。這是為了保護數據,防止丟失。!
比如你正看著書呢,小夥伴叫你去玩,你就加個書簽在讀過的那一頁放進書櫥,玩回來了拿出書翻到那一頁接著讀。而你的書簽就相當於起到保護作用,回來翻到那一頁就相當於取出之前存入棧中的內容。

E. 請問一下C語言關於棧的基本應用

#include <stdlib.h>

010 #include <malloc.h>

011 #include <memory.h>

012 #include <assert.h>
/*------------------------------------------------------------

12 // 棧結構的定義

13 ------------------------------------------------------------*/

14 typedef struct Stack

15 {

16 ElemType *base; // 棧基址

17 ElemType *top; // 棧頂

18 int stacksize; // 棧存儲空間的尺寸

19 } SqStack;

20

21 /*------------------------------------------------------------

22 // 棧的基本操作

23 ------------------------------------------------------------*/

24

25 bool InitStack(SqStack *S);

26 void DestroyStack(SqStack *S);

27 bool StackEmpty(SqStack S);

28 int StackLength(SqStack S);

29 bool GetTop(SqStack S, ElemType *e);

30 void StackTraverse(SqStack S, void (*fp)(ElemType));

31 bool Push(SqStack *S, ElemType e);

32 bool Pop(SqStack *S, ElemType *e);

33 bool bracketMatch(SqStack *S,const char *arr);

const int STACK_INIT_SIZE = 100; // 初始分配的長度

016 const int STACKINCREMENT = 10; // 分配內存的增量

017

018 /*------------------------------------------------------------

019 操作目的: 初始化棧

020 初始條件: 無

021 操作結果: 構造一個空的棧

022 函數參數:

023 SqStack *S 待初始化的棧

024 返回值:

025 bool 操作是否成功

026 ------------------------------------------------------------*/

027 bool InitStack(SqStack *S)

028 {

029 S->base = (ElemType *)malloc(STACK_INIT_SIZE *sizeof(ElemType));

030 if(S ->base == NULL)

031 return false;

032 S ->top = S ->base;

033 S->stacksize = STACK_INIT_SIZE;

034 return true;

035 }

036

037 /*------------------------------------------------------------

038 操作目的: 銷毀棧

039 初始條件: 棧S已存在

040 操作結果: 銷毀棧S

041 函數參數:

042 SqStack *S 待銷毀的棧

043 返回值:

044 無

045 ------------------------------------------------------------*/

046 void DestroyStack(SqStack *S)

047 {

048 assert(S != NULL);

049 free(S ->base);

050 S->base = S->top = NULL;

051 }

052

053 /*------------------------------------------------------------

054 操作目的: 判斷棧是否為空

055 初始條件: 棧S已存在

056 操作結果: 若S為空棧,則返回true,否則返回false

057 函數參數:

058 SqStack S 待判斷的棧

059 返回值:

060 bool 是否為空

061 ------------------------------------------------------------*/

062 bool StackEmpty(SqStack S)

063 {

064 assert((S.base != NULL)&&(S.top != NULL));

065 return (S.base == S.top);

066 }

067

068 /*------------------------------------------------------------

069 操作目的: 得到棧的長度

070 初始條件: 棧S已存在

071 操作結果: 返回S中數據元素的個數

072 函數參數:

073 SqStack S 棧S

074 返回值:

075 int 數據元素的個數

076 ------------------------------------------------------------*/

077 int StackLength(SqStack S)

078 {

079 assert((S.base != NULL)&&(S.top != NULL));

080 return (S.top - S.base);

081 }

082

083 /*------------------------------------------------------------

084 操作目的: 得到棧頂元素

085 初始條件: 棧S已存在

086 操作結果: 用e返回棧頂元素

087 函數參數:

088 SqStack S 棧S

089 ElemType *e 棧頂元素的值

090 返回值:

091 bool 操作是否成功

092 ------------------------------------------------------------*/

093 bool GetTop(SqStack S, ElemType *e)

094 {

095 assert((S.base != NULL) && (S.top != NULL));

096 if(S.top == S.base)

097 exit(0);

098 *e = *(S.top - 1);

099 return true;

100 }

101

102 /*------------------------------------------------------------

103 操作目的: 遍歷棧

104 初始條件: 棧S已存在

105 操作結果: 依次對S的每個元素調用函數fp

106 函數參數:

107 SqStack S 棧S

108 void (*fp)() 訪問每個數據元素的函數指針

109 返回值:

110 無

111 ------------------------------------------------------------*/

112 void StackTraverse(SqStack S, void (*fp)(ElemType))

113 {

114 assert((S.base != NULL) && (S.top != NULL));

115 while(S.base < S.top)

116 {

117 (*fp)(*S.base);

118 S.base++;

119 }

120 }

121

122 /*------------------------------------------------------------

123 操作目的: 壓棧——插入元素e為新的棧頂元素

124 初始條件: 棧S已存在

125 操作結果: 插入數據元素e作為新的棧頂

126 函數參數:

127 SqStack *S 棧S

128 ElemType e 待插入的數據元素

129 返回值:

130 bool 操作是否成功

131 ------------------------------------------------------------*/

132 bool Push(SqStack *S, ElemType e)

133 {

134 if(S == NULL)

135 return false;

136 assert((S->base != NULL) && (S ->top != NULL));

137 if(S ->top - S ->base >= S->stacksize)

138 {

139 S ->base = (ElemType *)realloc(S ->base,(S ->stacksize + STACKINCREMENT) * sizeof(ElemType));

140 if(! S ->base)

141 exit(0);

142 S ->top = S->base + S ->stacksize;

143 S ->stacksize += STACKINCREMENT;

144 }

145 *(S ->top++) = e;

146 return true;

147 }

148

149 /*------------------------------------------------------------

150 操作目的: 彈棧——刪除棧頂元素

151 初始條件: 棧S已存在且非空

152 操作結果: 刪除S的棧頂元素,並用e返回其值

153 函數參數:

154 SqStack *S 棧S

155 ElemType *e 被刪除的數據元素值

156 返回值:

157 bool 操作是否成功

158 ------------------------------------------------------------*/

159 bool Pop(SqStack *S, ElemType *e)

160 {

161 if((* S).top == (*S).base)

162 return false;

163 *e = * --(* S).top;

164 return true;

165 }

F. C語言 棧的應用 數制轉換

#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#define MaxSize 100

//用棧的方法
typedef struct {
int data[MaxSize];
int top;
}Stack;//順序棧,也可用鏈棧

void InitStack(Stack &S)//初始化棧
{
S.top=-1;
}

int push(Stack &S,int x)//入棧
{
if(S.top==MaxSize)
return 0; //棧滿
else{
S.top++;
S.data[S.top]=x;
return 1;
}
}

int pop(Stack &S,int &x)//出棧
{
if(S.top==-1)
return 0; //棧空
else{
x=S.data[S.top];
S.top--;
return 1;
}
}

void display(Stack &S)
{
int x;
while(S.top!=-1)
{
pop(S,x);
printf("%x",x);
}

}
void func(int n,int m)//n為十進制數,m為2,8,或16
{
Stack S;
InitStack(S);
while(n!=0)
{
push(S,n%m);
n=n/m;
}
display(S);
}

int main(int argc, char* argv[])
{
int n;
printf("\n請輸入一個數:");
scanf("%d",&n);
int m=2;
func(n,m);
m=8;
func(n,m);
m=16;
func(n,m);
return 0;
}

G. 數據結構C語言,棧的應用,請補充代碼

void
InitStack(SqStack
*s)

{

s.top=0;

}
int
StackEmpty(SqStack
S) {

return (S.top==0);
} //空返回1, 非空則返回
void Push(SqStack &S,SElemType e)
{
if (S.top==Maxlen){ //棧滿
printf(「Stackis overflow.\n」);
exit(1); }
S.top++;
S.data[S.top]=e;
}
elemtypePop(SqStack &S,SElemType &e) {
if (S.top==0) { //棧空
printf(「The stack is empty!\n」); //出錯提示信息
return (NULLELEM); } //返回空數據值
S.top--;
e=S.data[S.top+1];
return e;
} //返回原棧頂元素

H. c語言中什麼是堆棧,堆棧的具體應用,求高手幫我解釋下,最好能詳細點,謝謝

也就是一個井嘍,然後丟石頭進去,要的話就從最上面的拿上來。如
abc要進棧,則: 元素 棧中的元素
1.a進棧。 cb a
2.可以a出棧也可以b進棧。 c ba
3.可以b出棧也可以c進棧。 無 cba
如果按照示例的話,3次後abc都進入棧了,則從上往下的排序分別是c b a,此時只可以取出c,不可以取出其他的,也就是說,棧就是:
可以往裡面丟東西,填平了或者無聊的時候就取出最上面的東西。
你可以理解成人的肝臟。如果糖分多的話,多餘的就存在肝臟里,如果少了,就從肝臟裡面取。但是你不把靠近血液的糖取出來,你是沒有辦法取更裡面的糖分的......

它的應用吧,應該是可以用來加密。如可執行文件的出棧、入棧。不過一般我們電腦裡面的計算就用到了棧,只不過沒有察覺到。
原創!

I. 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
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);
}

J. 求大神精簡的講講c語言中棧的用法

堆棧用於特殊的數據存儲,一般用於函數調用,特點是先入後出。
在進行子程序(函數)調用時,它可以存儲調用時調用者給出的參數,子程序通過堆棧指針可找到所有傳入的參數,即使修改它們由於返回時設置堆棧指針也不會影響參數原型數據;子程序開設的普通變數也會在堆棧中開辟。
在進行中斷函數調用時,堆棧用於保護現場,可以將斷點處程序執行的關鍵寄存器壓棧保存,執行完後彈棧恢復現場。
堆棧的好處在於,cpu有專用的堆棧地址寄存器,壓棧、彈棧有專用語句,對於棧內數據存取以堆棧指針為參照存取方便,利於最後舍棄無需的數據。