t=p;
}
else
{
t=new list;
t->inf=x;
t->next=p;
t->prev=p->prev;
p->prev->next=t;
p->prev=t;
}
return t;
}
list *EDel(list *p, int *x)
{
list *t;
*x=p->prev->inf;
if(p->next==p)
{
delete p;
p=NULL;
}
else
{
t=p->prev;
p->prev=t->prev;
t->prev->next=p;
delete t;
}
return p;
}
list *BDel(list *p, int *x)
{
list *t;
*x=p->inf;
if(p->next==p)
{
delete p;
p=NULL;
}
else
{
t=p;
p=p->next;
p->prev=t->prev;
t->prev->next=p;
delete t;
}
return p;
}
int Check (list *p)
{
if (p==NULL)
return 1;
else
return 0;
}
list *Clear(list *p)
{
list *t;
while(p->next!=p)
{
t=p;
p=p->next;
p->prev=t->prev;
t->prev->next=p;
delete t;
}
delete p;
p=NULL;
return p;
}
list* Show(list *p)
{
list *t,*h;
int x;
t=Create();
if (p==NULL)
printf("Empty\n");
else{
do
{
p=BDel(p,&x);
t=EAdd(t,x);
}
while(!Check(p));
h=t;
printf("%i ",h->inf);
while(h->next!=t){
h=h->next;
printf("%i ",h->inf);
}
}
printf("\n");
return t;
}
list *Create()
{
return NULL;
}
Модуль с основной подпрограммой(main):
#include <stdio.h>
#include <conio.h>
#include "queue.h"
void main()
{
int i,x;
list *line;
line=Create();
printf("Enter action:\n\t1.-Add to beginning\n\t2.-Add to end\n\t3.-Delete from beginning\n\t4.-Delete from end\n\t5.-Check empty\n\t6.-Clear deck\n\t0.-Exit\n");
scanf("%i",&i);
while(i)
{
switch(i)
{
case 1:
printf("x = ?\n");
scanf("%i",&x);
line=BAdd(line, x);
break;
case 2:
printf("x = ?\n");
scanf("%i",&x);
line=EAdd(line, x);
break;
case 3:
if(!Check(line))
{
line=BDel(line,&x);
printf("x = %i\n",x);
}
else
printf("impossible action!\n");
break;
case 4:
if(!Check(line))
{
line=EDel(line,&x);
printf("x = %i\n",x);
}
else
printf("impossible action!\n");
break;
case 5:
if(!Check(line))
printf("not empty\n");
else
printf("empty\n");
break;
case 6:
if(!Check(line))
line=Clear(line);
else printf("empty\n");
break;
default: printf("Unknown command: %i\n",i);
}
printf("\nEnter command(nuul to exit): ");
scanf("%i",&i);
}
line=Show(line);
line=Show(line);
line=Show(line);
getch();
}
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.