Pers.narod.ru. Алгоритмы. Делаем динамическую структуру на основе двусвязного списка |
Описывается динамическая структура с двумя указателями, на ее основе написана программа с консольным меню для выполнения действий по редактированию данных списка. См. также эту главу из лекций по Си.
#include <stdio.h>
#include <alloc.h>
#include <conio.h>
#include <string.h>
struct book {
struct book * prev;
struct book * next;
long int categ;
char nazv[20];
long int tirazh;
long int cena;
};
char wait_key() {
puts("Нажмите любую клавишу...");
return getch();
}
void input(struct book * ptr) {
puts("Код категории: ");
scanf("%ld",&(ptr->categ));
puts("Название: ");
scanf("%s",&(ptr->nazv));
puts("Тираж: ");
scanf("%ld",&(ptr->tirazh));
puts("Цена: ");
scanf("%ld",&(ptr->cena));
}
struct book * print(struct book * ptr) {
if (ptr == NULL) return NULL;
printf("Код категории: %ld\n",ptr->categ);
printf("Название: %s\n",&(ptr->nazv));
printf("Тираж: %ld\n",ptr->tirazh);
printf("Цена: %ld\n",ptr->cena);
puts("================================");
return (ptr->next);
}
int printall(struct book * root) {
clrscr();
if (root == NULL) {
puts("Нет записей для показа.");wait_key(); return 1;
}
struct book * temp;
temp=root;
for(;temp=print(temp););
wait_key();
return 0;
}
book * destroy_record(struct book * ptr){
if (ptr==NULL) return NULL;
else
{
if (ptr->prev!=NULL) (ptr->prev)->next=ptr->next;
if (ptr->next!=NULL) (ptr->next)->prev=ptr->prev;
}
if ((ptr->prev==NULL)&(ptr->next==NULL)) {
free(ptr);
return NULL;
}
struct book * temp=ptr->prev;
if (temp==NULL) temp=ptr->next;
free(ptr);
return temp;
}
int main(void) {
clrscr();
struct book * root=NULL;
struct book * current_el=NULL;
struct book * new_el;
struct book * show_el=NULL;
struct book work;
root->next=NULL;
root->prev=NULL;
char vibor;
//main menu
int working=1;
while (working) {
clrscr();
puts("1. Добавить запись");
puts("2. Удалить текущую запись");
puts("3. Вывести все записи");
puts("4. Предыдущая запись");
puts("5. Следующая запись");
puts("9. Выход");
puts("\nТекущая запись:\n");
print(show_el);
vibor=getch();
switch (vibor) {
case '1' : clrscr();
input(&work);
if ((new_el = (struct book *)malloc(sizeof(struct book))) == NULL) {
puts("Out of memory."); break; }
if (root != NULL) current_el->next=new_el;
new_el -> categ = work.categ;
strcpy(&(new_el->nazv[0]),work.nazv);
new_el -> tirazh = work.tirazh;
new_el -> cena = work.cena;
new_el -> next = NULL;
new_el -> prev = current_el;
if (root == NULL) root=new_el;
show_el=current_el=new_el;
break;
case '2' :
if (show_el->next==NULL) {
if((show_el=current_el=destroy_record(show_el))==NULL)
{ root=NULL;root->next=NULL; }
}
else
if (show_el->prev==NULL) {
if((show_el=root=destroy_record(show_el))==NULL)
{ root=NULL;root->next=NULL; }
}
else
if ((show_el=destroy_record(show_el))==NULL)
{ root=NULL;root->next=NULL; }
break;
case '3' : printall(root);break;
case '4' : if (show_el->prev!=NULL) show_el=show_el->prev;break;
case '5' : if (show_el->next!=NULL) show_el=show_el->next;break;
case '9' : working=0;break;
}
}
return 0;
}
|
|