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;



while(i==0){


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

	if(newptr==NULL){
		printf("Couldn't allocate memory");
	}


	printf("Please enter your first name or enter 'done' to exit\n:");
	scanf("%s",bufferFirstName);
	if(strcmp (bufferFirstName,"done") ==0 ){;
		i=1;
	}
	if(i!=1){
	newptr->firstname=malloc(sizeof(strlen(bufferFirstName)));
		if(newptr->firstname==NULL){
			printf("Couldn't allocate memory\n");
		}
	strcpy(newptr->firstname,bufferFirstName);


	printf("\nPlease enter your last name\n:");
	scanf("%s",bufferLastName);
	printf("\n");
	newptr->lastname=malloc(sizeof(strlen(bufferLastName)));
			if(newptr->lastname==NULL){
				printf("Couldn't allocate memory\n");
			}
	strcpy(newptr->lastname,bufferLastName);
if(listStartPtr==NULL){
	listStartPtr=newptr;
}
	listStartPtr=newptr;//first run through
	listStartPtr->nextptr=newptr;//second loop
	listStartPtr->nextptr->nextptr=newptr;//third loop
	listStartPtr->nextptr->nextptr-nextptr=newptr;//fourth

              //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 :
N1->N2->N3->N4->N5->NULL

Say you have a pointer called head which does nothing but points to the starting node of the list then your list looks like :
head->N1->N2->N3->N4->N5->NULL

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 :
head->N6->N1->N2->N3->N4->N5->NULL

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.. :)

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.