5 Replies - 2198 Views - Last Post: 08 May 2009 - 05:14 AM Rate Topic: -----

#1 Hayling  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Linked list / pointers help

Posted 07 May 2009 - 06:09 PM

My assignment is this: "read in a sequence of integers, store them in a linked list, and then print them one at a time. To implement it, you should use pointers and dynamic memory allocation."

Looks pretty simple, right?

Well my professor has given us a few snippets of code that seem like they would apply to this lab, but I am pretty clueless on how to get them to work. Here is what I've got so far.

#include <iostream>
using namespace std;

struct node {
	int data;
	node *next;
};

void main()
{
		// To create a list of 10 numbers read in from the keyboard
	node q = new node;
	node *head;
	head = q;
	q->data = 10;	// assume the list contains 10 numbers

	for(i=0; i<10; i++)
	{
		node *p = new node;
		cin >> p->data;
		q->next = p;
		q = p;
	}
}



Here is my error output:
lab12.cpp(19) : error C2440: 'initializing' : cannot convert from 'node *' to 'node'
		No constructor could take the source type, or constructor overload resolution was ambiguous
lab12.cpp(21) : error C2440: '=' : cannot convert from 'node' to 'node *'
		No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
lab12.cpp(22) : error C2819: type 'node' does not have an overloaded member 'operator ->'
lab12.cpp(12) : see declaration of 'node'
		did you intend to use '.' instead?
lab12.cpp(22) : error C2232: '->node::data' : left operand has 'struct' type, use '.'
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(28) : error C2819: type 'node' does not have an overloaded member 'operator ->'
lab12.cpp(12) : see declaration of 'node'
		did you intend to use '.' instead?
lab12.cpp(28) : error C2232: '->node::next' : left operand has 'struct' type, use '.'
lab12.cpp(29) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'node *' (or there is no acceptable conversion)
lab12.cpp(15): could be 'node &node::operator =(const node &)'
		while trying to match the argument list '(node, node *)'



This stuff is a big change from what we have done previously in the semester, so I'm a bit lost.

This is one of my last assignments and I could really use some help figuring it out!

Thanks

This post has been edited by Hayling: 07 May 2009 - 06:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Linked list / pointers help

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Linked list / pointers help

Posted 07 May 2009 - 06:23 PM

View PostHayling, on 7 May, 2009 - 09:09 PM, said:

...
	for(i=0; i<10; i++)
	{
		node *p = new node;
		cin >> p->data;
		q->next = p;
		q = p;
	}
...



Here is my error output:
...
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(24) : error C2065: 'i' : undeclared identifier
lab12.cpp(28) : error C2819: type 'node' does not have an overloaded member 'operator ->'
...




Well, this isn't the answer to your entire problem, but in the for() loop, you use the variable i without declaring it first. Try for(int i = 0; whatever; whatever) and see if that solves any problems
Was This Post Helpful? 0
  • +
  • -

#3 Hayling  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Re: Linked list / pointers help

Posted 07 May 2009 - 06:31 PM

Alright, now my error output is smaller I think:

lab12.cpp(19) : error C2440: 'initializing' : cannot convert from 'node *' to 'node'
		No constructor could take the source type, or constructor overload resolution was ambiguous
lab12.cpp(21) : error C2440: '=' : cannot convert from 'node' to 'node *'
		No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
lab12.cpp(22) : error C2819: type 'node' does not have an overloaded member 'operator ->'
lab12.cpp(12) : see declaration of 'node'
		did you intend to use '.' instead?
lab12.cpp(22) : error C2232: '->node::data' : left operand has 'struct' type, use '.'
lab12.cpp(28) : error C2819: type 'node' does not have an overloaded member 'operator ->'
lab12.cpp(12) : see declaration of 'node'
		did you intend to use '.' instead?
lab12.cpp(28) : error C2232: '->node::next' : left operand has 'struct' type, use '.'
lab12.cpp(29) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'node *' (or there is no acceptable conversion)
lab12.cpp(15): could be 'node &node::operator =(const node &)'
		while trying to match the argument list '(node, node *)'


Was This Post Helpful? 0
  • +
  • -

#4 Hayling  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Re: Linked list / pointers help

Posted 07 May 2009 - 07:56 PM

Okay well I got the program to run simply by changing

node q = new node;



to

node *q = new node;



Now I just need to "print them one at a time".

How would I go about outputting each number in a linked list?

cout << q->data;



outputs the LAST number I input, but I need to output each number.

Any ideas?

This post has been edited by Hayling: 07 May 2009 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Linked list / pointers help

Posted 08 May 2009 - 02:47 AM

Read and enjoy
http://www.cprogramm...l/lesson15.html
Was This Post Helpful? 1
  • +
  • -

#6 Hayling  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-March 09

Re: Linked list / pointers help

Posted 08 May 2009 - 05:14 AM

Thanks janotte, that tutorial helped a lot! I got it working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1