Hi I'm currently working on a college programming assignment and for this assignment I need to create a contact list of first name and last name and store them as the program runs

I may not be able to explain this well but I understand what i have to do and i feel like my problem is just basic C logic that i cant quite wrap my head around

so i have

struct contact{//initialize the structure contact
	char *firstname;
	char *lastname;
	struct contact *nextptr;

where nextptr points to a new contact stucture

I start a while loop and, I prompt the user for the first and last name and store that in newptr

then i set the contact list as a whole as listStart=newptr;

now the while loop starts over and newptr gets reset to NULL

now i want to set listStart->nextptr=newptr; which i understand

now the while loop loops and now i want to set listStart->nextptr->nextptr=newptr;

How can I do this without having to type ->nextptr after every loop? and keep adding a ->nextptr for ever how many loops i want?
please let me know if theres any questions. Thanks to anyone that can help, here's the code that i'm working on.

struct contact{//initialize the structure contact
	char *firstname;
	char *lastname;
	struct contact *nextptr;

//initialize subroutines
void PrintContact(struct contact *listStartPtr);

int main(){
int i=0;
char bufferFirstName[200];
char bufferLastName[200];
struct contact *listStartPtr=NULL;
struct contact *newptr=NULL;


	newptr=(struct contact *)malloc(sizeof(struct contact));

		printf("Couldn't allocate memory");

	printf("Please enter your first name or enter 'done' to exit\n:");
	if(strcmp (bufferFirstName,"done") ==0 ){;
			printf("Couldn't allocate memory\n");

	printf("\nPlease enter your last name\n:");
				printf("Couldn't allocate memory\n");
	listStartPtr=newptr;//first run through
	listStartPtr->nextptr=newptr;//second loop
	listStartPtr->nextptr->nextptr=newptr;//third loop

              //How would I keep going for the nth amount of loops?


Solution is quite simple.Don't insert it from the end insert it from the beginning.

Assume the below given is your list :

Say you have a pointer called head which does nothing but points to the starting node of the list then your list looks like :

The point to look at is that both head and N1 point to the same memory location.So now you want to add a new node N6 then you can do something like

N6->nextptr = head;
head = N6;

So now your list looks like :

So you can go on and on inserting node (At the beginning) by using a head pointer.I think you can work your way out now.. :)

This article has been dead for over six months. Start a new discussion instead.