Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / SPISOK(3

.CPP
Скачиваний:
26
Добавлен:
30.04.2013
Размер:
3.47 Кб
Скачать
#include <iostream.h>
#include <conio.h>

enum boolean {false,true};

class element
{
    int data;
    element* next;

 public:
   element() {data=0;next=0;}

   element(int a) {data=a;next=0;}

   friend ostream& operator << (ostream&,element&);

   friend class spisok;
};

   class spisok
   {
   element* top;
   public:
   spisok() {top=0;}

   spisok(int);

   spisok(spisok& a);
   ~spisok();
   spisok& operator=(spisok& a);
   friend ostream& operator << (ostream&,spisok&);

   element* poisk (int);

   void push(int);
   boolean pop(int);
   int peek(unsigned int);
   void show ();
   boolean member(int);
   };


      ostream& operator<< (ostream& s,element& a)
      {
      s<<a.data <<" ";
      return s;
      }

   spisok::spisok(int a)
   {
   top=0;
   element* p = new element(a);
   top=p;
   top->next=0;
   }

     spisok::spisok(spisok& a) {
     top = 0;
     element* p=a.top;
     do
     {
     push (p->data);
     p=p->next;
     }
     while (p!=0);
     }
     spisok::~spisok()
     {
     element* p=top,* ptr=p;
     do
       {
       if(ptr!=0)delete ptr;
       p=p->next;
       ptr=p;
       }
     while (p!=0);
     top=0;
     }
     ostream& operator<<(ostream& s,spisok& a)
     {
     a.show();
     return s;
     }
     element* spisok:: poisk (int x)
     {
     element* r=top;
     element* q=0;

     do
     {
     q=r;
     r=r-> next;
     }
     while (x<r->data&&r!=0);
     return q;
     }

     void spisok::push(int a)
     {
     element* q=poisk (a);
     element* p=new element(a);
     if (q==0)
     {
     if (top!=0) p-> next=top;
     top=p;
     }
     else
     {
     p-> next=q->next;
     q->next=p;
     }
     }
     boolean spisok::pop(int a) {
     if (top!=0)
     {
     if(member(a)==false)
     return false;
     element* q= poisk(a);
     element* r;
     if (q==0)
     {
     r=top;
     top=top-> next;
     delete r;
     return true;
     }
     else
     {
     r= q->next;
     q->next=q->next->next;
     delete r;
     return true;
     }
     }
     else return false;
     }
     int spisok:: peek (unsigned int a)
     {
     if (top==0)
     return false;
     element* p=top;
     for(int i=0;i<a;i++)
     {
     p=p-> next;
     if (p==0) break;
     }
     if(p==0) return false;
     else return p-> data;
     }
     void spisok:: show()
     {
     if (top==0) return;
     element* p=top;
     do
     {
     cout << p-> data<<" ";
     p=p-> next;
     }
     while (p!=0);
     }

     boolean spisok:: member(int a)
     {

     if (top==0) return false;
     element* p=top;
     for (int i=0;p!=0;i++)
     {
     if(p->data==a) return true; p=p-> next;
     }
     return false;
     }

     void main()
     {  spisok s;
     s.push(10);
     s.push(20);
     s.push(30);
     char key;
     int c;
     do

     {clrscr();
      cout<<s;
      cout<<"\n\n1.push\n2.pop\n3.view all\n4.view element";
      cout<< "\nesc-exit";
      key=getch();
      switch (key)
      {

      case 49 : {cout <<"\n\ndata="; cin>>c ;
      s.push(c);
      getch(); break;  }
      case 50 :{ cout <<"\n\nerase_data="; cin>>c;
      s.pop(c);
      getch();break; }
      case 51:cout <<"\n\n"<<s;getch();
      };
     }
     while(key!=27); }












Соседние файлы в папке Лабы