الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

~¤¦¦§¦¦¤~Stack&Queue~¤¦¦§¦¦¤~

مغلق
بدأه كتكوتة في 22 أكتوبر 2004 · 1 رد · 585 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاتة

هذا الكود مكتوب بواسطة الborland c++

العرض على شكل منيو البرنامج يضم stack & queue

بالاضافه لبعض الاوامر اللي تدمج مابينهم

#include<iostream.h>
#include<conio.h>
#include<process.h>
enum bool{f,t};
//***************************************************************************
class node
{
 friend class stack;
 friend class queue;
 int data;
 node*next;
};
//***************************************************************************
class stack
{
   friend class queue;
   private:
      node * top;
   public:
      stack();
      bool empty();
      void push ( int );
      int  pop();
      void display ();
      void reverse();
      void transfer(stack &);
      void revstack_using_queue();
};
//***************************************************************************
stack::stack()
{
 top=NULL;
}
//***************************************************************************
bool stack:: empty()
{
  if( top==NULL )
    return t;
  else
    return f;
}
//***************************************************************************
void  stack:: push( int e)
{
  node * ptr=new node;
  ptr->data=e;
  ptr->next=top;
  top=ptr;
}
//***************************************************************************
int stack :: pop()
{
  node *temp=top;
  int x=top->data;
  if( empty())
    cout<<" THE STACK IS EMPTY ";
  else
  {  top=top->next;
     temp->next=NULL;
     delete temp;
  }
  return x;
}
//***************************************************************************
void stack::display()
{
  stack temp;
  int p;
  while(top!=NULL)
  {
    p=pop();
    cout<<p<<'\n';
    temp.push(p);
  }
  while (temp.top!=NULL)
  {
    push(temp.pop());
  }
}
//***************************************************************************
void stack :: transfer(stack &s)
{
  stack s1;
  int x;
  while ( ! empty())
  {
    x=pop();
    s1.push (x);
  }
  while ( ! s1.empty())
  {
    x=s1.pop();
    s.push(x);
  }
}
//***************************************************************************
void stack ::reverse()
{
  stack s1,s2;
  int x;
  while ( ! empty())
  {
    x=pop();
    s1.push (x);
  }
  while ( ! s1.empty())
  {
    x=s1.pop();
    s2.push(x);
  }
  while (!s2.empty())
  {
    x=s2.pop();
    push(x);
  }
}
//***************************************************************************
class queue
{
  private:
    node*last,*first;
  public:
     queue();
     bool qempty();
     bool qfull();
     void enqueue(int );
     int dequeue();
     void display ();
     queue copy();
     void revque_using_stack();
};
//***************************************************************************
void stack::revstack_using_queue()
{
  queue q;
  int s;
  while(top!=NULL)
  {
    q.enqueue(pop());
  }
  while(!q.qempty())
  {
    s=q.dequeue();
    push(s);
   }
}
//***************************************************************************
queue::queue()
{
  first=last=NULL;
}
//***************************************************************************
bool queue::qempty()
{
  if(  first==NULL && last==NULL)
    return t;
  else
    return f;
}
//***************************************************************************
void queue::enqueue( int e)
{
  node * ptr=new node;
  ptr->data=e;
  if(qempty())
  {
    first=ptr;
    last=ptr;
    ptr->next=NULL;
  }
  else
  {
    last->next=ptr;
    ptr->next=NULL;
    last=ptr;
  }
}
//***************************************************************************
int queue::dequeue()
{
  node* temp=first;
  int e;
  e=temp->data;
  if(qempty())
  return -1;
  if(first==last)
  {
   first=last=NULL;
  }
  else
  {
    first=first->next;
    temp->next=NULL;
  }
  delete temp;
  return e;
}
//***************************************************************************
void queue::display()
{
  queue q;
  int s;
  while (!qempty())
  {
    s=dequeue();
    cout<<s;
    q.enqueue(s);
  }
  while(!q.qempty())
  {
    enqueue(q.dequeue());
  }
}
//***************************************************************************
queue queue ::copy()
{
  queue tempq,q2;
  int  q;
  while(!qempty())
    tempq.enqueue(dequeue());
  while(!tempq.qempty())
  {
    q=tempq.dequeue();
    q2.enqueue(q);
    enqueue(q);
  }
 return q2;
}
//***************************************************************************
void queue::revque_using_stack()
{
  stack st;
  int s;
  while (! qempty())
  {
    s=dequeue();
    st.push(s);
  }
  while(!st.empty())
  {
    s=st.pop();
    enqueue(s);
  }
}
//***************************************************************************
 void main()
{
  stack s,s1;
  queue q,q1;
  int choice;
  for(;;)
  {
   clrscr();
   cout<<"1- ENQUEUE THE ELEMENT."<<endl;
   cout<<"2- DEQUEUE THE ELEMENT."<<endl;
   cout<<"3- DISPLAY ELEMENTS OF THE QUEUE."<<endl;
   cout<<"4- COPY AN ELEMENT TO ANOTHER QUEUE."<<endl;
   cout<<"5- REVERSE THE QUEUE USING STACK."<<endl;
   cout<<"6- PUSH THE ELEMENT."<<endl;
   cout<<"7- POP THE ELEMENT."<<endl;
   cout<<"8- REVERSE THE STACK."<<endl;
   cout<<"9- TRANSFER THE ELEMENT TO ANOTHER STACK IN THE SAME ORDER."<<endl;
   cout<<"10- DISPLAY THE ELEMENT OF THE STACK."<<endl;
   cout<<"11- REVERSE THE STACK USING QUEUE."<<endl;
   cout<<"12- EXIT."<<endl;
   cout<<" Enter ur choice"<<endl;
   cin>>choice;
  int c;
  switch(choice )
   {
    case 1:     clrscr();
   cout<<" Enter the element: ";
   cin>>c;
   q.enqueue(c);
   getch();
   break;
    case 2:      clrscr();
   cout<<q.dequeue();
   getch();
   break;
    case 3:      clrscr();
   q.display();
   getch();
   break;
    case 4:      clrscr();
   q1=q.copy();
   cout<<"The copied queue is:\n";
   q1.display();
   getch();
   break;
    case 5 :     clrscr();
   q.revque_using_stack();
   cout<<"To check that the queue is reversed select display from the menu";
   getch();
   break;
    case 6:      clrscr();
   cout<<" Enter the element: ";
   cin>>c;
   s.push(c);
   getch();
   break;
    case 7:      clrscr();
   cout<<s.pop();
   getch();
   break;
   case 8:       clrscr();
   s.reverse();
   cout<<"To check that the stack is reversed select display from the menu";
   getch();
   break;
   case 9:       clrscr();
   s.transfer(s1);
   cout<<"The elements of your stack transfered to a new stack\n";
   s1.display();
   getch();
   break;
   case 10 :     clrscr();
   s.display();
   getch();
   break;
   case 11:      clrscr();
   s.revstack_using_queue();
   cout<<"To check that the stack is reversed select display from the menu";
   getch();
   break;
   case 12:      exit(0);
   }
  }
}

//***************************************************************************

اتمنى انه يفيدكم ........تمنياتي لكم بالتوفيق كتكوتة

STACKQUE.CPP

تم تعديل هذه المشاركة بواسطة كتكوتة في 22 أكتوبر 2004 في 13:19

#2

وعليكم السلام ورحمة الله وبركاته

مشكوووووووووووووووووووووووووووره على البرنامج

بس يا ريت تساعدني

اضغط هنا حتى تستطيعي مساعدتي

هذا الموضوع مغلق.

مواضيع مشابهة