Программа работаем с двусвязным списком состоящим из структур. Тут реализовано несколько функций: создание, просмотр, добавление в конец и удаление структуры элемент которой year будет меньше заданного. Вот последняя функция удаления и не работает у меня, такое впечатление, что не выполняется цикл с предусловием While(p!=NULL), но я для самопроверки добавил там вывод структуры на экран, выводится правильно, т.к. я новичок в этом деле вопрос где я не прав?
Код:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
struct spis
{
char name[20];
char author[20];
int year;
struct spis *prev;
struct spis *next;
};
void create(void);
void list(spis *);
void add(void);
void del(void);
struct spis *head, *tail;
main()
{
char c;
while(1)
{
clrscr();
puts("1 - Create list ");
puts("2 - View list ");
puts("3 - Add new struct at the end of list");
puts("4 - Correction of list ");
puts("5 - Exit ");
c=getch();
switch(c)
{ case '1':create();break;
case '2':list(head);break;
case '3':add();break;
case '4':del();break;
case '5':return 0;
default:puts("Error! Use buttons 1-5");
}
}
// free(head);
// getch();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
void create(void)
{
clrscr();
spis *p,*pred;
pred=NULL;
do
{
p=(spis *)malloc(sizeof(spis));
printf("\n Enter the name of publication: ");
scanf("%s",&p->name);
printf("Enter author of publication: ");
scanf("%s",&p->author);
printf("Enter the year of publication: ");
scanf("%d",&p->year);
p->prev=pred;
if(pred!=NULL)
pred->next=p;
else
head=p;
pred=p;
printf("\n Press <esc> for the exit or any key to continue");
}
while(getch()!=27);
tail=p;
tail->next=NULL;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
void list(spis *p)
{
clrscr();
printf("\n-------------------------------------------------------------------------");
printf("\n | Name of Publication | Author | Year of publication | \n");
printf("-------------------------------------------------------------------------");
p=head;
while (p!=NULL)
{
printf("\n %20s %20s %18d", p->name,p->author,p->year);
p=p->next;
}
printf ("\n Press any button for the exit in main menu");
getch();
}
void add(void)
{
spis *p,*pn;
clrscr();
pn=(spis *)malloc(sizeof(spis));
printf("\n Enter name of publication:");scanf("%s",&pn->name);
printf("\n Enter author of publication:");scanf("%s",&pn->author);
printf("\n Enter year of the publication:");scanf("%d",&pn->year);
p=tail; // perehod v konec spiska
pn->prev=tail; //ustanovka svyzei
pn->next=NULL;
p->next=pn;
tail=pn; //novii konec spiska
}
void del()
{
spis *p,*temp;
int year2;
clrscr();
printf("\n Enter the year of publication ");scanf("%d",&year2);
p=head;
printf("%20s%20s%d \n",p->name,p->author,p->year);
getch();
// p=tail;
// printf("%20s%20s%d \n",p->name,p->author,p->year);
getch();
while (p!=NULL)
{ if (p->year==year2)
{ if (p==head)
{ printf("\n udalena first zapis: %20s%20s%d \n",p->name,p->author,p->year);
head=p->next;
head->prev=NULL;
free(p);
p=head;
}
else
if (p==tail)
{printf("\n udalena poslednyy zapis %20s%20s%d \n",p->name,p->author,p->year);
tail=p->prev;
tail->next=NULL;
free(p);
p=tail;
}
else
{printf("\n Udalena zapis %20s%20s%d \n",p->name,p->author,p->year);
p->next->prev=p->prev;
p->prev->next=p->next;
temp=p;
p=p->next;
free(temp);
}
}
else
p=p->next;
}
}
___________
Код нужно оформлять по правилам:
тегом [CODE]..[/СODE]
(кнопочка на панели форматирования с решёточкой #)
Не забывайте об этом!
Модератор.