About the sequence stack initialization, into the stack, out of the stack, stack full, empty stack operation
Source: Internet
Author: User
This program is run under VC environment. Sequential stacks are relatively simple. This program simply deletes the data directly after inserting a data. Please forgive me for not being perfect.
typedef struct{
int data[maxsize];
int top;
}seqstack;
Seqstack.cpp files.
#include "Seqstack.h"
Initialization of sequential stacks
int Initseqstack (Seqstack *s) {
s->top=-1;
return True;
}
Judgment Stack Full
int Isfull (Seqstack s) {
if (s.top==maxsize-1) {
return True;
}else{
return False;
}
}
Judge Stack Empty
int IsEmpty (Seqstack s) {
if (s.top==-1) {
return True;
}else{
return False;
}
}
Into the stack
int push (Seqstack *s,int data) {
if (Isfull (*s)) {//stack full cannot be inserted
return False;
}else{
s->data[++ (s->top)]=data;
return True;
}
}
Out Stack
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 ("Enter the data to be inserted:");
scanf ("%d", &data);
if (push (&s,data)) {
printf ("Insert data successfully.") ");
}else{
printf ("The order stack is full.) ");
}
printf ("\ n");
if (pop (&s,&data)) {
printf ("Removed data is:%d", data);
}else{
printf ("Sequential stack is empty.) ");
}
printf ("\ n");
}
The results of the operation are as follows:
Please enter the data you want to insert: 23
Successfully inserted data.
The deleted data is: 23
Press any key to continue
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.