lab-8 - all the elements in the list listrec *p1;...

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

View Full Document Right Arrow Icon
#include<iostream> using namespace std; u struct listrec //the structure that later initializes a singly linked list in main() { int value; //values contained in the list struct listrec *next; //serves as a pointer to the next element of the list / int listsize(listrec x, listrec *next) //takes in a listrec data object and next pointer { //outputs the number of elements in the list listrec *p1; //initializes a pointer variable to a listrec data object p1=&x; //p1 is now the memory address of x int count=0; i while(p1 != NULL) //loop ends when the end of the list is reached by the pointer p1 { count++; p1 = p1->next; } return count; //count is returned }; } int listsum(listrec x, listrec *next) //takes in a listrec data object and next pointer { //outputs the sum of
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: all the elements in the list listrec *p1; p1=&amp;x; int sum=0; i while(p1 != NULL) { sum+=x.value; p1 = p1-&gt;next; } return sum; //sum is returned }; } }; } void main() { struct listrec loc1,loc2,loc3,listrec; //initialization of count and sum variables loc1.value = 4; //as well as the single linked list to be tested loc1.next = &amp;loc2; loc2.value = 5; loc2.next = &amp;loc3; loc3.value = 3; loc3.next = (struct listrec*)NULL; int count,sum; i count=listrec.listsize(loc1,loc2.next); cout&lt;&lt;&quot;The number of elements in the linked list is: &quot;&lt;&lt;count&lt;&lt;endl; //desired output / sum=count=listrec.listsum(loc1,loc2.next); cout&lt;&lt;&quot;The sum of the elements in the linked list is: &quot;&lt;&lt;sum&lt;&lt;endl; //desired output / system(&quot;pause&quot;); }...
View Full Document

This note was uploaded on 04/25/2010 for the course CECS CIS 200 taught by Professor Jieshen during the Winter '08 term at University of Michigan-Dearborn.

Page1 / 2

lab-8 - all the elements in the list listrec *p1;...

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