السلام عليكم ورحمة الله وبركاته
عندي واجب كيف تعمل حذف وإضافه للمكدس linked list طبعا
وهذي محاولتي بالبرنامج لكن هذا البرنامج يسوي اضافه push بالاضافه الى الطباعه لكن ماعرفت كيف الحذف
ارجوا من لديه حل الايبخل علينا وشكرا مقدما
#include<iostream.h>
void menu();
class stack
{
private:
int element;
stack *next;
public:
stack* push(stack*,int);
void stack_display(stack*);
}*head;
stack* stack::push(stack* head,int key)
{
stack *temp,*temp1;
temp1=head;
temp=new stack;
temp->element=key;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
while(head->next!=NULL)
head=head->next;
head->next=temp;
head=temp1;
}
return head;
}
void stack::stack_display(stack* head)
{
stack* temp;
temp=head;
if(temp!=NULL)
{
while(temp->next!=NULL)
{
cout<<temp->element<<" ";
temp=temp->next;
}
cout<<temp->element;
cout<<endl;
}
else
cout<<"the stack is empty\n";
}
void menu()
{
cout<<" main menu \n";
cout<<"________________";
cout<<"\n1>add\n";
cout<<"2>del\n";
cout<<"3>print\n";
cout<<"4>exit\n";
}
int main()
{
int c,x;
stack object;
head=NULL;
menu();
cin>>c;
while(c!=4)
{
switch(c)
{
case 1:
cout<<"\n enter: ";
cin>>x;
head=object.push(head,x);
break;
case 2:
break;
case 3:
cout<<"\n";
object.stack_display(head);
cout<<"\n";
break;
}
menu();
cin>>c;
}
return 0;
}