Jump to content
СофтФорум - всё о компьютерах и не только

двусвязный список


Recommended Posts

разработать программу для создания и работы с двусвязным списком, состоящим из структур. для работы со списком создать меню со следующими пунктами:

1. Создание списка.

2. Просмотр списка.

3. Добавление в конец списка новой структуры.

4. Корректировка списка.

5. Выход.

Структура содержит название книги, автора, год издания. п4.- Удалить издания с годом меньше заданного.

//============================/////////============================

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#include <io.h>

//=============================//////===============================

struct Book

{

char nazv[10];

char avtor[20];

int godizd;

Book* next;

Book* prev;

};

struct list

{

Book* first;

Book* last;

};

void menu();

void addBook(list&);

void delBook(list&);

void printlist(list*);

void createlist(list*&);

list* thelist=0;

int main()

{

Book* first=0;

Book* last=0;

menu();

getche();

return 0;

}

void menu()

{ char quit;

enum{false,true};

quit = false;

while(true)

{

int choice;

printf("\n -----MENU-----\n");

printf("(1) vvedite spisok \n");

printf("(2) prosmotr \n");

printf("(3) dobavlenie \n");

printf("(4) ydalenie \n");

printf("(5) exit \n");

scanf("%d", &choice);

switch (choice)

{

case(1): if (!thelist)

{

createlist(thelist );

printf(" the list has been created succesfully... \n");

}

else

printf(" the list is already created...\n" );

break;

case(2):

if (thelist)

printlist(thelist);

else

printf(" the list is not created...\n");

break;

case(3):

if (thelist)

addBook(*thelist);

else

printf(" the list is not created...\n");

break;

case 4:

if (thelist)

delBook(*thelist);

else

printf(" the list is not created... \n");

break;

case(5):

quit = true;

}

if (quit == true)

break;

}

}

//==============================/////////===================================

void addBook(list& thelist)

{

printf("\n *** dobavit' novuy knigy *** \n");

printf(" vvedite nazvanie: ");

Book* newBook = new Book;

scanf("%s",&newBook->nazv);

printf(" vvedite avtora: ");

scanf("%s",&newBook->avtor);

printf(" vvedite god izdania: ");

scanf("%d",&newBook->godizd);

if (thelist.last)

{

thelist.last->next = newBook;

newBook->prev = thelist.last;

}

else

{

thelist.first = newBook;

newBook->prev = 0;

}

thelist.last = newBook;

newBook->next = 0;

printf("*** book was added successfully *** \n");

}

//=======================================///////////////==========================

void createlist (list*& thelist)

{

thelist = new list;

thelist->first = NULL;

thelist->last = NULL;

Book *p,*k=NULL;

do

{

p = new Book;

printf( "*** vvedite knigy *** \n");

printf(" vvedite nazv: ");

scanf("%s", &p->nazv);

printf( " vvedite avtora: " );

scanf("%s", &p->avtor);

printf(" vvedite god izdania: ");

scanf( "%d",&p->godizd);

if(thelist->first==NULL) thelist->first = p;

p->next = NULL;

if(k)

{

p->prev = k;

k->next = p;

}

else

{

p->prev = NULL;

}

k = p;

puts(" Zakonchit' - <esc>");

}

while (getch()!=27);

thelist->last=p;

}

//=======================================================///////////============================

void printlist(list* thelist)

{

printf("\n *** prosmotr knig *** \n");

Book* curBook = thelist->first;

while (curBook)

{

// printf("%s\n",curBook->nazv);

// printf("%s\n",&curBook->avtor);

// printf("%d\n",&curBook->godizd);

// curBook = curBook->next;

printf("\n %s %s %d ",curBook->nazv,curBook->avtor,curBook->godizd);

curBook = curBook->next;

}

printf("\n *** end *** \n" );

}

//=================//======================//=======================//

void delBook(list& thelist)

{

Book* curBook = thelist.first;

int Pos;

printf( " Enter the godizdania for deleted Book: \n");

scanf("%d",&Pos);

printf("%d",Pos);

for (int i=0; i<Pos ; i++)

{

if (curBook)

curBook = curBook->next;

}

if (curBook && (Pos >= 0))

{

if (curBook->prev)

{

curBook->prev->next = curBook->next;

}

else

{

thelist.first = curBook->next;

}

if (curBook->next)

{

curBook->next->prev = curBook->prev;

}

else

{

thelist.last = curBook->prev;

}

delete curBook;

printf("Book ? ", Pos ," has been deleted successfully..." );

}

else

printf("Book ? ", Pos , " not found..." );

}

помогите подправить и исправить ошибки, пункт удаление не работает вообще

Link to comment
Share on other sites

