Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
15
Добавлен:
17.04.2013
Размер:
1.89 Кб
Скачать
// lab2_3zad.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
#include "new.h"
struct node {
	int info;
	node *next;
	node *last;
};
class list2 {
   node *head,*tail;
public: 
	list2() {head=tail=NULL;};
	 ~list2();
	 void AddStart(int info);
     void AddEnd(int info);
	 void printf(int b);
     //DelStart();
	 void DelEnd();	  

};
void list2::AddStart(int info)
{   node *p; 
 	if (head==NULL) 
	{head=new node;
	 if (head==NULL) return;
	 head->info=info;
	 head->last=head->next=NULL;
	 tail=head;
	} else
    {p=new node;
	 if (p==NULL) return;
	 p->info=info;
     p->next=head;
	 head->last=p;
	 p->last=NULL;
	 head=p;
	};
}
void list2::AddEnd(int info)
{    node *p; 
 	if (head==NULL) 
	{head=new node;
	 if (head==NULL) return;
	 head->info=info;
	 head->last=head->next=NULL;
	 tail=head;
	} else
    {p=new node;
	 if (p==NULL) return;
	 p->info=info;
     p->last=tail;
	 tail->next=p;
	 p->next=NULL;
	 tail=p;
	};
};
void list2::DelEnd()
{  node *p;
   if (tail==NULL) return;
   if (tail->last!=NULL) 
   { tail->last->next=NULL;
     p=tail;
	 tail=tail->last;
	 delete p;
   } else
   { delete tail;
     head=NULL;
   }
   return;
}
void list2::printf(int b)
{   node *p;
 	if (b==0) 
    { p=head;
	  cout<<">>";
		  while (p) 
		  { cout<<p->info<<" ";
		    p=p->next;
		  }
	  cout<<endl;
	} else
	{ p=tail;
	  cout<<"<<";
	  while (p) 
	  { cout<<p->info<<" ";
	    p=p->last;
	  }
	}
	cout<<endl;
};
list2::~list2()
{ while(!head) DelEnd();};
void main(int argc, char* argv[])
{ list2 A;
   int i;
   //for (i=1;i<10;i++) A.AddEnd(i);
   for (i=1;i<10;i++) A.AddStart(i);
   A.printf(0);
   A.printf(1);
}
Соседние файлы в папке ООП_ЭТМО