Stack

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

‫‪Stack‬‬

‫‪:‬مميزاتها‬
‫‪ dynamic‬حجمها ليس ثابتا ‪1.‬‬
‫‪Lifo: last in first out‬تعمل وفق ‪2.‬‬
‫‪ Struct‬نوعه البياني ‪3.‬‬
‫لديها مكتبة كامل في لغة السي‪ ++‬اهمها الدوال التالية ‪4.‬‬
‫) (‪push‬‬
‫وضيفتها ادخال العناصر داخل المكدس‬
‫) (‪pop‬‬
‫وضيفتها اخراج اوحذف العناصر المكدس‬
‫) (‪empty‬‬
‫‪ top=-1‬للتبين من ان‬
‫اي ان المكدس فارغ ام ال‬
‫) (‪full‬‬
‫للتبين من ان المكدس قد تخطى الحجم الذي افترضناه له ام ال‬
‫) (‪print‬‬
‫لطبع عناصر المكدس‬
‫‪++:‬وتاليا بناء هذه الدوال في لغة سي‬

‫>‪#include<iostream‬‬
‫‪#define size 10‬‬
‫;‪using namespace std‬‬
‫{‪struct stack‬‬
‫;‪int top‬‬
‫;]‪int item[size‬‬
‫; ‪}s‬‬

‫)‪void push(stack &,int‬‬


‫;)& ‪int pop(stack‬‬
‫;)‪int empty(stack‬‬
int full(stack);
void print(stack);

int main() {//Maaaaaaaaaaaaaain


int choice ,total ,num;

cout<<"1-push ele into stack"<<endl;


cout<<"2-pop ele into stack"<<endl;
cout<<"3-print ele into stack"<<endl;
cout<<"4-exit ele into stack"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;

while(choice!=4){

if(choice==1){
cout<<"how many ele to push "<<endl;
cin>>total;

for(int i=-1; i<total; i++){


cout<<"pushing the ele "<<i<<" ";
cin>>num;
push(s,num);
}
}//end if ch 1

if(choice==2){
cout<<"how many ele to pop"<<endl;
cin>>total;
for(int i=-1; i<total; i++)
pop(s);
}//end if ch 2
if(choice==3)
print(s);
cout<<"enter another choice"<<endl;
cin>>choice;
}//end while
return 0;
s.top=-1;
}//end of Maaaaaaaain

int empty(stack s){

if(s.top == -1)
return 1;
else
return 0;
}
int full(stack s){
if(s.top==size-1)
return 1;
else
return 0;}
void push(stack &s,int num){
if(full(s))
cout<<"stack is full";
else
s.item[++(s.top)]=num ;
}
int pop(stack &s){
if (empty(s))
cout<<"stack is empty"<<endl;
else
return s.item[s.top--] ;
}

void print(stack s){


while(s.top!=-1){
cout<<s.item[s.top]<<endl;
s.top--;
}
}

You might also like