Мне ненравится эта часть. Символьная переменная quit, а значение ей присваивается как булевой

...  char quit;  enum{false, true};  quit = false;...
Link to comment
Share on other sites

разработать программу для создания и работы с двусвязным списком, состоящим из структур. для работы со списком создать меню со следующими пунктами:

1. Создание списка.

2. Просмотр списка.

3. Добавление в конец списка новой структуры.

4. Корректировка списка.

5. Выход.

Структура содержит название книги, автора, год издания. п4.- Удалить издания с годом меньше заданного.

[/code]//============================/////////============================

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#include <io.h>

//=============================//////===============================

struct Book

{

char nazv[10];

char avtor[20];

int godizd;

Book* next;

Book* prev;

};

struct list

{

Book* first;

Book* last;

};

void menu();

void addBook(list&);

void delBook(list&);

void printlist(list*);

void createlist(list*&);

list* thelist=0;

int main()

{

Book* first=0;

Book* last=0;

menu();

getche();

return 0;

}

void menu()

{ char quit;

enum{false,true};

quit = false;

while(true)

{

int choice;

printf("\n -----MENU-----\n");

printf("(1) vvedite spisok \n");

printf("(2) prosmotr \n");

printf("(3) dobavlenie \n");

printf("(4) ydalenie \n");

printf("(5) exit \n");

scanf("%d", &choice);

switch (choice)

{

case(1): if (!thelist)

{

createlist(thelist );

printf(" the list has been created succesfully... \n");

}

else

printf(" the list is already created...\n" );

break;

case(2):

if (thelist)

printlist(thelist);

else

printf(" the list is not created...\n");

break;

case(3):

if (thelist)

addBook(*thelist);

else

printf(" the list is not created...\n");

break;

case 4:

if (thelist)

delBook(*thelist);

else

printf(" the list is not created... \n");

break;

case(5):

quit = true;

}

if (quit == true)

break;

}

}

//==============================/////////===================================

void addBook(list& thelist)

{

printf("\n *** dobavit' novuy knigy *** \n");

printf(" vvedite nazvanie: ");

Book* newBook = new Book;

scanf("%s",&newBook->nazv);

printf(" vvedite avtora: ");

scanf("%s",&newBook->avtor);

printf(" vvedite god izdania: ");

scanf("%d",&newBook->godizd);

if (thelist.last)

{

thelist.last->next = newBook;

newBook->prev = thelist.last;

}

else

{

thelist.first = newBook;

newBook->prev = 0;

}

thelist.last = newBook;

newBook->next = 0;

printf("*** book was added successfully *** \n");

}

//=======================================///////////////==========================

void createlist (list*& thelist)

{

thelist = new list;

thelist->first = NULL;

thelist->last = NULL;

Book *p,*k=NULL;

do

{

p = new Book;

printf( "*** vvedite knigy *** \n");

printf(" vvedite nazv: ");

scanf("%s", &p->nazv);

printf( " vvedite avtora: " );

scanf("%s", &p->avtor);

printf(" vvedite god izdania: ");

scanf( "%d",&p->godizd);

if(thelist->first==NULL) thelist->first = p;

p->next = NULL;

if(k)

{

p->prev = k;

k->next = p;

}

else

{

p->prev = NULL;

}

k = p;

puts(" Zakonchit' - <esc>");

}

while (getch()!=27);

thelist->last=p;

}

//=======================================================///////////============================

void printlist(list* thelist)

{

printf("\n *** prosmotr knig *** \n");

Book* curBook = thelist->first;

while (curBook)

{

// printf("%s\n",curBook->nazv);

// printf("%s\n",&curBook->avtor);

// printf("%d\n",&curBook->godizd);

// curBook = curBook->next;

printf("\n %s %s %d ",curBook->nazv,curBook->avtor,curBook->godizd);

curBook = curBook->next;

}

printf("\n *** end *** \n" );

}

//=================//======================//=======================//

void delBook(list& thelist)

{

Book* curBook = thelist.first;

int Pos;

printf( " Enter the godizdania for deleted Book: \n");

scanf("%d",&Pos);

printf("%d",Pos);

for (int i=0; i<Pos ; i++)

{

if (curBook)

curBook = curBook->next;

}

if (curBook && (Pos >= 0))

{

if (curBook->prev)

{

curBook->prev->next = curBook->next;

}

else

{

thelist.first = curBook->next;

}

if (curBook->next)

{

curBook->next->prev = curBook->prev;

}

else

{

thelist.last = curBook->prev;

}

delete curBook;

printf("Book ? ", Pos ," has been deleted successfully..." );

}

else

printf("Book ? ", Pos , " not found..." );

}

[/

помогите подправить и исправить ошибки, пункт удаление не работает вообще

[/quote

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...