-1

Задание: 1. Создание списка. 2. Добавление элемента в список: • в начало списка; • в конец списка; • после элемента с заданным номером; • после элемента с заданным ключом. 3. Вывод содержимого списка на экран. 4. Удаление элемента из списка: • из начала списка; • из конца списка; • элемента с заданным номером; • элемента с заданным ключом. 5. Упорядочивание элементов в списке по выбранному признаку. Почему код не работает?

#include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #define TRUE 1
    struct List
    {
     int x;
     struct List * Next;
    };
    struct List * CreateList(void);
    void DisplayList( struct List* ); 

    void RemoveTermBegin( struct List** );
    void RemoveTermTag( struct List* );
    void RemoveTermEnd( struct List* );
    void AddTermBegin( struct List** );
    void AddTermEnd(struct List* );
    void AddTermTag(struct List* );
    void FreeList(struct List** );
    int main(void)
{
 struct List* Begin;
 char Key;
 while(TRUE)
 {
 printf( "\nEnter the number- the mode of ”
“operations with Lists: "
 "\n 1 - FORMATION OF THE LIST"
 "\n 2 - VIEWING OF CONTENTS OF”
“THE LIST”
 “\n 3 - REMOVE THE ELEMENT FROM ”
“THE BEGIN OF LIST"
 "\n 4 - REMOVE THE ELEMENT FROM ”
“THE MIDLE OF LIST"
 "\n 5 - REMOVE THE ELEMENT FROM ”
“THE END OF LIST”
 "\n 6 - ADD THE ELEMENT IN THE ”
“BEGIN OF LIST"
 "\n 7 - ADD THE ELEMENT IN THE ”
“END OF LIST”
 "\n 8 - ADD THE ELEMENT AFTER ”
“ELEMENT-KEY OF LIST”
 "\n 9 - END OF WORK\n");
 fflush( stdin );
 scanf("%c",&Key);
clrscr();
 switch (Key)
 {
 case '1':
 clrscr();
 Begin = CreateList();
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch(); 

 clrscr();
 break;
 case '2':
 clrscr();
 DisplayList(Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '3':
 clrscr();
 RemoveTermBegin(&Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '4':
 clrscr();
 RemoveTermTag(Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '5':
 clrscr();
 RemoveTermEnd(Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '6':
 clrscr();
 AddTermBegin(&Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch(); 

 clrscr();
 break;
 case '7':
 clrscr();
 AddTermEnd(Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '8':
 clrscr();
 AddTermTag(Begin);
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 case '9':
 FreeList(&Begin);
 DisplayList(Begin);
 printf("\n\nPress any key to ”
“exit...\n");
getch();
 return 0;
 default:
 clrscr();
 printf("\nIncorrect input!!! Try ”
“again!!!\n");
 printf("\n\nPress any key to return in ”
“the menu...\n");
 getch();
 clrscr();
 break;
 }
 }
}
struct List* CreateList(void)
{
 struct List* Begin = NULL, *Previos = NULL, 

*Current= NULL;
 char Ok = 'y';
 while (Ok == 'y')
 {
 printf("\nEnter the numbers of List: ");
 Current =
( struct List*)malloc(sizeof(struct List));
 if (Begin==NULL)
Begin = Current;
 else
Previos -> Next = Current;
 Previos = Current;
 scanf("%d",&Current -> x);
 fflush( stdin );
 printf("\nContinue? (Y/N):");
 scanf("%c",&Ok);
 }
 Previos -> Next = NULL;
 return Begin;
}
void DisplayList(struct List* Begin)
{
 struct List* Current = Begin;
if ( Begin == NULL )
 {
 printf("List is empty\n");
 return;
 }
 printf("\nThe List of numbers:\n");
 while (Current)
 {
 printf("%d ",Current -> x);
 Current = Current -> Next;
 }
 printf("\n");
} 

void RemoveTermBegin(struct List** Begin)
{
 struct List** Current = Begin;
 *Begin = (*Current) -> Next;
 free(Current);
}
void RemoveTermTag(struct List* Begin)
{
 struct List* Current = Begin, *Previos = Current;
 int NumberTag;
 printf("\nEnter number-key: ");
 scanf("%d",&NumberTag);
 while ( Current )
 {
 if ( Current -> x == NumberTag )
 {
 Previos -> Next = Current -> Next;
 free(Current);
 Current = Previos -> Next;
 }
 else
 {
 Previos = Current;
 Current = Current -> Next;
 }
 }
}
void RemoveTermEnd(struct List* Begin)
{
 struct List* Current = Begin, *Previos;
 while (Current -> Next)
 {
 Previos = Current;
 Current = Current -> Next;
 }
 Previos -> Next = NULL;
 free(Current);
} 
95
void AddTermBegin(struct List** Begin)
{
 struct List* Current =
(struct List*)malloc(sizeof(struct List));
 printf("\nEnter number: ");
 scanf("%d",&Current -> x);
 Current -> Next = *Begin;
 *Begin = Current;
}
void AddTermEnd(struct List* Begin)
{
 struct List* Current = Begin, *Previos;
 while (Current)
 {
 Previos = Current;
 Current = Current -> Next;
 }
 Current = (struct List*)malloc(sizeof(struct List));
 Previos -> Next = Current;
 printf("\nEnter the number:\n");
 scanf("%d",&Current -> x);
 Current -> Next = NULL;
}
void AddTermTag(struct List* Begin)
{
 struct List* Current = Begin, *Previos = Current;
 int NumberTag;
 printf("\nEnter number-tag: ");
 scanf("%d",&NumberTag);
 while ( Current )
 {
 if ( Current -> x == NumberTag )
 {
 Previos = Current;
 Current =
(struct List*)malloc(sizeof(struct List)); 

 Current -> Next = Previos -> Next;
 Previos -> Next = Current;
 printf("\nEnter the number:\n");
 scanf("%d",&Current -> x);
 break;
 }
 else
 {
 Previos = Current;
 Current = Current -> Next;
 }
 }
}
void FreeList(struct List** Begin)
{
 struct List* Current = *Begin;
 while (Current)
 {
 *Begin = (*Begin) -> Next;
 free(Current);
 Current = *Begin;
 }
} 
7
  • 1
    "Почему код не работает?" Что такое "не работает"? 18 дек 2018 в 16:45
  • ошибки выдаёт :(
    – Zars.Stars
    18 дек 2018 в 16:46
  • 1
    Какие "ошибки"? Компиляции? Выполнения? Где именно? Почему эта информация не приведена в вопросе? Вопрос не риторический: я хочу подробное объяснение, почему вы скрываете эту информацию. 18 дек 2018 в 16:51
  • undefined reference to 'clrscr'
    – Zars.Stars
    18 дек 2018 в 17:08
  • @Zars.Stars это означает, что при линковке линковщик не нашел этой функции ни в одной из подключаемых библиотек. Так что либо не подключили библиотеку с этой функцией, либо, если это ваша функция, то у нее отсутствует реализация. 18 дек 2018 в 17:12

1 ответ 1

0

Функция clrscr() работает только на компиляторе Borland (Turbo C). Но вы можете использовать функцию system("cls").

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.