C語(yǔ)言中棧的兩種實(shí)現(xiàn)方法詳解
一、順序棧
#include<stdio.h> #include<stdlib.h> #define maxsize 64 //定義棧 typedef struct { int data[maxsize]; int top; }sqstack,*sqslink; //設(shè)置??? void Clearstack(sqslink s) { s->top=-1; } //判斷??? int Emptystack(sqslink s) { if (s->top<0) return 1; else return 0; } //進(jìn)棧 int Push(sqslink s, int x) { if (s->top>=maxsize-1) return 0; else { s->top++; s->data[s->top]=x; return 1; } } // 出棧 int Popstack(sqslink s) { int n; if (Emptystack(s)>0) return NULL; else { n=s->data[s->top]; s->top--; return n; } } void main() { sqslink s1; s1 =(sqslink)malloc(sizeof(sqstack)); Clearstack(s1); printf("%d\n",s1->top); for(int i=0; i<=10;i++) { Push(s1, i); printf("%d is pushed into stack\n",i); } printf("top is point to %d\n",s1->top); printf("\n"); int n; n = Popstack(s1); printf("number %d is poped\n",n); printf("top is point to %d\n",s1->top); }
二、鏈?zhǔn)綏?/h2>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node * next;
}snode,*slink;
struct Node
{
slink i;
slink n;
};
// 清空棧
void Clearstack(slink top)
{
top=NULL;
}
//判斷棧是否為空
int Emptystack(slink top)
{
if (top==NULL) return 1;
else return 0;
}
// 進(jìn)棧
slink Push(slink top, int x)
{
slink node = NULL;
node = (slink)malloc(sizeof(snode));
node->data = x;
node->next = top;
top = node;
printf("*************************\n");
printf("%d",top->data);
printf("*************************\n");
return top;
}
// 出棧
struct Node Pop(slink top)
{
slink node = NULL;
struct Node result;
if (Emptystack(top))
{
result.i=node;
}
else
{
int n;
node = top;
top = node->next;
result.i = top;
result.n = node;
return result;
}
}
void main()
{
slink top_ = NULL;
for(int i =0; i<10;i++)
{
top_ = Push(top_, i);
printf("%d is pushed in to the stack\n",i);
}
int e;
e = top_->data;
printf("top is pointint to %d\n",e);
printf("\n");
printf("\n");
printf("\n");
slink node =NULL;
printf("*************************\n");
struct Node result = Pop(top_);
if ((result.i)!=NULL)
{
top_ = result.i;
node = result.n;
e = top_->data;
printf("top is pointint to %d\n",e);
int e_node;
e_node = node->data;
printf("the node Poped 's data is pointint to %d\n",e_node);
free(node);
}
else
{
printf("stack is empty");
}
}
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node * next; }snode,*slink; struct Node { slink i; slink n; }; // 清空棧 void Clearstack(slink top) { top=NULL; } //判斷棧是否為空 int Emptystack(slink top) { if (top==NULL) return 1; else return 0; } // 進(jìn)棧 slink Push(slink top, int x) { slink node = NULL; node = (slink)malloc(sizeof(snode)); node->data = x; node->next = top; top = node; printf("*************************\n"); printf("%d",top->data); printf("*************************\n"); return top; } // 出棧 struct Node Pop(slink top) { slink node = NULL; struct Node result; if (Emptystack(top)) { result.i=node; } else { int n; node = top; top = node->next; result.i = top; result.n = node; return result; } } void main() { slink top_ = NULL; for(int i =0; i<10;i++) { top_ = Push(top_, i); printf("%d is pushed in to the stack\n",i); } int e; e = top_->data; printf("top is pointint to %d\n",e); printf("\n"); printf("\n"); printf("\n"); slink node =NULL; printf("*************************\n"); struct Node result = Pop(top_); if ((result.i)!=NULL) { top_ = result.i; node = result.n; e = top_->data; printf("top is pointint to %d\n",e); int e_node; e_node = node->data; printf("the node Poped 's data is pointint to %d\n",e_node); free(node); } else { printf("stack is empty"); } }
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
簡(jiǎn)單對(duì)比C語(yǔ)言中的fputs()函數(shù)和fputc()函數(shù)
這篇文章主要介紹了簡(jiǎn)單對(duì)比C語(yǔ)言中的fputs()函數(shù)和fputc()函數(shù),注意其之間的區(qū)別,需要的朋友可以參考下2015-08-08C/C++: Inline function, calloc 對(duì)比 malloc
以下是對(duì)c/c++中的malloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下2016-07-07C/C++讀取大文件數(shù)據(jù)方式詳細(xì)講解
這篇文章主要介紹了C語(yǔ)言/C++讀取大文件數(shù)據(jù)的完整方式過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09C語(yǔ)言使用posix正則表達(dá)式庫(kù)的實(shí)現(xiàn)
在C語(yǔ)言中,你可以使用 POSIX 正則表達(dá)式庫(kù)(regex.h)來(lái)進(jìn)行正則表達(dá)式的模式匹配,本文主要介紹了C語(yǔ)言使用posix正則表達(dá)式庫(kù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12C++調(diào)用matlab函數(shù)的實(shí)例
這篇文章主要介紹了C++調(diào)用matlab函數(shù)的方法,包括封裝matlab函數(shù),編譯matlab函數(shù)及C++環(huán)境配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Cocos2d-x 3.x入門教程(一):基礎(chǔ)概念
這篇文章主要介紹了Cocos2d-x 3.x入門教程(一):基礎(chǔ)概念,本文講解了Director、Scene、Layer、Sprite等內(nèi)容,需要的朋友可以參考下2014-11-11C語(yǔ)言 經(jīng)典題目螺旋矩陣 實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言 經(jīng)典題目螺旋矩陣 實(shí)例詳解的相關(guān)資料,這里附有代碼實(shí)例及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12C語(yǔ)言嵌入informix基礎(chǔ)入門示例講解
這篇文章主要介紹了C語(yǔ)言嵌入informix基礎(chǔ)方法,大家參考使用2013-11-11