Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 20.07.2011, 23:43   #1
Alexander1205
Пользователь
 
Аватар для Alexander1205
 
Регистрация: 22.01.2011
Сообщений: 78
По умолчанию стек_очередь

Доброго времени суток, форумчане! Нужно реализовать абстрактный класс " Список " и выполнить две его реализации:
- стеком
- очередью
Класс List есть, но дальше него никак...

Код HTML:
#include<iostream>
using namespace std;
 
template<typename t>
class List
{
        protected:
                int data;
public:
        virtual void Add(t int) = c;
        virtual void Del(t) = c;
        virtual void Prn(t) = c;
};
Alexander1205 вне форума Ответить с цитированием
Старый 21.07.2011, 00:02   #2
Сtrl
C++
Форумчанин
 
Аватар для Сtrl
 
Регистрация: 27.03.2011
Сообщений: 803
По умолчанию

Абстрактный класс скорее уж так выглядит:
Код:
template <class T>
class List abstract
{
protected:
  struct Elem
  {
    T data;
    Elem* next;
  };

public:
  virtual void push(T value) = 0;
  virtual T pop(void) const = 0;
  virtual void print(istream& out = cout) = 0;
};
Реализацию в виде стека и очереди могу помочь написать. Обращайся в Skype.
Ищете информацию по C++?
cplusplus.com
Сtrl вне форума Ответить с цитированием
Старый 21.07.2011, 00:32   #3
Alexander1205
Пользователь
 
Аватар для Alexander1205
 
Регистрация: 22.01.2011
Сообщений: 78
По умолчанию

Код HTML:
#include<iostream>
using namespace std;

template <typename T>
class List
{
private:    
    class node   
{
    public:
	T data;
	   node*   next; 
    public:
        node(node *_next) :next(_next){} 
   
    
};

private:
	class node *head, *tail;

public:
    void push_front(const T& val)
	{   
       if (empty())
	  {
		    head = new node(tail);   
	        head->data = val;
	  }
	 else
	   {
	      head = new node(head);
	      head->data = val;
	   }
    }

public:
    void push_back(const T& val)
    {
	     if (empty())
        {
		  head= new node(tail);
 	      head->data = val;
	    }   
         else
	      {
		 node *srh_node = head;	       
         for (;srh_node->next != tail ; srh_node = srh_node->next);  
	     srh_node->next = new node(tail); 
	     srh_node->next->data = val;
	      }
    }

public:
    T pop_front()
	   {
	    T val = head->data;
	    node *to_del = head;
	    head = head->next;
	    delete to_del;
	      return val;
       }
public:
	 T pop_back()
    {
	      node*srh_node = head;
	     if (srh_node->next == tail)
      
    {
	    head = tail;
		  T val = srh_node->data;
	      delete srh_node;
	      return val;       
    }
        else
	 {
	    for (;
(srh_node->next->next != tail); srh_node = srh_node->next);    

		  T val = srh_node->next->data;    
          node* to_del = srh_node->next;
		  srh_node->next =srh_node->next->next;
	      delete to_del;        
          return val;
	    }
    }

public:
    bool
empty() const
    {
	      return head == tail;
    }

public:
    virtual void clear()
	  {
	 node*dl_node;
        for (node *cr_node = head; cr_node != tail;)      
            {
	            dl_node = cr_node;
		        cr_node =cr_node->next;
		        delete dl_node;
            }
        head = tail;
      }

public:
	 List()  
    {
	  head = tail = new node(0);
    }
public:
    List(const List& l)
    {
	  clear();
	  node*cur_node = head;
	     for (node* srh_node =l.head; srh_node != l.tail; srh_node = shr_node->next)		
	       {
	           cur_node->next = new node(tail);  
               cur_node->data = srh_node->data;
	       }
    }

public:
	     virtual ~List()
    {	    
         clear();
    }
};

template <typename Ty>
class Queue :
protected List<Ty>
{
public:
    Queue()	  
:List() {}

public:
	Queue(const Queue& rhs):List(rhs)  {}  

public:
    ~Queue(){}

public:
    inline void push(const Ty& value)
   
      {
        List<Ty>::push_back(value);
	  }

public:
    inline Ty pop()
    {
	      return List<Ty>::pop_front();
    }
};

template
<typename Ty>
class Stack : protected List<Ty>
{
public:
    Stack():List(){}     

public:
    Stack(const Stack& rhs):List(rhs){} 

public:
    ~Stack(){}	 

public:
   
inline void push(const Ty& value)
    {	     
        List<Ty>::push_front(value);
    }

public:
	 
inline Ty pop()
	 {
	      return List<Ty>::pop_front();
     }
};

int main()
{
Queue<int> q;
q.push(1); 
q.push(2);
q.push(3);
cout << q.pop() << endl;
cout << q.pop() << endl;   
cout << q.pop() << endl;

Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.pop() << endl;
cout << s.pop() << endl;   
cout << s.pop() << endl;
}
Alexander1205 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц