Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 17.05.2009, 00:38   #1
HD295
Новичок
Джуниор
 
Регистрация: 17.05.2009
Сообщений: 1
По умолчанию Random element deleting

Помогите пожалуйста сделать случайное удаление из списка. Вот изначальный код:
#include <iostream.h>

struct node
{ char name[20]; // Name of up to 20 letters
node *nxt;// Pointer to next node
};

node *start_ptr = NULL;
node *current; // Used to move along the list
int option = 0;

void add_node_at_end()
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the name of the channel: ";
cin >> temp->name;
temp->nxt = NULL;

// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}

void display_list()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Name : " << temp->name << " ";
/* cout << "Age : " << temp->age << " ";
cout << "Height : " << temp->height; */
if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;

}
cout << "End of list!" << endl;
}
}

void delete_end_node()
{ node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{ temp1 = start_ptr;
if (temp1->nxt == NULL)
{ delete temp1;
start_ptr = NULL;
}
else
{ while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}

void main()
{ start_ptr = NULL;
do
{
display_list();
cout << endl;
cout << "Please select an option : " << endl;
cout << "0. Exit the program." << endl;
cout << "1. Add a TV channel to the end of the list." << endl;

cout << "2. Delete the last TV channel from the list." << endl;
cout << endl << " >> ";
cin >> option;

switch (option)
{
case 1 : add_node_at_end(); break;

case 3 : delete_end_node(); break;
}
}
while (option != 0);
}
HD295 вне форума Ответить с цитированием
Старый 17.05.2009, 04:29   #2
ISergeyN
Maniac
Форумчанин
 
Аватар для ISergeyN
 
Регистрация: 03.01.2009
Сообщений: 450
По умолчанию

Первый удельный совет - Пользуйтесь тегом CODE(#), и про нормальное форматирование кода не забывайте!!. А то глаза аж косит.
Стандартные библиотеки разработаны с учетом многолетнего опыта лучших программистов и они не больны "детскими болезнями крутизны в программизме"....
ISergeyN вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
random в С++ Swool Общие вопросы C/C++ 21 18.02.2012 21:55
Random W_P Паскаль, Turbo Pascal, PascalABC.NET 2 08.10.2008 00:05
Random Febreze Общие вопросы Delphi 2 28.04.2008 14:17
p:element и q:^element - в чем разница? Arkuz Компоненты Delphi 1 21.04.2008 01:16
deleting files Pavel_aress Общие вопросы Delphi 1 06.08.2007 13:21