日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
c語言棧的pop函數(shù)編寫 棧pop函數(shù)怎么用

怎樣用C語言寫出對棧進(jìn)行的五種運(yùn)算:push()、pop()、top()、empty()、makempty()

這是我用鏈表寫的:

成都創(chuàng)新互聯(lián)長期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為江口企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),江口網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

#include stdio.h

#include stdlib.h

typedef struct node

{

int x;

struct node *next;

}Node;

typedef struct stack

{

Node *top;

}Stack;

void InitStack(Stack *s)

{

s-top=NULL;

}

int IsEmpty(Stack *s)

{

if(s-top==NULL)

return 1;

else

return 0;

}

void PushStack(Stack *s,int x)

{

Node *p;

p=(Node*)malloc(sizeof(Node));

p-x=x;

// p-next=NULL;

p-next=s-top;

s-top=p;

}

int PopStack(Stack *s)

{

int data;

Node *p;

p=(Node *)malloc(sizeof(Node));

if(IsEmpty(s))

{

printf("the stack is empty!\n");

free(p);

return -1;

}

else

{

p=s-top;

data=p-x;

s-top=p-next;

free(p);

return data;

}

}

int main (int argc,char **argv)

{

int i;

Stack s;

InitStack(s);

for(i=0;i1000;i++)

{

PushStack(s,i);

}

for(i=0;i1000;i++)

{

printf("%d\n",PopStack(s));

}

}

