#include?stdio.h

10年積累的成都網站建設、網站設計經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計制作后付款的網站建設流程,更有永興免費網站建設讓你可以放心的選擇與我們合作。
#include?conio.h
#include?stdlib.h
#define?elemType?int /*?鏈棧元素數據類型?*/
#define?SNODE_SIZE?sizeof?(struct?sNode) /*?鏈棧結點空間大小?*/
#define?status?int /*?狀態型變量?*/
#define?OVERFLOW?-1 /*?內存溢出狀態碼?*/
#define?ERROR?0 /*?錯誤狀態碼?*/
#define?OK?1 /*?正確狀態碼?*/
/*?鏈棧結點存儲結構?*/
typedef?struct?sNode?{
elemType?data;
struct?sNode?*next;
}?sNode,?*sNodePtr;
/*?鏈棧存儲結構?*/
typedef?struct?linkStack?{
sNodePtr?top;?/*?棧頂指針?*/
}?linkStack;
/*?初始化?*/
/*?操作結果:構造一個帶頭結點的空鏈棧S?*/
void?initStack?(linkStack?*S)?{
S-top?=?(sNodePtr)?malloc?(SNODE_SIZE);?/*?產生頭結點,棧頂指針指向此頭結點?*/
if?(!S-top)?/*?內存分配失敗?*/
exit?(OVERFLOW);
S-top-next?=?NULL;
}
/*?銷毀?*/
/*?初始條件:鏈棧S已存在。操作結果:銷毀鏈棧S?*/
void?destroyStack?(linkStack?*S)?{
sNodePtr?p,?q;
p?=?S-top;?/*?p指向S的頭結點?*/
while?(p)?{
q?=?p-next;?/*?q指向p的下一個結點?*/
free?(p);?/*?回收p指向的結點?*/
p?=?q;?/*?p移動到下一個結點?*/
}?/*?直到沒有下一個結點?*/
}
/*?判斷鏈棧是否為空?*/
/*?初始條件:鏈棧S已存在。操作結果:若S為空鏈棧,則返回TRUE,否則返回FALSE?*/
status?stackIsEmpty?(linkStack?*S)?{
return?S-top-next?==?NULL;
}
/*?入棧?*/
/*?操作結果:在S的棧頂插入新的元素e?*/
status?push?(linkStack?*S,?elemType?e)?{
sNodePtr?p;
p?=?(sNodePtr)?malloc?(SNODE_SIZE);?/*?產生新結點?*/
if?(!p)?/*?內存分配失敗?*/
exit?(OVERFLOW);
p-data?=?e;
p-next?=?S-top-next;?/*?將新結點鏈接到原棧頂?*/
S-top-next?=?p;?/*?棧頂指向新結點?*/
}
/*?出棧?*/
/*?操作結果:刪除S的棧頂元素,并由e返回其值?*/
status?pop?(linkStack?*S,?elemType?*e)?{
sNodePtr?p;
if?(stackIsEmpty?(S))
return?ERROR;
p?=?S-top-next;?/*?p指向鏈棧的第一個結點?*/
*e?=?p-data;?/*?取出數據?*/
S-top-next?=?p-next;
free?(p);?/*?刪除該結點?*/
if?(S-top?==?p)?/*?棧為空?*/
S-top-next?=?NULL;
return?OK;
}
/*?打印棧內容?*/
/*?初始條件:鏈棧S已存在。操作結果:當棧不為空時,打印棧內容并返回OK,否則返回ERROR?*/
status?printStack?(linkStack?*S)?{
sNodePtr?p;
if?(stackIsEmpty?(S))?{
puts?("The?stack?is?empty!?");
return?ERROR;
}
p?=?S-top-next;
while?(p)?{
printf?("%d\t",?p-data);
p?=?p-next;
}
putchar?('\n');
return?OK;
}
int?main?(void)?{
linkStack?S;
elemType?e;?
elemType?a,?b,?c,?d;
a?=?1;?b?=?2;?c?=?3;?d?=?4;
initStack?(S);
push?(S,?a);
push?(S,?b);
push?(S,?c);
push?(S,?d);
puts?("Push?4?elements");
printf?("S:\t");
printStack?(S);
putchar?('\n');
pop?(S,?e);
puts?("Pop?1?element");
printf?("S:\t");
printStack?(S);
destroyStack?(S);
getch?();?/*?屏幕暫留?*/
return?0;
}
如有問題,可以點擊頭像聯系我
Pop函數改成這樣:
int Pop (Stack * pstack, int * pname)
{
if(pstack-top=0)
{
return 0;
}
pstack-top--;
* pname = pstack-data[pstack-top];
return 1;
}
Push函數改成這樣:
int Push (Stack * pstack, int num)
{
if(pstack-top=Stack_size)
{
printf("Push Error!");
return 0;
}
pstack-data[pstack-top]=num;
pstack-top++;
return 0;
}
試試(原來那樣當元素達到最大數目時pstack-top就越界了)。
#include?stdio.h
#include?stdlib.h
#define?MAXSIZE?32
typedef?struct{
int?*elem;/*?棧的存儲區?*/
??int?max;???/*?棧的容量,即找中最多能存放的元素個數?*/
??int?top;???/*?棧頂指針?*/?
}Stack;
int?InitStack(Stack?*S,?int?n)?/*創建容量為n的空棧*/
{
S-elem?=?(int?*)malloc(n?*?sizeof(int));
if(S-elem==NULL)?return?-1;
S-max=n;
S-top?=0;?//棧頂初值0
return?0;
}
int?Push(Stack?*S,?int?item)?/*將整數item壓入棧頂*/
{
if(S-top==S-max)?{
printf("Stack?is?full!?\n");
return?-1;
}
S-elem[S-top++]?=?item;?//壓棧,棧頂加1
return?0;
}
int?StackEmpty(Stack?S)
{
return?(!S.top)?1:0;?/*判斷棧是否為空*/
}
int?Pop(Stack?*S)?/*棧頂元素出棧*/
{
if(!S-top)?{
printf("Pop?an?empty?stack!\n");
return?-1;
}
return?S-elem[--S-top]??;?//彈出棧,棧頂減1
}
void?MultibaseOutput(long?n,int?B)
{
int?m;?Stack?S;
if(InitStack(S,MAXSIZE)){
printf("Failure!\n");
return;
}
do?{
if?(Push(S,B?))?//------
{
printf("Failure!\n");
return;
}
n=?n-1?;?//--------
}while(n!=0);
while(!StackEmpty(S))?{?/*輸出B進制的數*/
m=Pop(S);
if(m10)?printf("%d",m);?/*小于10,輸出數字*/
else?printf("%c",?m+55);?/*大于或等于10,輸出相應的字符*/
}
printf("\n");
}
網站標題:c語言出棧pop函數 出棧函數pops返回的是什么值
本文路徑:http://www.yijiale78.com/article30/ddgcjpo.html
成都網站建設公司_創新互聯,為您提供云服務器、域名注冊、動態網站、外貿網站建設、做網站、用戶體驗
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