الأربعاء، 9 مارس 2011

تمثيل الستاك لنك لست باستخدام ++c

هذا برنامج الستاك لنك لست بلغة السي بلس بلس وفيه ميزات كثيرة من عملية اضافة العناصر على الستاك والحذف من الستاك ان هذا البرنامج يبني الستاك باستخدام الستركت ثم يقوم  بتمثيل الدوال الخاصة بالستاك داخل الكلاس وبعد ذلك تم بناء كل دالة داخل الكلاس وذلك باستخدام اللنك لست حيث يتم تمثيل عناصر الستاك داخل لنك لست

يحتوي البرنامج على واجهة بالين تظهر فيهات كل العمليات التي يستطيع المستخدم تنفيذها من اضافة عناصر وحذف عناصر ومعرفة التوب في الستاك


# include <iostream>
# include <conio>

template <class T>
struct node
{
  T info;
  node<T> *link;

};

 template <class T>
 class stacktype
 {
    private:
              node<T> *stacktop;
              int count;



    public:
              stacktype();
              void inti();
              void push(const T& item);
              void pop();
              T top();
              bool isempty();
             int c_item();
             void print();


 };


  template <class T>
  int stacktype<T>::c_item()
  {
    return count;

  }

  template <class T>
  T stacktype<T>::top()
  {
  if(!isempty())
    return stacktop->info;
    else
    return false ;

  }



    template <class T>
  bool stacktype<T>::isempty()
  {
     if(stacktop==NULL)
     return true;
     else
     return false;

  }


  template <class T>
  stacktype<T>::stacktype()
  {

     stacktop=NULL;
     count =0;


  }

    template <class T>
  void stacktype<T>:: inti()
  {
         node<T> *temp;
      while(stacktop != NULL)
      {
          temp=stacktop;
          stacktop=stacktop->link;
          delete temp;
      }

  }

  template <class T>
  void stacktype<T>:: push(const T& item)
  {

            node<T> *newnode;
         newnode=new node<T>;
         newnode->info=item;
         newnode->link=stacktop;
         stacktop=newnode;
         count++;


  }

    template <class T>
  void stacktype<T>:: pop()
  {
           node<T> *temp;
      if(stacktop != NULL)
      {
          temp=stacktop;
          stacktop=stacktop->link;
          delete temp;
          count--;
          cout<<" you pop from  stack."<<endl;

      }
      else
         cout<<"cannot pop from empty stack."<<endl;

  }

      template <class T>
  void stacktype<T>::print()
  {
      node<    T> *current;
      current=stacktop;
      while(current != NULL)
      {
        cout<<current->info<< " ";
        current=current->link;
      }
  }

  int main()
  {

       stacktype<int> stack_link;
       int x,item,n;

       textcolor(43);
       cprintf("        case 1: push item in stack link.");
       cout<<endl;
       cprintf("        case 2:pop item from stack link.");
       cout<<endl;
       cprintf("        case 3: top of stack link.");
       cout<<endl;
       cprintf("        case 4:print item in stack.");
       cout<<endl;
       cprintf("       press zero (0) to end.");
       cout<<endl<<endl;


       stack_link.inti();

       do
       {
          cprintf("Enter your choise :");
          cin>>x;
       switch(x)
       {
           case 1:
                 {
                    cout<<"Enter the number of item that u want push : ";
                    cin>>n;
                    for(int i=0;i<n;i++)
                    {
                         cprintf("     Enter item");cprintf("(");cout<<i;cprintf(")");cprintf(":");
                                     cin>>item;
                       stack_link.push(item);

                     }
                     cout<<endl;
                   break;
                 }

           case 2:
                {

                       stack_link.pop();

                       cout<<endl;
                     break;
                 }
           case 3:
                 {
                           if( stack_link.top()==0)
                           cout<<" the stack is empty"<<endl;
                           else
                         cout<<"stacktop is : "<<stack_link.top();
                         cout<<endl;

                  break;
                 }
           case 4:
                 {
                         
                           if( stack_link.top()==0)
                           {
                           cout<<" the stack is empty"<<endl;
                           }
                           else
                           {
                         cprintf("The stacklink is :");
                             stack_link.print();

                            }
                    cout<<endl;
                  break;
                }

           }// end switch
             } // end while

               while(x !=0);

   return 0;
  }


ليست هناك تعليقات:

إرسال تعليق