C語言 出棧操作Pop(struct SqStack * MyStack, ElemType *e

#include?stdio.h

#include?conio.h

#include?stdlib.h

#define?elemType?int /*?鏈棧元素?cái)?shù)據(jù)類型?*/

#define?SNODE_SIZE?sizeof?(struct?sNode) /*?鏈棧結(jié)點(diǎn)空間大小?*/

#define?status?int /*?狀態(tài)型變量?*/

#define?OVERFLOW?-1 /*?內(nèi)存溢出狀態(tài)碼?*/

#define?ERROR?0 /*?錯(cuò)誤狀態(tài)碼?*/

#define?OK?1 /*?正確狀態(tài)碼?*/

/*?鏈棧結(jié)點(diǎn)存儲(chǔ)結(jié)構(gòu)?*/

typedef?struct?sNode?{

elemType?data;

struct?sNode?*next;

}?sNode,?*sNodePtr;

/*?鏈棧存儲(chǔ)結(jié)構(gòu)?*/

typedef?struct?linkStack?{

sNodePtr?top;?/*?棧頂指針?*/

}?linkStack;

/*?初始化?*/

/*?操作結(jié)果:構(gòu)造一個(gè)帶頭結(jié)點(diǎn)的空鏈棧S?*/

void?initStack?(linkStack?*S)?{

S-top?=?(sNodePtr)?malloc?(SNODE_SIZE);?/*?產(chǎn)生頭結(jié)點(diǎn),棧頂指針指向此頭結(jié)點(diǎn)?*/

if?(!S-top)?/*?內(nèi)存分配失敗?*/

exit?(OVERFLOW);

S-top-next?=?NULL;

}

/*?銷毀?*/

/*?初始條件:鏈棧S已存在。操作結(jié)果:銷毀鏈棧S?*/

void?destroyStack?(linkStack?*S)?{

sNodePtr?p,?q;

p?=?S-top;?/*?p指向S的頭結(jié)點(diǎn)?*/

while?(p)?{

q?=?p-next;?/*?q指向p的下一個(gè)結(jié)點(diǎn)?*/

free?(p);?/*?回收p指向的結(jié)點(diǎn)?*/

p?=?q;?/*?p移動(dòng)到下一個(gè)結(jié)點(diǎn)?*/

}?/*?直到?jīng)]有下一個(gè)結(jié)點(diǎn)?*/

}

/*?判斷鏈棧是否為空?*/

/*?初始條件:鏈棧S已存在。操作結(jié)果:若S為空鏈棧,則返回TRUE,否則返回FALSE?*/

status?stackIsEmpty?(linkStack?*S)?{

return?S-top-next?==?NULL;

}

/*?入棧?*/

/*?操作結(jié)果:在S的棧頂插入新的元素e?*/

status?push?(linkStack?*S,?elemType?e)?{

sNodePtr?p;

p?=?(sNodePtr)?malloc?(SNODE_SIZE);?/*?產(chǎn)生新結(jié)點(diǎn)?*/

if?(!p)?/*?內(nèi)存分配失敗?*/

exit?(OVERFLOW);

p-data?=?e;

p-next?=?S-top-next;?/*?將新結(jié)點(diǎn)鏈接到原棧頂?*/

S-top-next?=?p;?/*?棧頂指向新結(jié)點(diǎn)?*/

}

/*?出棧?*/

/*?操作結(jié)果:刪除S的棧頂元素,并由e返回其值?*/

status?pop?(linkStack?*S,?elemType?*e)?{

sNodePtr?p;

if?(stackIsEmpty?(S))

return?ERROR;

p?=?S-top-next;?/*?p指向鏈棧的第一個(gè)結(jié)點(diǎn)?*/

*e?=?p-data;?/*?取出數(shù)據(jù)?*/

S-top-next?=?p-next;

free?(p);?/*?刪除該結(jié)點(diǎn)?*/

if?(S-top?==?p)?/*?棧為空?*/

S-top-next?=?NULL;

return?OK;

}

/*?打印棧內(nèi)容?*/

/*?初始條件:鏈棧S已存在。操作結(jié)果:當(dāng)棧不為空時(shí),打印棧內(nèi)容并返回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;

}

如有問題,可以點(diǎn)擊頭像聯(lián)系我

利用堆棧指針PUSH和POP編寫一個(gè)C語言程序

#include stdio.h

#include stdlib.h

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode Stack;

struct Node

{

int x;

PtrToNode Next;

};

int IsEmpty( Stack s )

{

return s-Next == NULL;

}

void Push( int x, Stack s )//壓棧

{

PtrToNode TmpCell;

TmpCell = malloc( sizeof( struct Node ) );

if( TmpCell == NULL)

{

printf("Out of Space!!");

exit(0);

}

else

{

TmpCell-x = x;

TmpCell-Next = s-Next;

s-Next = TmpCell;

printf("%d has been pushed!\n",x);

}

}

int Top( Stack s )//返回棧頂元素

{

if( !IsEmpty( s ))

{

return s-x;

}

printf("The stack is Empty!");

return -1;

}

void Pop( Stack s )//出棧

{

PtrToNode FirstCell;

if( IsEmpty( s ) )

{

printf("The stack is Empty!");

return;

}

else

{

FirstCell = s-Next;

s-Next = s-Next-Next;

printf("%d has been poped!\n",FirstCell-x);

free(FirstCell);

}

}

void MakeEmpty( Stack s )//清空棧

{

if( s == NULL )

{

printf( "Must use CreateStack first" );

return;

}

else

{

while( !IsEmpty( s ) )

{

Pop(s);

}

}

}

Stack CreateStack()//創(chuàng)建新棧

{

Stack s = malloc( sizeof( struct Node ) );

if( s == NULL )

{

printf( "Out of space!" );

exit(0);

}

s-Next = NULL;

MakeEmpty( s );

return s;

}

void main()

{

int i;

Stack s;

s = CreateStack();

for(i=1;i=20;++i)//將1~20壓入棧

{

Push(i,s);

}

for(i=1;i=20;++i)

{

Pop(s);

}

}

希望能幫到你,回答有點(diǎn)晚,希望來得及~

C語言寫Pop函數(shù)取棧的出錯(cuò)

Pop函數(shù)改成這樣:

int Pop (Stack * pstack, int * pname)

{

if(pstack-top=0)

{

return 0;

}

pstack-top--;

* pname = pstack-data[pstack-top];

return 1;

}

Push函數(shù)改成這樣:

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;

}

試試(原來那樣當(dāng)元素達(dá)到最大數(shù)目時(shí)pstack-top就越界了)。


標(biāo)題名稱:c語言棧的pop函數(shù)編寫 棧pop函數(shù)怎么用
文章位置:http://www.dlmjj.cn/article/docseps.html