關於順序棧的初始化,進棧,出棧,棧滿,棧空的操作

來源:互聯網
上載者:User
本程式在VC環境下運行。順序棧比較簡單。本程式只是插入一個資料之後直接刪除該資料。不夠完善請原諒。


seqstack.h檔案。
#include<stdio.h>
#include<stdlib.h>


#define MAXSIZE 50


#define False 0;
#define True 1;


typedef struct{
int data[MAXSIZE];
int top;
}seqstack;


seqstack.cpp檔案。
#include "seqstack.h"


//順序棧的初始化
int Initseqstack(seqstack *s){
s->top=-1;

return True;
}


//判斷棧滿
int isFull(seqstack s){
if(s.top==MAXSIZE-1){
return True;
}else{
return False;
}
}


//判斷棧空
int isEmpty(seqstack s){
if(s.top==-1){
return True;
}else{
return False;
}
}


//進棧
int push(seqstack *s,int data){
if(isFull(*s)){//棧滿無法插入
return False;
}else{
s->data[++(s->top)]=data;

return True;
}

}


//出棧
int pop(seqstack *s,int *data){
if(isEmpty(*s)){
return False;
}else{
(*data)=s->data[(s->top)--];


return True;
}
}






void main(){
seqstack s;
int data;


Initseqstack(&s);


printf("請輸入要插入的資料:");
scanf("%d",&data);


if(push(&s,data)){
printf("成功插入資料。");
}else{
printf("順序棧已滿。");
}


printf("\n");


if(pop(&s,&data)){
printf("刪除的資料是:%d",data);
}else{
printf("順序棧為空白。");
}


printf("\n");
}


運行結果如下:
請輸入要插入的資料:23
成功插入資料。
刪除的資料是:23
Press any key to continue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.