Home › Forums › C Programming › Structures and Algorithms in C++ › Re: Re: Structures and Algorithms in C++
January 8, 2010 at 9:47 pm
#3622
GWILouisaxwzkla
Participant
Heres a simple version ( the items are entered into the list from the back )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : January,8,2010<br /> * Comments : new project<br /> * Compiler/Assembler : Visual C++ 6.0<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 4:13:24 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include < iostream ><br /> //#include < string.h ><br /> //#include < conio.h ><br /> //#include < math.h ><br /> //#include < iomanip ><br /> //#include < ctype.h ><br /> <br /> using namespace std;<br /> <br /> //node for the list<br /> struct node<br /> {<br /> int data;<br /> node * next;<br /> };<br /> <br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> cout << "enter numbers ( 0 to stop ) " << endl << endl;<br /> <br /> node * front = NULL; //front of the linked list<br /> node * temp = NULL;<br /> node * back = NULL;<br /> <br /> int input;<br /> <br /> cout << "enter number: ";<br /> cin >> input;<br /> <br /> //######### READ THE LIST FROM THE KEYBOARD<br /> <br /> while ( input != 0 )<br /> {<br /> temp = new node; //make a new node in the list<br /> if ( temp == NULL ) //check that node was allocated properly<br /> {<br /> cout << "allocation error , node not stored! " << endl;<br /> continue;<br /> }<br /> temp -> data = input;//copy data<br /> temp -> next = NULL; //new item has no next item<br /> if ( front == NULL ) //if the list is empty add a new node<br /> {<br /> front = temp; //front of list points to temp<br /> back = temp; //back of list pointer points to temp<br /> }<br /> else //add the item to the back of the list<br /> {<br /> back -> next = temp;//next item in the list is the item read<br /> back = temp; //new item is the new last item<br /> }<br /> cout << "enter number: "; //get the next number<br /> cin >> input;<br /> }<br /> <br /> <br /> cout << "Printed list: " << endl << endl;<br /> <br /> temp = front; //start at the front of the list<br /> <br /> //############ PRINT THE LIST<br /> <br /> while ( temp ) //while not at the end of the list<br /> {<br /> cout << temp -> data << " " ; //output data in list item<br /> temp = temp -> next; //go to next item in the list<br /> }<br /> <br /> //######## DELETE THE LIST<br /> <br /> while ( front )<br /> {<br /> temp = front; //save the address of the front node<br /> front = front -> next; //make the next item first<br /> delete temp; //deallocate memory for the old front node<br /> }<br /> <br /> <br /> return 0 ;<br /> }<br /> <br /> <br /> <br /> <br /> |