Click to See Complete Forum and Search --> : Freakin Link Lists...


m316foley
12-20-2002, 02:17 AM
#include "iostream.h"
#include "bool.h"

struct node
{
int number;
node * next;
};

void create(node * &head);
void print(node * head);
void kill(node * &head);
void insert(node * & head);

int main()
{
node * head=NULL;
create(head);
print(head);

for(int i=0;i<4;i++)
{
kill(head);
print(head);

insert(head);
print(head);
}
}

void create(node * &head)
{
int num,flag=0;
node * now;

cout << "Enter a positive number, or a negative number to quit: ";
cin >> num;

if(num>0)
{
head = new node;
now = head;
}
while (num > 0)
{

if(flag==0)
{
now->number=num;
flag++;
}
else
{
node * newnode = new node;
newnode->number = num;
now->next = newnode;
now = now->next;
}
cout << "Enter a positive number, or a negative number to quit: ";
cin >> num;
if(num<0)
now->next = NULL;

}

}

void print(node * head)
{
node * now = head;

cout << "\nOriginal List\n";
if (now == NULL)
cout << "List is empty"<<endl;

while (now != NULL)
{
cout << now->number<<endl;
now = now->next;
}
}

void kill(node * &head)
{
bool found = false;
int search;
cout << "\nWhich number would you like to delete? ";
cin >> search;
if(head!=NULL)
{

node * before = head;
node * now = head->next;
node * after = now->next;

if(before->number==search)
{
head=before->next;
found = true;
}
else
{
while (now != NULL)
{

if (now->number == search)
{
found = true;
before->next = now->next;
delete now;
now = before->next;
if(after==NULL)
break;
after=now->next;
}
else
{
if(after==NULL)
break;
before=before->next;
now=now->next;
after = after->next;

}
}
}
}
if (!found)
cout << "\nThere is no "<<search<<" in the list\n";
}

void insert(node * & head)
{
node * after;
bool putIn = false;

if(head==NULL)
{
head = new node;
head->next=NULL;
int newnumber;
cout << "\nEnter the number you wish to insert: ";
cin >> newnumber;
head->number=newnumber;
putIn = true;
}
else
{
bool putIn = false;
node * before = head;
node * now = head->next;
if(now!=NULL)
node * after = now->next;
if(now==NULL)
{
int newnumber;
cout << "\nEnter the number you wish to insert: ";
cin >> newnumber;

node * newnode = new node;
newnode -> number = newnumber;
if(before->number>newnumber)
{
newnode->next = head;
putIn = true;
head=newnode;
}
else if(before->number<newnumber)
{
before->next=newnode;
newnode->next=now;
putIn = true;
}
}
else
{

int newnumber;
cout << "\nEnter the number you wish to insert: ";
cin >> newnumber;
node * newnode = new node;
newnode -> number = newnumber;
while (now != NULL)
{
if(before->number>newnumber)
{
newnode->next = head;
putIn = true;
head=newnode;
break;
}
if (now->number < newnumber)
{
if(after==NULL)
break;
before = before->next;
now = now->next;
after = after->next;
}
else
{
before->next = newnode;
newnode -> next = now;
putIn = true;

break;
}
}
if (!putIn)
{
now->next = newnode;
newnode->next = after;
}
}
}

}


What I need to be able to do:

1. Create a list with 2,4,6,8
2. Delete a 2
3. Insert a 2
4. Delete a 6
5. Insert a 7
6. Delete a 8
7. Insert a 10
8. Create a list with nothing on it
9. Delete a 7
10. Insert a 7
11. Create a list with a 2 in it
12. Kill the 2
13. Insert a 3
14. Create a list with 10,2,8,3,7,11,1,14 and sort the list.

Now I really don't care about the sort, but everything else is pretty much a must.
I can get it to do steps 8-10 just fine.
I can get it to do steps 11-13 just fine.
Steps 1-4 work fine until I get to step 7.
When I input a 7 it overrides the original 8.

Frostburn
12-20-2002, 04:45 PM
I didn't look thoroughly through the program, (thats your job) but I did notice this:


while (now != NULL)
{
if(before->number>newnumber)
{
newnode->next = head;
putIn = true;
head=newnode;
break;
}
if (now->number < newnumber)
{
if(after==NULL)
break;
before = before->next;
now = now->next;
after = after->next;
}
else
{
before->next = newnode;
newnode -> next = now;
putIn = true;

break;
}
}
if (!putIn)
{
now->next = newnode;
newnode->next = after;
}
}

At the end, if the nodes are equal. You set the previous node to the inserted but didn't set the inserted to the previous' one's next

original:

[before]---[after]

it should look like this:

[before]---[now]---[after]

but right now, I believe it looks like:

[before]---[now]

Frostburn
12-20-2002, 04:49 PM
Oh, just a little programming tip, use 4 spaces instead of tabs. It makes a world of difference to people on other systems where indents are different lengths.

Grizzly
12-20-2002, 04:59 PM
Originally posted by Frostburn
Oh, just a little programming tip, use 4 spaces instead of tabs. It makes a world of difference to people on other systems where indents are different lengths.

I'm not sure if get your drift, care to elaborate on that? If I use a tab (\t) character, which happens to be represented as 4 spaces on my system, what does it matter if "Joe" sees my code when his system understands tabs as 5 spaces? A tab is a tab is it not?

It's kind of like typing "Hello" in a web page. My system's default font is Times New Roman, so that is the font I'll see "Hello" rendered in. But if Mr. TreeHugger views my web page on his Mac, he'll see "Hello" rendered in a different font. Does it really matter? No, because we both end up reading the word "Hello" when all is said and done.

I have a feeling I didn't understand the point you were trying to make, so I'm trying to explain my train-of-thought here. Hopefully you can guide me through your reasoning as well.

Frostburn
12-20-2002, 05:28 PM
I've always been taught to use spaces instead of tabs because tabs can very from machine to machine and operating system to operating system.

Lets say you programmed a c file, to be used on a command line. Instead of spaces you used tabs. If someone else wants to edit that file on their dos system, and their tab margin is lets say 16 characters instead of 4, all the lines start to wrap around. Thus creating hard to follow code.

I use [shameless plug]www.jedit.org[/shameless plug] and it has a feature "soft tabs," which allows the tab key to insert 4 spaces instead of a \t characater. (Lots of text editors have this function emacs, textpad and even the ms dos editor)

All in all, it's so anyone who looks at the file will see the exact same thing as the original programmer.

Frostburn
12-20-2002, 05:29 PM
It's also for printing reasons, not all printers print the same amount of space per tab.

Grizzly
12-20-2002, 05:51 PM
What system uses 16 characters as a tab margin? That's ridiculous! :p

I can see your point, I guess I just don't agree that it's a big deal. To me, if someone has their system configured to view tabs with a ridiculous amount of characters, AND they have some silly auto-wrapping going on, than it's their own fault if there's code they find difficult read.

To me, it's the same thing as complaining that CNN.com looks funny in your Netscape 1.0 browser. What do you expect?

Frostburn
12-20-2002, 05:54 PM
Job security =)