Linked lists.... help!!

This is a discussion on Linked lists.... help!! within the C++ Programming forums, part of the General Programming Boards category; I'm trying to practice linked lists and stacks, specifically using the -> operator, but my book is missing atm and ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Linked lists.... help!!

    I'm trying to practice linked lists and stacks, specifically using the -> operator, but my book is missing atm and I'd like to find some simple, clean cut examples of linked lists... not all the crap I keep finding in my search that has an entire class defined for it and such... I just want a clean cut example!!

    Thanks
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    simple single-linked list, without any "class crap" as you stated with (->) .......................enjoy...

    // Compiler: Microsoft Visual C++ (ver. 6.0)
    //
    // Task: Write a program to do the following:
    // The user will be asked to enter a list of integer values.
    // Each value will be stored in a struct data structure.
    // The structs will be put into a linked list.
    // Show the linked list after it has been loaded.
    // The user will be asked to choose one value to remove from the list
    // Show the linked list after the value has been removed
    // Show the average of the integers in the list.
    //
    // Format of output
    //
    // enter integer values; use 0 to quit
    //
    // 1 2 3 4 5 6 7 8 9
    //
    // HEAD->1->2->3->4->5->6->7->8->9->NULL
    //
    // average is: 5
    //
    // which value do you want to delete? 8
    //
    // HEAD->1->2->3->4->5->6->7->9->NULL
    //
    // average is: 4.625
    //
    // Last Updated: 11/22/2000
    //
    /////////////////////////////////////////////////////////////////////////////////////////

    //---------------------------------------------------------------------------------------
    // Preprocessor directives

    #include<iostream>
    using namespace std;

    //---------------------------------------------------------------------------------------
    // Function prototypes

    void create_list();
    void show_list();
    void remove_value(int);
    void average();

    //---------------------------------------------------------------------------------------
    // Declaration of a node

    struct node
    {
    int value;

    node *next; // pointer to next node in the list...
    };

    //---------------------------------------------------------------------------------------
    // Global variables

    node *head = NULL; // global pointer to the first node in the list

    //---------------------------------------------------------------------------------------
    // Driver function

    int main()
    {
    create_list();

    int remove;

    cout << "Which value do you want to remove from the list? ";
    cin >> remove;
    cout << endl;

    remove_value(remove); // will delete variable (remove) from the list

    return 0;
    }

    //---------------------------------------------------------------------------------------
    // create_list(): creates a linked list
    //
    // Parameters: none
    // Return Value: void

    void create_list()
    {
    node *cur, *new_node;

    int temp; // temporary variable for (int) value of node

    new_node = new node; // creates a node on the heap

    head = new_node; // head will point to the first node in
    // the list
    cur = head;

    cout << "Please enter an integer, use 0 (zero) to quit: \n\n";
    cin >> temp;


    if(temp != 0)
    {
    cur->value = temp;
    }


    while(temp != 0)
    {
    new_node = new node; // keeps on adding nodes to the list
    cur->next = new_node; // as long as the user won't enter 0 (zero)
    cur = new_node;

    cin >> temp;

    if(temp != 0)
    {
    cur->value = temp;
    }

    }

    cur->next = NULL; // last's nodes pointer (next) must point
    // to NULL

    show_list(); // will display contents of a list to
    // the screen

    average(); // will display average of all values onto
    // the screen...

    cur = NULL;
    new_node = NULL;

    return;
    }

    //---------------------------------------------------------------------------------------
    // show_list(): will output the contents of a linked lists to the screen
    //
    // Parameters: none
    // Return Value: void

    void show_list()
    {
    cout << endl;

    node *cur;
    cur = head;

    cout << "HEAD->";

    while(cur->next != NULL)
    {
    cout << cur->value << "->";
    cur = cur->next;
    }

    cout << "NULL \n";

    return;

    }

    //---------------------------------------------------------------------------------------
    // remove_value(): will remove the desired value from the linked list
    //
    // Parameters: integer (value whose node will be removed from the list)
    // Return Value: void

    void remove_value(int remove)
    {
    node *cur;
    node *prev;
    cur = head;

    if(cur->value == remove) // if the desired (int) to remove is the
    { // the first item in the list...
    cur = head;
    head = head->next;
    delete cur; // ** special case **
    cur = NULL;
    }

    else
    {
    while( (cur->value != remove) && (cur->next != NULL) )
    {
    prev = cur;
    cur = cur->next;

    if(cur->value == remove)
    {
    prev->next = cur->next; // will exit the loop at first occurence
    delete cur; // of desired (user-inputed) variable
    cur = NULL; // called (remove)...
    prev = NULL;
    break;
    }
    }
    }

    show_list(); // diplays contents of a new node

    average(); // diplays average of all values

    return;
    }

    //---------------------------------------------------------------------------------------
    // average(): displays the average of all integers that linked list contains
    //
    // Parameters: none
    // Retur Value: void

    void average()
    {
    cout << endl;

    node *cur;
    cur = head;

    double average;
    int sum = 0;
    int counter = 0;

    while(cur->next != NULL) // traverses the entire linked list
    {
    sum = sum + cur->value;
    counter++;
    cur = cur->next;
    }

    average = ( (static_cast<double>(sum)) / counter);

    cout << "The average of values is: " << average << "\n\n";

    return;
    }

    //---------------------------------------------------------------------------------------

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ooooooooooops...........sorry.......forgot the code tags....


    Code:
    // Compiler: Microsoft Visual C++ (ver. 6.0)
    //
    // Task:	 Write a program to do the following:
    //			 The user will be asked to enter a list of integer values. 
    //			 Each value will be stored in a struct data structure.
    //			 The structs will be put into a linked list.
    //			 Show the linked list after it has been loaded.
    //			 The user will be asked to choose one value to remove from the list
    //			 Show the linked list after the value has been removed
    //			 Show the average of the integers in the list.
    //
    //						Format of output
    //
    //				enter integer values; use 0 to quit 
    //
    //				1  2  3  4  5  6  7  8  9
    //
    //				HEAD->1->2->3->4->5->6->7->8->9->NULL
    //
    //				average is: 5
    //
    //				which value do you want to delete? 8
    //
    //				HEAD->1->2->3->4->5->6->7->9->NULL
    //
    //				average is: 4.625
    //
    // Last Updated:  11/22/2000
    //
    /////////////////////////////////////////////////////////////////////////////////////////
    
    //---------------------------------------------------------------------------------------
    // Preprocessor directives
    
    #include<iostream>
    using namespace std;
    
    //---------------------------------------------------------------------------------------
    // Function prototypes
    
    void create_list();
    void show_list();
    void remove_value(int);
    void average();
    
    //---------------------------------------------------------------------------------------
    // Declaration of a node
    
    struct node
    {
    	int value;
    
    	node *next;							// pointer to next node in the list...
    };
    
    //---------------------------------------------------------------------------------------
    // Global variables
    
    node *head = NULL;						// global pointer to the first node in the list
    
    //---------------------------------------------------------------------------------------
    // Driver function
    
    int main()
    {
    	create_list();
    
    	int remove;
    
    	cout << "Which value do you want to remove from the list? ";
    	cin >> remove;
    	cout << endl;
    
    	remove_value(remove);				// will delete variable (remove) from the list
    
    	return 0;
    }
    
    //---------------------------------------------------------------------------------------
    // create_list():  creates a linked list
    // 
    // Parameters:   none
    // Return Value: void
    
    void create_list()
    {
    	node *cur, *new_node;
    
    	int temp;								// temporary variable for (int) value of node
    
    	new_node = new node;					// creates a node on the heap
    	
    	head = new_node;						// head will point to the first node in
    											// the list
    	cur = head;
    
    	cout << "Please enter an integer, use 0 (zero) to quit: \n\n";
    	cin >> temp;
    
    	
    	if(temp != 0)
    	{
    		cur->value = temp;
    	}
    
    
    	while(temp != 0)
    	{		
    		new_node = new node;				// keeps on adding nodes to the list
    		cur->next = new_node;				// as long as the user won't enter 0 (zero)
    		cur = new_node;
    
    		cin >> temp;
    
    		if(temp != 0)
    		{
    			cur->value = temp;
    		}
    	
    	}
    
    	cur->next = NULL;						// last's nodes pointer (next) must point
    											// to NULL
    
    	show_list();							// will display contents of a list to
    											// the screen
    
    	average();								// will display average of all values onto
    											// the screen...
    	
    	cur = NULL;
    	new_node = NULL;
    
    	return;
    }
    
    //---------------------------------------------------------------------------------------
    // show_list():   will output the contents of a linked lists to the screen
    //
    // Parameters:   none
    // Return Value: void
    
    void show_list()
    {
    	cout << endl;
    
    	node *cur;
    	cur = head;
    
    	cout << "HEAD->";
    
    	while(cur->next != NULL)
    	{
    		cout << cur->value << "->";
    		cur = cur->next;
    	}
    
    	cout << "NULL \n";
    
    	return;
    
    }
    
    //---------------------------------------------------------------------------------------
    // remove_value():   will remove the desired value from the linked list
    //
    // Parameters:   integer (value whose node will be removed from the list)
    // Return Value: void
    
    void remove_value(int remove)
    {
    	node *cur;
    	node *prev;
    	cur = head;
    
    	if(cur->value == remove)				// if the desired (int) to remove is the 
    	{										// the first item in the list...
    		cur = head;
    		head = head->next;
    		delete cur;							//  ** special case **
    		cur = NULL;
    	}
    
    	else
    	{
    		while( (cur->value != remove) && (cur->next != NULL) )
    		{
    			prev = cur;
    			cur = cur->next;
    			
    			if(cur->value == remove)
    			{
    				prev->next = cur->next;		// will exit the loop at first occurence
    				delete cur;					// of desired (user-inputed) variable
    				cur = NULL;					// called (remove)...
    				prev = NULL;
    				break;
    			}
    		}	
    	}
    
    	show_list();							// diplays contents of a new node
    
    	average();								// diplays average of all values
    	
    	return;
    }
    
    //---------------------------------------------------------------------------------------
    // average():   displays the average of all integers that linked list contains
    //
    // Parameters:  none
    // Retur Value: void
    
    void average()
    {
    	cout << endl;
    	
    	node *cur;
    	cur = head;
    
    	double average;
    	int sum = 0;
    	int counter = 0;
    
    	while(cur->next != NULL)				// traverses the entire linked list
    	{
    		sum = sum + cur->value;
    		counter++;
    		cur = cur->next;
    	}
    
    	average = ( (static_cast<double>(sum)) / counter);
    
    	cout << "The average of values is: " << average << "\n\n";
    
    	return;
    }
    
    //---------------------------------------------------------------------------------------

  4. #4
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Thanks!! It's nice and clean too! Heh, just seemed that all the code examples I could find were defining an entire header file to go with it. Just too much included with it to work around with it.
    I am Error. When all else fails, use fire.

    My Current Screenshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21