Скачиваний:
91
Добавлен:
01.05.2014
Размер:
1.6 Кб
Скачать

#include <iostream.h>
#include <stdio.h>

typedef int CInf;

class CElem{
public:
	CInf inf;
	CElem *next;
	CElem *prev;
	CElem(CInf inf0){
		inf = inf0;
		next = 0;
		prev = 0;
	}
	~CElem(){}
};

class CList{
	CElem *beg;
	CElem *end;
public:

	CList(){ beg = 0; end = 0;}

	~CList(){
		if (beg != 0){
			CElem *buf = beg;
			while(buf){
				buf = buf->next;
				delete beg;
				beg = buf;
			}
		}
	}

	void AddEl(CInf inf){
		CElem *Elem = new CElem(inf);
		if (beg == 0) beg = end = Elem;
		else{
			Elem->prev = end;
			end->next = Elem;
			end = Elem;
		}
	}

	void DelEl(int k){
		CElem *buf = beg;
		int i = 0;
		while((i<k-1) && (buf->next != 0)){
			buf = buf->next;
			i++;
		}
		buf->prev->next = buf->next;
		buf->next = buf->prev;
		delete buf;
	}

	void InsEl(int k, CInf d){
		CElem *Elem = new CElem(d);
		CElem *buf = beg;
		int i = 1;
		while((i<k-1) && (buf->next != 0)){
			buf = buf->next;
			i++;
		}
		Elem->next = buf->next;
		buf->next = Elem;
		Elem->prev = buf;
		buf->next->prev = Elem;
	}

	void InsEl(int k, CElem *Elem){
		CElem *buf = beg;
		int i = 1;
		if (k==1){
			Elem->next = beg;
			beg = Elem;
			return;
		}
		while((i<k-1) && (buf->next != 0)){
			buf = buf->next;
			i++;
		}
		if (buf->next == 0) end = Elem;
		Elem->next = buf->next;
		buf->next = Elem;
		Elem->prev = buf;
		buf->next->prev = Elem;

	}
	
	void PrintList(){
		CElem *buf = beg;
		while (buf !=0){
			printf("%d\n", buf->inf);
			buf = buf->next;
		}
	}
	CElem * GetEne(){return end;}
};
Соседние файлы в папке kurs