Untitled2 - struct node *pNew = NULL; struct node *current...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* Kanad Biswas / To create a linked list by adding elements at the rear, and to print the list */ * #include <stdio.h> #include <stdlib.h> # struct node { int data; struct node *next; }; } struct node* Addrear(struct node *list, int d) ; void PrintList(struct node *list); v int main( ) { int number = 0; struct node *pList=NULL; // Let users add number into list until they enter a -1. while(number!= -1) { // Get the next number. printf("enter data for next node\n"); scanf("%d", &number); // Add it if it's not -1. if (number !=-1) { pList = Addrear( pList, number ); } } // Print everything out. printf("items in linked list\n"); PrintList (pList); system("PAUSE"); return 1; } // Pre-condition: list points to the head of a linked list. // Post-condition: returns a pointer to the new head of a linked list that // adds a node with d to the end of the original list. struct node* Addrear(struct node *list, int d) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: struct node *pNew = NULL; struct node *current = NULL; // Create the new node. pNew = (struct node*)malloc(sizeof(struct node)); pNew-&gt;data = d; pNew-&gt;next = NULL; // Store front of the linked list current = list; // if list is empty then this becomes the first node. if (list == NULL) return pNew; // Iterate so that current points to the last node in the list. while (current -&gt;next != NULL) current = current-&gt;next; // Link the last node to the new node. current-&gt;next = pNew; // Return a pointer to the edited list. return list; } // Pre-condition: list points to the head of a linked list. // Post-condition: Prints out each item in the list pointed to by list, in order. void PrintList( struct node *list) { // Iterate through each item and print it! while (list != NULL) { printf(&quot;--&gt;%d&quot;, list-&gt;data); list = list-&gt;next; } printf(&quot;\n&quot;); }...
View Full Document

This document was uploaded on 07/12/2009.

Page1 / 2

Untitled2 - struct node *pNew = NULL; struct node *current...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online