help me to find answer!

linklist using pointer..

Page 1 of 1

1 Replies - 506 Views - Last Post: 29 May 2008 - 07:56 AM Rate Topic: -----

#1 sid2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-April 08

help me to find answer!

Post icon  Posted 29 May 2008 - 06:52 AM

Hi everyone...I am posting again the question which was asked in my exam actually i got correction in it but i dont know the correct answer if someone have question plz help me bec just for my curiocity bec exam was over and i got too less grade so i just want to know the answer now..



Consider the following C++ declarations for problems 1 through 10. Complete any 7 of the 10 problems. Extra credit will be given to additional correct problems.

typedef int elementType;

class node1{
friend class list1;
private:
elementType data;
node1 * next;
};

class node2{
friend class list2;
private:
elementType data;
node2 * next;
node2 * prev;
};

class list1 {
public:
list1() {first =NULL;}//constructor
. . . //other member functions

private:
node1 * first;
};


class list2 {
public:
list2() {first =NULL;}//constructor
. . . //other member functions

private:
node2 * first;

};

here are the questions


2. Write function to sum the data that in stored in a singly linked list. This function should return an integer representing the sum of the data in the list.

int list1::Sum()


4. Write a member function to find the last node in a doubly circular linked list. This function returns a pointer pointing to the last node in the linked list.

node2 * list2::findlast()


you dont know but giving me the reply of those questions you would help me a lot...

Is This A Good Question/Topic? 0
  • +

Replies To: help me to find answer!

#2 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: help me to find answer!

Posted 29 May 2008 - 07:56 AM

i dont have a compiler in front of me so the syntax might be a little off but it should be more then enough to get you the answers

2) all you have to do for this is step through the link list
int list1::Sum()
{
	//Temp pointer so we can walk through the list
	node1 * walker = first;
	
	int sum = 0;

	//keep going until walker is false
	//  same thing as
	//	while(walker != NULL)
	//	while(walker == true)
	while(walker)
	{
		sum += walker->data;
		walker = walker->next;
	}

	return sum;
}


4) if this is a true circular link list then

node2 * list2::findlast()
{
	//because its circular all we should have to do is
	//look at the first node and return the prev pointer
	return first->prev;
}


please put your code into code blocks [ code] [ /code]

This post has been edited by realNoName: 29 May 2008 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1