Lab8 - #include<iostream> using...

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

View Full Document Right Arrow Icon
Thomas Nanke CIS 200-001 CIS 200L-001 Lab #8 Singly Linked List 3/24/08
Background image of page 1

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

View Full DocumentRight Arrow Icon
The Machine type that I used is the PC with Microsoft XP Professional as the operating system. The compiler type is the Microsoft Visual 6.0 Compiler. The goal of this lab was to write two different functions that would work with a singly linked list named listrec . Function listsize was supposed to return the number of elements in the given linked list, and function listsum was supposed to return the sum of those elements. I designed both of my functions with two input parameters, the first one being an arbitrary listrec data object and the second parameter being a next pointer, so that the next element of the list could be obtained no matter the size of the list. My program did manage to output correct results on the screen, and can work for plenty of other singly linked lists, not just the one given. Here is a copy of my source code (well-commented in the .cpp file)
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include<iostream> using namespace std; struct listrec { int value; struct listrec *next; int listsize(listrec x, listrec *next) { listrec *p1; p1=&x; int count=0; while(p1 != NULL) { count++; p1 = p1->next; } return count; }; int listsum(listrec x, listrec *next) { listrec *p1; p1=&x; int sum=0; while(p1 != NULL) { sum+=x.value; p1 = p1->next; } return sum; }; }; void main() { struct listrec loc1,loc2,loc3,listrec; loc1.value = 4; loc1.next = &loc2; loc2.value = 5; loc2.next = &loc3; loc3.value = 3; loc3.next = (struct listrec*)NULL; int count,sum; count=listrec.listsize(loc1,loc2.next); cout<<"The number of elements in the linked list is: "<<count<<endl; sum=count=listrec.listsum(loc1,loc2.next); cout<<"The sum of the elements in the linked list is: "<<sum<<endl; system("pause"); } Test Run:...
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 / 3

Lab8 - #include<iostream> using...

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

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