User Rating:      By 2 Users

Compatibility:C++ (general)

Users have accessed this code 2376 times.

  (About the author) 

 

  

     This will create a linked list through user input, and will output the linked list. This is to help you how to get linked list through user input, instead of writing it out the long way. This can be useful for those of you in high school who will be taking the AB exam for the AP Computer Science exam in May.

 

 

  

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//**************************************

//     

// Name: Linked Lists through user input

//     

// Description:This will create a linked

//     list through user input, and will output

//     the linked list. This is to help you how

//     to get linked list through user input, i

//     nstead of writing it out the long way. T

//     his can be useful for those of you in hi

//     gh school who will be taking the AB exam

//     for the AP Computer Science exam in May.

//     

// By: 

//

// Inputs:the user enters in various num

//     bers

//

// Returns:its basically "void", but it 

//     just outputs the linked list

//

// Assumes:You need to know some basics 

//     of how a linked list works.

//

//This code is copyrighted and has// limited warranties.Please see http://

//     www.1CPlusPlusStreet.com/xq/ASP/txtCodeI

//     d.393/lngWId.3/qx/vb/scripts/ShowCode.ht

//     m//for details.//**************************************

//     

/*

Linked Lists sample program

this program will create a linked list with user input

and will output that list.

Program created on April 28, 2000

By: David Turner

*/

#include 

struct NODE

    {

     int number;

     NODE *next;

};

// function prototypes

void OutputList (NODE* &head);

// Main

int main()

    {

     // Dimension local variables

     int number_entered;

     NODE *list = new NODE, *current_list = list, *current_address, *previous_address;

     cout << "\tWelcome user!\n\nThis program will help with linked lists!\n\n"

      << "When promted, enter in a number\n";

     for (;; list = list->next) 

         {

        

         cout << "\nPlease enter in a number (0 to quit) ==> ";

         cin >> number_entered;

        

         if (number_entered)

             {

             previous_address = current_address;

             list->number = number_entered;

             }

             else 

                 {

                 list = previous_address->next = NULL;

                 delete current_address; // removes the new NODE created

                 break;

                 }

                

                 current_address = list->next = new NODE;

                 }

                

                 cout << "\n\nThe current list is: ";

                 OutputList (current_list);

                

                 cout << "\n\nThanks for using this program!\n";

                 return 0;

            }

            void OutputList (NODE* &head)

            // precondition: a linked list is entere

            //     d

            // postcondition: the linked list is out

            //     puted

                {

                 NODE* outputlist;

                 for (outputlist = head; outputlist; outputlist = outputlist = outputlist->next)

                 cout << outputlist->number << ' ';

            }