Freakin Link Lists...

Sharky Forums


Results 1 to 8 of 8

Thread: Freakin Link Lists...

  1. #1
    Hammerhead Shark m316foley's Avatar
    Join Date
    Nov 2000
    Posts
    2,706

    Freakin Link Lists...

    Code:
    #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.
    Amd Athlon Mobile 2600+ (200 X 12.5 @ 1.7)
    Asus A7N8X-Deluxe (Not Rev. 2)
    2-80gig WD Hard drives
    Corsair XMS pc3200 - 512megs
    Saphire Radeon 9800pro
    Soundblaster Live Platinum 5.1
    3Com EtherLink 10/100 3CR990-TX-97
    Creative Blaster v.92 Modem
    Sony Trinitron 21inch Monitor
    Plextor 24X Burnproof Cd-Writer
    Pioneer 16X DVD Drive
    Klipsch Ultras 5.1 Surround Sound
    Sony 3.5inch Floppy Drive
    Windows 2k of course...
    -------------------------
    Bart: Hey! Why is it killing my toys?
    Lisa: They must have programed it to destroy the competition.
    Bart: Sort of like Microsoft!
    -------------------------
    *HAVE A NICE DAY*

  2. #2
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    I didn't look thoroughly through the program, (thats your job) but I did notice this:

    Code:
    			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]

  3. #3
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    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.

  4. #4
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077
    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.

  5. #5
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    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.

  6. #6
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    It's also for printing reasons, not all printers print the same amount of space per tab.

  7. #7
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077
    What system uses 16 characters as a tab margin? That's ridiculous!

    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?

  8. #8
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    Job security =)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •