C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2004, 11:18 AM
Theledge21 Theledge21 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 7 Theledge21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 12 sec
Reputation Power: 0
Removing node from a linked list

Hi

So far i have come up with this code for a linked list but i need to know how to remove a node from the linked list,
i know i have to search for the node then remove it by setting the current array pointer to the previous one but im not sure which
way to go about it!
#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 10;
int size = 0;
int headOfList = -1;
int freeList = 0;
typedef struct { string name;
int next;
}node;
node list[MAXSIZE];
void addToList();
void displayList();
void removefromlist();
bool isFull()
{ return (size==MAXSIZE); }
bool isEmpty()
{ return (size==0); }

void init()
{
for(int x=0; x<MAXSIZE;x++)
list[x].next = x+1;

list[MAXSIZE-1].next = -1;
}

void main()
{
int choice = 0;
init();

while (choice != 4)
{
cout << "\nMain Menu\n\n1. Add to List\n2. Remove from List\n3. Display list";
cout << "\n4. Exit\n\n Select: ";
cin >> choice;
cin.get();
switch(choice)
{
case 1 : addToList();
break;
case 2 : removefromlist();
break;
case 3 : displayList();
break;
case 4 : cout << "\nEnd of Program\n\n";
break;
default : cout << "\nInvalid selection. Try again: \n";
}
}
cout << "\n\n";
}

void addToList()
{
int current, previous;
bool found;

if (isFull())
{
cout << "\nList is full\n";
}
else
{
cout << "\nEnter a name: ";
getline(cin, list[freeList].name);
int newSlot = freeList;
freeList=list[freeList].next;

if (isEmpty())
{
headOfList = newSlot;
list[newSlot].next = -1;
}
else
{
current = headOfList;
previous = current;
found = false;
while ((current != -1) && (found != true))
{
if (list[newSlot].name < list[current].name)
{
if (current == headOfList)
{
list[newSlot].next = current;
headOfList = newSlot;
}
else
{
list[previous].next = newSlot;
list[newSlot].next = current;
}
found = true;
}
else
{
previous = current;
current = list[current].next;
}
} // of while
if (found != true)
{
list[previous].next = newSlot;
list[newSlot].next = -1;
}
} // of else
size++;
} // of first else
} // of function

void displayList()
{
int current;
if (isEmpty())
{
cout << "\nList is empty. Nothing to display!\n\n";
}
else
{
current = headOfList;
cout << endl;
while (current != -1)
{
cout << list[current].name << endl;
current = list[current].next;
}
cout << endl;
}
}

Reply With Quote
  #2  
Old September 30th, 2004, 02:19 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Try this

In keeping with your line of thought, try what's below. Just a note though,
you might want to have a look at pointers, if you're not familiar with them.
They provide a cleaner, and I'd say easier solution, once you understand them.

void removefromlist() {
string nameToRemove = string();

if (isEmpty()) {

cout << "\nNothing to remove.\n\n";
}
else {

cout << "\nWhich name would you like to remove?\n";
displayList();
cout << "Select: ";

getline(cin, nameToRemove);


int current = headOfList;
int previous = -1;

while (current != -1)
{
if ( list[current].name == nameToRemove) {

//need to adjust the "link" pointing to current
//so that it now points to what current points to

if (previous != -1) {

//anything but the head of this list
list[previous].next = list[current].next;

}
else {

//move headOfList to point to what current points to
headOfList = list[current].next;
}

//done
break;

}

previous = current;
current = list[current].next;

} //while loop

} //else


} //removefromlist()

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Removing node from a linked list


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway