Помощь - Поиск - Пользователи - Календарь
Полная версия: Линейный список
Форум «Всё о Паскале» > Современный Паскаль и другие языки > Ада и другие языки
nblazhko
Здравствуйте, есть программа с использование линейного списка,которая запоминает в список число,мне нужно чтобы можно было запомнить слово попытался реализовать так,но она запоминает только 1 букву


#include <vcl.h>
#pragma hdrstop

#include <tchar.h>
#include <iostream.h>
#include <windows.h>

//---------------------------------------------------------------------------
using namespace std;

class Node
{
public:
int number;
char fam;
Node* next;
};
void russia(const char*);
#pragma argsused

int _tmain(int argc, _TCHAR* argv[])
{
Node* head = NULL;
Node* lastPtr = NULL;
short action = -1;
while (1)
{
russia("1. Добавить Элемент\n");
russia("2. Просмотр Списка\n");
russia("3. Поиск Элемента\n");
russia("4. Удалить Элемент\n");
russia("5. Удалить Элемент По Выбору\n");
russia("0. Выход\n\n");
russia("Ваш Выбор: ");
cin>>action;
if (action == 0)
{
system("CLS");
break;
}

if (action == 1)
{
system("CLS");
Node* ptr = new Node;
int numb = -1;
char numb2;

russia("Введите Число: ");
cin>>numb;
ptr->number = numb;

russia("Введите Строку: ");
cin>>numb2;
ptr->fam = numb2;

ptr->next = NULL;
if (head == 0)
{
head = ptr;
lastPtr = ptr;
system("CLS");
continue;
}
lastPtr->next = ptr;
lastPtr = ptr;
system("CLS");
continue;
}

if (action == 2)
{
Node* ptr = NULL;
system("CLS");
if (head == NULL)
{
russia("\t!!! СПИСОК ПУСТ !!!\n\n");
system("PAUSE");
system("CLS");
continue;
}
russia("* * * * * СПИСОК * * * * *\n\n");
ptr = head;
while (1)
{
cout<<ptr->number<<" ";
cout<<ptr->fam;

if (ptr->next == 0)
break;
ptr = ptr->next;
}
cout<<"\n\n";
system("PAUSE");
system("CLS");
continue;
}

if (action == 3)
{
Node* ptr = NULL;
int key = -1;
system("CLS");
if (head == NULL)
{
russia("\t!!! СПИСОК ПУСТ !!!\n\n");
system("PAUSE");
system("CLS");
continue;
}
russia("Введите Элемент Для Поиска: ");
cin>>key;
ptr = head;
while (1)
{
if (key == ptr->number)
{
russia("\n\t!!! ЭЛЕМЕНТ НАЙДЕН !!!\n");
break;
}
if (ptr->next == NULL)
{
russia("\n\t!!! ЭЛЕМЕНТ НЕ НАЙДЕН !!!\n");
break;
}
ptr = ptr->next;
}
system("PAUSE");
system("CLS");
continue;
}

if (action == 4)
{
system("CLS");
Node* ptrDelete = NULL;
if (head == NULL)
{
russia("\t!!! СПИСОК ПУСТ !!!\n\n");
system("PAUSE");
system("CLS");
continue;
}
if (head->next == NULL)
{
head = NULL;
delete head;
continue;
}
ptrDelete = head;
head = ptrDelete->next;
delete ptrDelete;
continue;
}

if (action == 5)
{
system("CLS");
Node* ptrPrev = NULL;
Node* ptrDelete = NULL;
int key = -1;
if (head == NULL)
{
russia("\t!!! СПИСОК ПУСТ !!!\n\n");
system("PAUSE");
system("CLS");
continue;
}
russia("Введите Элемент Для Удаления: ");
cin>>key;
ptrDelete = head;
if (ptrDelete->number == key)
{
head = ptrDelete->next;
delete ptrDelete;
system("CLS");
continue;
}
while (1)
{
if (key == ptrDelete->number)
{
ptrPrev->next = ptrDelete->next;
delete ptrDelete;
break;
}
if (ptrDelete->next == 0)
{
russia("\n\t!!! ЭЛЕМЕНТ НЕ НАЙДЕН !!!\n");
system("PAUSE");
break;
}
ptrPrev = ptrDelete;
ptrDelete = ptrDelete->next;
}
system("CLS");
continue;
}

if (action > 5)
{
system("CLS");
russia("\t!!! НЕВЕРНЫЙ ВЫБОР. ПОВТОРИТЕ ВВОД !!!\n\n");
system("PAUSE");
system("CLS");
continue;
}
}

return 0;
}

void russia(const char* rus)
{
char word[100];
CharToOem(rus, word);
cout<<word;
}
//---------------------------------------------------------------------------





подскажите что не так?
volvo
Цитата
что не так?
Вот это:
Цитата
class Node
{
public:
int number;
char fam; // <--- !!!
Node* next;
};
Ты хочешь хранить строку? Строка это не char, это как минимум char*, я бы сделал так:

class Node
{
public:
int number;
char *fam;
Node* next;
};

// ...
if (action == 1)
{
system("CLS");
Node* ptr = new Node;
int numb = -1;
char numb2[100] = {0}; // Место для хранения вводимой строки
russia("Введите Число: ");
cin>>numb;
ptr->number = numb;
russia("Введите Строку: ");
cin>>numb2; // Получаем строку от пользователя
ptr->fam = new char[strlen(numb2) + 1]; // выделяем в списке место под строку + '\0'
strcpy(ptr->fam, numb2); // копируем введенную пользователем строку в список
ptr->next = NULL;
if (head == 0)
{
head = ptr;
lastPtr = ptr;
system("CLS");
continue;
}
lastPtr->next = ptr;
lastPtr = ptr;
system("CLS");
continue;
}
(кроме всего прочего, у тебя не совсем оптимальный алгоритм, можно было бы обойтись без дублирования кода, и заменить все if(action == ...) на switch(action), но сейчас не об этом)

Естественно, при поиске строки тоже надо пользоваться спец. функцией (strcmp), а не простым "==".
nblazhko
Благодарю! smile.gif
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.