#include <iostream>

using namespace std;

typedef struct Template
{
	int a;
	Template * link; // goes forward (one way reference)
	 
} list;

typedef list * listptr;

int main()
{
listptr head; // head is a type listptr
listptr p ;
listptr next ;




 head = NULL;  //therfore make it null
next = NULL;

 for(int i =0 ; i<4 ; ++i)
	 {
p = new list;// create new element of p type of listptr (new structure)
		
		cin >> p->a;

		cout << p->a <<endl;
	
 }
	 
 p->link = next;
cout << p <<endl;
cout << next;
 

return 0;

}

Hey guys, struggling to make a 2nd pointer run after the first. So that it can hold the address of the 2nd to last node. Any suggestions? Whilst I've managed to create the pointer, I'm unsure how to assign the previous memory location to it.

Many Thanks,
John

Which way are you trying to build the list? Are you trying to have the most-recently entered element be at the front of the list, or are you trying to have the most-recently entered element be at the end of the list. Right now, it looks like you want the former, but since you appear very confused, I have to ask.

Which way are you trying to build the list? Are you trying to have the most-recently entered element be at the front of the list, or are you trying to have the most-recently entered element be at the end of the list. Right now, it looks like you want the former, but since you appear very confused, I have to ask.

hey dude, thanks for taking the time to read / reply. The most recent datat item is added to the end. In order to sort and use finding algos on the list there must be 2 pointers, whilst I've declared the 2 points to NULL I cant figure out how to use trailing pointers....

#include <iostream>

using namespace std;

typedef struct Template
{
	int a;
	Template * link; // goes forward (one way reference)
	 
} list;

typedef list * listptr;

int main()
{
listptr head; // head is a type listptr
listptr p ;
listptr next ;




 head = NULL;  //therfore make it null
next = NULL;

 for(int i =0 ; i<4 ; ++i)
	 {
p = new list;// create new element of p type of listptr (new structure)
		
		cin >> p->a;

		cout << p->a <<endl;
	
 }
	 
 p->link = next;
cout << p <<endl;
cout << next;
 

return 0;

}

Hey guys, struggling to make a 2nd pointer run after the first. So that it can hold the address of the 2nd to last node. Any suggestions? Whilst I've managed to create the pointer, I'm unsure how to assign the previous memory location to it.

Many Thanks,
John

Hey dude i can give u the c code for that problem if u want but in C++ i have not clear funsdas///

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.