Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
13
Добавлен:
17.04.2013
Размер:
1.91 Кб
Скачать
#include <iostream.h>
#include <new.h>

typedef int ElemType;

struct Cell
{
	ElemType x;
    Cell* Next;
};

class List
{
	  Cell Head;
   public:
   	  List();
      ~List();
	  void Add(ElemType x);
      void Insert(Cell* pos,ElemType x);
      void Delete(Cell* pos);
      Cell* First(void);
      Cell* End(void);
      Cell* Locate(ElemType x);
      ElemType Retrieve(Cell* pos);
      void MakeNull(void);
      Cell* Next(Cell* pos);
      Cell* Prev(Cell* pos);
	  void PrnList(void);
};
List::List()
{
	Head.Next=NULL;
};
List::~List()
{
	MakeNull();
};
void List::Add(ElemType x)
{
	Cell* p;
	p=&Head;
	while(p->Next!=NULL)
		p=p->Next;
	p->Next=new Cell;
	p->Next->Next=NULL;
	p->Next->x=x;
};
void List::Insert(Cell* pos,ElemType x)
{
	Cell* temp;
	temp=pos->Next;
	pos->Next=new Cell;
	pos->Next->x=x;
	pos->Next->Next=temp;
};
void List::Delete(Cell* pos)
{
		Cell* temp;
		temp=pos->Next;
		pos->Next=pos->Next->Next;
		delete temp;
};
Cell* List::First(void)
{
	return Head.Next;
};
Cell* List::End(void)
{
		Cell* p=&Head;
		if(p->Next==NULL) return NULL;
		while(p->Next->Next!=NULL) p=p->Next;
		return p;
};
Cell* List::Locate(ElemType x)
{
	if(Head.Next!=NULL)
	{
		Cell* p=&Head;
		while(p->Next!=NULL)
		{
			if(p->Next->x==x) return p->Next;
			p=p->Next;
		};
	};
	return NULL;
	
};
ElemType List::Retrieve(Cell* pos)
{
	return pos->x;
};
void List::MakeNull()
{
	while(End()!=NULL) Delete(End());
};
Cell* List::Next(Cell* pos)
{
	return pos->Next;
};
Cell* List::Prev(Cell* pos)
{
	Cell* p=&Head;
	while(p->Next!=pos && p->Next!=NULL) p=p->Next;
	if(p->Next==pos) return p;
		else return NULL;
};

void List::PrnList(void)
{

		Cell* p=&Head;
		while(p->Next!=NULL)
		{
			p=p->Next;
			cout<<p->x<<" ";
			cout.flush();
		};
		cout<<endl;
		cout.flush();
};
Соседние файлы в папке 2 - Two tables