Random element deleting

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

Сообщение HD295 » Сб май 16, 2009 7:32 pm

Помогите пожалуйста сделать случайное удаление из списка. Вот изначальный код:
#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
Призывник
 
Сообщений: 2
Зарегистрирован: Сб май 16, 2009 7:23 pm
Пункты репутации: 0

Сообщение DruiD » Сб май 16, 2009 9:22 pm

Код: выделить все

#include <iostream>
#include <stdlib.h>
using namespace std;
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;
int count  = 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;
}
count++;
}

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;
}
}
count--;
}

void delete_random_node(){
  if(!count) return;
  
  if(count==1){
    delete start_ptr;
    start_ptr=NULL;
    count--;
    return;
  }

  node *temp1, *prev;
  int node=rand()%count;  
  int i=0;

  cout << "Deleting " << node << " node."<<endl;
  if(node==0){
    temp1=start_ptr;
    start_ptr=temp1->nxt;
    delete temp1;
    count--;
    return;
  }
  else{
    temp1=start_ptr;
    while(1){
      if(i++==node){
    if(temp1->nxt!=NULL)
      prev->nxt=temp1->nxt;
      if(node+1==count){
        prev->nxt=NULL;
      }
      delete temp1;
      count--;
      return;
      }
      prev = temp1;
      temp1=temp1->nxt;
    }
  }
}

int main()
{ start_ptr = NULL;
srand((unsigned) time(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 random TV channel from the list." << endl;
cout << "3. Delete the last TV channel from the list." << endl;
cout << endl << " >> ";
cin >> option;

switch (option)
{
case 1 : add_node_at_end(); break;
case 2 : delete_random_node(); break;
case 3 : delete_end_node(); break;
}
}
while (option != 0);
}
"Вы можете тысячу раз обвинять нас, богиня вечного суда истории усмехнется, разорвет приговор вашего суда, и, после, объявит нас свободными".
А. Гитлер, Mein Kampf.
DruiD
Полковник
 
Сообщений: 1171
Зарегистрирован: Чт июн 02, 2005 3:13 pm
Откуда: Зеленоград
Пункты репутации: 0

Сообщение HD295 » Вс май 17, 2009 10:39 am

Спасибо огромное! Всё работает! :)
Последний раз редактировалось HD295 Вс май 17, 2009 10:58 am, всего редактировалось 1 раз.
HD295
Призывник
 
Сообщений: 2
Зарегистрирован: Сб май 16, 2009 7:23 pm
Пункты репутации: 0


Вернуться в Программирование

Кто сейчас на форуме

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 7

cron