栈的简单实现
Conceptions of Stack In Data Structure
A stack is a linear data structure in which insertion and deletion of data at the one end.
Stack is Abstract data type .
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。Stack can be represented in momery with the use of array .
Implementation Of Stack
implementating stack by use blow struct :
typedef int DataType; #define MaxSize 64 typedef struct { DataType data[MaxSize]; int top; }SeqStack;
Create:create an empty stack
//create a stack SeqStack CreateEmptyStack(){
SeqStack stack ;
stack.top=0;
return stack; }
Push:Pushes the element onto the stack .Nothing returned.
//push a data to top of stack void push(SeqStack &stack , DataType c){ stack.top++; stack.data[ stack.top]=c; }
Pop:return and than remove the element on the top of the stack
//return and remove the data at the top of stack DataType Pop(SeqStack &stack ){ DataType TopData=stack.data[stack.top]; stack.data[stack.top--]=NULL; return TopData; }
Peek:return the element on the top of stack
//return data at the top of stack DataType Peek(SeqStack &stack ){ return stack.data[stack.top]; }
IsEmpty:Test if the stack is empty ,return ture if stack is empty , returns false if the stack contains elements .
//return true ,if stack is empty bool IsEmpty(SeqStack &stack){ return stack.top==0; }
IsFull :check whether stack is full or not ,return true if stack is full ,and returns false if stack is not full.
//return true ,if the stack is full of data bool isFull(SeqStack &stack ){ return stack.top==MaxSize-1; }
DecimalToOctalConversion: turn decimal number to Octal number
//Decimal to octal conversion void DecimalToOctal(int Decimal ){ int remainder ,temp; SeqStack stack =CreateEmptyStack(); remainder =Decimal; temp= remainder ; while(remainder!=0){temp%=8; push(stack,temp); remainder/=8; temp=remainder; } puts("octal number is show in below :"); while(stack .top!=0)DisplayData(Pop(stack)); }
full code is show in below :
//进制间转换 #include<stdio.h> #include<stdlib.h> typedef int DataType; #define MaxSize 64 typedef struct { DataType data[MaxSize]; int top; }SeqStack; //create a stack SeqStack CreateEmptyStack(){ SeqStack stack ; stack.top=0; return stack; } //push a data to top of stack void push(SeqStack &stack , DataType c){ stack.top++; stack.data[ stack.top]=c; } //return and remove the data at the top of stack DataType Pop(SeqStack &stack ){ DataType TopData=stack.data[stack.top]; stack.data[stack.top--]=NULL; return TopData; } //return data at the top of stack DataType Peek(SeqStack &stack ){ return stack.data[stack.top]; } //return true ,if stack is empty bool IsEmpty(SeqStack &stack){ return stack.top==0; } //return true ,if the stack is full of data bool isFull(SeqStack &stack ){ return stack.top==MaxSize-1;} //getting data from keybroad DataType getDataType(){ DataType dt; scanf("%d",&dt); return dt; } //display the data void DisplayData(DataType data){ printf("%d",data); } //Decimal to octal conversion void DecimalToOctal(int Decimal ){ int remainder ,temp; SeqStack stack =CreateEmptyStack(); remainder =Decimal; temp= remainder ; while(remainder!=0){temp%=8; push(stack,temp); remainder/=8; temp=remainder; } puts("octal number is show in below :"); while(stack .top!=0)DisplayData(Pop(stack)); } void main(){ //create a empty stack SeqStack stack =CreateEmptyStack(); //push data DataType data; puts("input data seperate by blank space and end with 0!"); while((data=getDataType())!=0 ){push(stack,data);} //show data while(stack .top!=0)DisplayData(Pop(stack)); puts("\nstart turn Decimal to octal\ninput a Decimal Number"); //Decimal to octal conversion DecimalToOctal(getDataType()); puts(""); }
following is result :

更多精彩