can someone help me? Still confused in linked lists.
there's something wrong when I input a record for the 2nd time.
it sometimes works but then when I display all records It doesn't display correctly.
here's the code...

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node *nd;

struct node{
		  int next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
	}NODE;

menu();
Insert(nd *head);
Display(nd *head);

main()
{
	int m;
	char ans;
        nd head=NULL;

	do{
		clrscr();
		m=menu();
		switch(m)
        	{
		case 1: Insert(&head);
		break;
		case 2: printf("under construction");
		break;
		case 3: printf("under construction");
		break;
		case 4: Display(&head);
		break;
		case 5:
		break;
		default: {printf("Enter a number from the menu.");
			 getch();}
		break;
		}
	}while(m!=5);
}

menu()
{
	int menu;
	printf("1. Data Entry\n2. Search and Edit\n3. Search and Delete\n");
	printf("4. Display all records\n5. Exit\nchoice: ");
	scanf("%d" ,&menu);
	return menu;
}

Insert(nd *head)
{  int x=14,y=1;
	nd temp, h=*head;
	char tel[15], ln[20], fn[20], ad[50];
	clrscr();

	printf("Last name: \n");
	printf("First name: \n");
	printf("Tel. Number: \n");
	printf("Address: ");
	gotoxy(x,y++); scanf("%s", &ln);
	gotoxy(x,y++); scanf("%s", &fn);
	gotoxy(x,y++); scanf("%s", &tel);
	gotoxy(x,y++); scanf("%s", &ad);

	if (h==NULL){
	    h=malloc(sizeof(NODE));
		 strcpy(h->lname, ln);
		 strcpy(h->fname, fn);
	         strcpy(h->telnum, tel);
		 strcpy(h->address, ad);
	    h->next=NULL;
	    *head=h;
	  }

	else {
	     temp=malloc(sizeof(NODE));
	     strcpy(temp->telnum, tel);
	     temp->next=h;
	     h=temp;
	     *head=h;
	   }

	free(temp);
}

Display(nd *head)
{
	nd p=*head;
	clrscr();

	while(p!=NULL){
		puts(p->lname);
		puts(p->fname);
		puts(p->telnum);
		puts(p->address);
		p=p->next;
		}

	getch();
}

Recommended Answers

All 16 Replies

struct node{
		  int next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
	}NODE;

NODE is an instance of the structure, not a typedef name. If you want a typedef name then code like this:

typedef struct node{
		  int next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
	}NODE;

Thanks Ancient Dragon. I did that and got through the 2nd input.
but when I input the 2nd time the output gets all messed up and displays some special characters like greek symbols,spades etc.. and when I input the 3rd time it goes into an infinite loop..

Edit: ok it's getting stuck on the 2nd input again.

Insert(nd *head); Shouldn't the above be Insert(nd head); ? You've already used typedef to make a variable of type 'nd' a pointer to struct node. So when you declare Insert(nd *head); , i think head becomes a pointer to a pointer to a structure.

Even i'm still learning, so correct me if i'm wrong.

if you use Insert(nd head); you'll lose track of the head.. you gotta have to point it to the head in the main().. still learning too..:confused:

One of the reasons your program craps out is because you are freeing up the memory that was allocated for the nodes. Delete line 90.

I compiled your program and it has several errors that you need to correct. I hope your compiler tells you about them. For example: you need to specify the return value for the functions. main() returns an int so you need to code it as int main() The same with the other functions.


line 9: next must be a pointer, so you need to code it like this: struct node* next; >>typedef struct node *nd;
It may be just a personal thing but I don't like using typedefs like that because it hides the underlying code. It's much clearer to write int foo(struct node** head) because you know immediately what the parameter is -- its a pointer to the top of the linked list.

It appears that you are inserting new nodes at the head of the linked list. If that is true then you can simplify the function like this: (note: I removed the gotoy() calls because it isn't supported by my compiler. Just use what you have coded for that).

void Insert(nd *head)
{  
    int x=14,y=1;
    nd temp;
    clrscr();
    temp = malloc(sizeof(NODE));

    printf("Last name: \n");
    scanf("%s", temp->lname);
    printf("First name: \n");
    scanf("%s", temp->fname);
    printf("Tel. Number: \n");
    scanf("%s", temp->telnum);
    printf("Address: ");
    scanf("%s", temp->address);
    temp->next = *head;
    *head = temp;

}

thanks. that worked..
I'm going to finish this later..

I'm trying to display it alphabetically but it gets an error on line 88.
and when i use a space on the input() it doesn't work right.
example:
when I input
Last name: mylname xxxxx yyyyy zzzzz
it takes it as:
mylname as my last name
xxxx as my first name
yyyy as my tel number
zzzz as my address

Here's the CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node *nd;

typedef struct node{
		  int *next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
	}NODE;

int menu();
void Insert(nd *head);
Display(nd *head);

main()
{
	int m;
	char ans;
        nd head=NULL;

	do{
		clrscr();
		m=menu();
		switch(m){
		case 1: Insert(&head);
		break;
		case 2: printf("under construction");
		break;
		case 3: printf("under construction");
		break;
		case 4: Display(&head);
		break;
		case 5:
		break;
		default: {printf("Enter a number from the menu.");
		getch();}
		break;
		}
	}while(m!=5);
}

int menu()
{
	int menu;
	printf("1. Data Entry\n2. Search and Edit\n3. Search and Delete\n");
	printf("4. Display all records\n5. Exit\nchoice: ");
	scanf("%d" ,&menu);
	return menu;
}

void Insert(nd *head)
{
	int x=14,y=1;
	nd temp ,h;
	clrscr();
	temp = malloc(sizeof(NODE));
	h=*head;

       /*input*/
	printf("Last name: \n");
	gotoxy(x,y++); scanf("%s", &temp->lname);
	printf("First name: \n");
	gotoxy(x,y++); scanf("%s", &temp->fname);
	printf("Tel. Number: \n");
	gotoxy(x,y++); scanf("%s", &temp->telnum);
	printf("Address: ");
	gotoxy(x,y++); scanf("%s", &temp->address);
       
       /*insert*/
	if(h==NULL){
	temp->next=*head;
	*head=temp;
        }
	if(h!=NULL){
	while(h!=NULL){
       /*insert beginning*/
	if(strcmpi(h->lname,temp->lname)>0){
	temp->next=h;
	*head=temp;
	break;
	}
       /*insert middle*/
if(strcmpi(h->lname,temp->lname)<0 && strcmpi(h->next->lname,temp->lname)>0){
	temp->next=h->next;
	h->next=temp;
	break;
	}
       /*insert end*/
	if(strcmpi(h->lname,temp->lname)<0 && h->next==NULL){
	temp->next=NULL;
	h->next=temp;
	break;
	}
	else
	h=h->next;

	}
    }
}

Display(nd *head)
{
	nd p;
	int y=2;
	clrscr();
	p=*head;

	printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
	while(p!=NULL){
		gotoxy( 1,y); printf("%s, %s" ,p->lname, p->fname);
		gotoxy(27,y); printf("|   %s" ,p->telnum);
		gotoxy(50,y); printf("|   %s" ,p->address);
		p=p->next;
		y++;
		}
	getch();
}

Can someone please help me with this..
I'm stuck here.
TIA

You have quite a few syntax errors in that code. Here are the corrections. Note: I had to code dummy functions for clrscr() and gotoxy() because those two functions are nonportable and not implemented by my compiler. Just remove my version and you will be good to go.

I did get a clean compile, but didn't test it.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node *nd;
typedef struct node{
		  struct node *next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
	}NODE;

int menu();
void Insert(nd *head);
void Display(nd *head);
void clrscr()
{
    system("cls");
}
void gotoxy(int x, int y)
{

}

int main()
{
	int m;
        nd head=NULL;

	do{
		clrscr();
		m=menu();
		switch(m){
		case 1: Insert(&head);
		break;
		case 2: printf("under construction");
		break;
		case 3: printf("under construction");
		break;
		case 4: Display(&head);
		break;
		case 5:
		break;
		default: {printf("Enter a number from the menu.");
		getch();}
		break;
		}
	}while(m!=5);
}

int menu()
{
	int menu;
	printf("1. Data Entry\n2. Search and Edit\n3. Search and Delete\n");
	printf("4. Display all records\n5. Exit\nchoice: ");
	scanf("%d" ,&menu);
	return menu;
}

void Insert(nd *head)
{
	int x=14,y=1;
	nd temp ,h;
	clrscr();
	temp = malloc(sizeof(NODE));
	h=*head;

       /*input*/
	printf("Last name: \n");
	gotoxy(x,y++); scanf("%s", &temp->lname);
	printf("First name: \n");
	gotoxy(x,y++); scanf("%s", &temp->fname);
	printf("Tel. Number: \n");
	gotoxy(x,y++); scanf("%s", &temp->telnum);
	printf("Address: ");
	gotoxy(x,y++); scanf("%s", &temp->address);
       
       /*insert*/
	if(h==NULL){
	temp->next = *head;
	*head=temp;
        }
	if(h!=NULL){
	while(h!=NULL){
       /*insert beginning*/
	if(strcmpi(h->lname,temp->lname)>0){
	temp->next=h;
	*head=temp;
	break;
	}
       /*insert middle*/
if(strcmpi(h->lname,temp->lname)<0 && strcmpi(h->next->lname,temp->lname)>0){
	temp->next=h->next;
	h->next=temp;
	break;
	}
       /*insert end*/
	if(strcmpi(h->lname,temp->lname)<0 && h->next==NULL){
	temp->next=NULL;
	h->next=temp;
	break;
	}
	else
	h=h->next;

	}
    }
}

void Display(nd *head)
{
	nd p;
	int y=2;
	clrscr();
	p=*head;

	printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
	while(p!=NULL){
		gotoxy( 1,y); printf("%s, %s" ,p->lname, p->fname);
		gotoxy(27,y); printf("|   %s" ,p->telnum);
		gotoxy(50,y); printf("|   %s" ,p->address);
		p=p->next;
		y++;
		}
	getch();
}

Thanks Ancient Dragon

but I still can't use a space because it inputs it to the next one.

scanf() can not be used to input text that contain spaces. Use fgets() instead.

gotoxy(x,y++); 
fgets(temp->lname, sizeof(temp->lname), stdin);
if( strlen(temp->lname)-1 == '\n')
      temp->lname[strlen(temp->lname-1)] = 0;

Since you use the above several times, you might just make it a function and call that function forf each input item

void getline(char* item, size_t itemSize)
{
    fgets(item, itemSize, stdin);
    if( item[strlen(item)-1] == '\n')
        item(strlen(item)-1) = 0;
}

...
<snip>
printf("Last name: \n");
gotoxy(x,y++); getline(temp->lname, sizeof(temp->lname));
printf("First name: \n");
gotoxy(x,y++); get;ome(temp->fname, sizeof(temp-->fname));
printf("Tel. Number: \n");
gotoxy(x,y++); get;ome(temp->telnum, sizeof(temp->telnum));
printf("Address: ");
gotoxy(x,y++); getline(temp->address, sizeof(temp->address));

Thanks.
But I still have to ask our teacher if we're allowed to use fgets().

Is there no way that I can use gets() or scanf() in this?

>Is there no way that I can use gets() or scanf() in this?
There's no way to use gets safely, but if your teacher is too ignorant or stubborn to recognize that, you really have no choice but to use it (keeping in mind that it's a very bad practice). scanf is generally a bad choice for string input because it doesn't play nice with other input functions.

You might also consider that if they insist on using gets() that they really don't know C at all. What they're teaching you is a bunch of non-portable rubbish which is only good for the one compiler they know.

It's all very well, but when you come to your next compiler, you could be in for a surprise!.

Ok but she's the boss.
I have to use gets() because we will be discussing about fgets() by next week.

Thanks for all your help.
I'll be working on the Search & Edit and Search & Delete now.

here's my final code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node *nd;
typedef struct node{
		  struct node *next;
		  char telnum[15];
                  char fname[30];
                  char lname[30];
		  char address[70];
		  char flname[60];
	}NODE;

int menu();
void Insert(nd *head);
void Search_Edit(nd * head);
void Search_Delete(nd * head);
void Display(nd *head);

int main()
{
	int m;
        nd head=NULL;

	do{
		clrscr();
		m=menu();
		switch(m){
		case 1: Insert(&head);
		break;
		case 2: Search_Edit(&head);
		break;
		case 3: Search_Delete(&head);
		break;
		case 4: Display(&head);
		break;
		case 5:
		break;
		default: {printf("Enter a number from the menu.");
		getch();}
		break;
		}
	}while(m!=5);
}

int menu()
{
	int menu;
	printf("1. Data Entry\n2. Search and Edit\n3. Search and Delete\n");
	printf("4. Display all records\n5. Exit\nchoice: ");
	scanf("%d" ,&menu);
	return menu;
}

void Insert(nd *head)
{
	nd temp ,h;
	char ans;
	do{
	clrscr();
	temp = malloc(sizeof(NODE));
	h=*head;

	fflush(stdin);
	printf("Last name:   ");
	gets(temp->lname);
	printf("First name:  ");
	gets(temp->fname);
	printf("Tel. Number: ");
	gets(temp->telnum);
	printf("Address:     ");
	gets(temp->address);

	strcpy(temp->flname,temp->lname);
	strcat(temp->flname,temp->fname);
	strcpy(h->flname,h->lname);
	strcat(h->flname,h->fname);

	if(h==NULL){
	temp->next = *head;
	*head=temp;
        }
	if(h!=NULL){
		while(h!=NULL){
			if(strcmpi(h->flname,temp->flname)==0){
			printf("Entry already in record.");
			}

			else if(strcmpi(h->flname,temp->flname)>0){
			temp->next=h;
			*head=temp;
			break;
			}


			else if(strcmpi(h->flname,temp->flname)<0 && strcmpi(h->next->flname,temp->flname)>0 && h->next!=NULL){
			temp->next=h->next;
			h->next=temp;
			break;
			}

			else if(strcmpi(h->flname,temp->flname)<0 && h->next==NULL){
			temp->next=NULL;
			h->next=temp;
			break;
			}

			h=h->next;
		}
	}
	printf("\nRecord saved");
	printf("\nEnter another record?(y/n) ");
	scanf("%c" ,&ans);
	}while(ans=='y' ||ans=='Y');
}

void Search_Edit(nd *head)
{

  nd h=*head;
  char ln[30] ,fn[30];
  int count=0 ,y=2;
  clrscr();

	fflush(stdin);
	printf("Enter Family Name: ");
	gets(ln);

  clrscr();

	while (h!=NULL){
	    if (strcmpi(ln,h->lname)==0){
		count++;
	    }
	    h=h->next;
	}
	if (count==0){
	    printf("\n %s is not in the directory.", ln);
	    printf("\n\n Press any key to continue.");
	}
	if (count==1){
	    h=*head;
	    while (h!=NULL){
		clrscr();
		printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
		if (strcmpi(ln,h->lname)==0){
	    		gotoxy( 1,y); printf("%s, %s" ,h->lname, h->fname);
	   		gotoxy(27,y); printf("|   %s" ,h->telnum);
	   		gotoxy(50,y); printf("|   %s" ,h->address);
			gotoxy(1,y+3);	printf("Enter new address: ");
			gets(h->address);
			printf("Enter new telephone number: ");
			gets(h->telnum);
			gotoxy(1,y+6);printf("Record saved.");
			gotoxy(1,y+7);printf("Press any key to continue...");
			break;
			}
		h=h->next;
		}
	}
	if (count>1){
	    h=*head;
	    printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
	    while (h!=NULL){
		if (strcmpi(ln,h->lname)==0){
	    		gotoxy( 1,y); printf("%s, %s" ,h->lname, h->fname);
	   		gotoxy(27,y); printf("|   %s" ,h->telnum);
	   		gotoxy(50,y); printf("|   %s" ,h->address);
			y++;
		}
		h=h->next;
	    }
	    printf("\n\nMore than one record found!");
	    printf("\n\nEnter firstname: ");
	    gets(fn);
	    h=*head;

	    while (h!=NULL){
		clrscr();
		y=2;
		printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
		if (strcmpi(fn,h->fname)==0 && strcmpi(ln,h->lname)==0){
	    		gotoxy( 1,y); printf("%s, %s" ,h->lname, h->fname);
	   		gotoxy(27,y); printf("|   %s" ,h->telnum);
	   		gotoxy(50,y); printf("|   %s" ,h->address);
			gotoxy(1,y+3); printf("Enter new telephone number: ");
			gets(h->telnum);
			printf("Enter new address: ");
			gets(h->address);
			gotoxy(1,y+6); printf("Record saved.");
			printf("\nPress any key to continue.");
			break;
		}
		else{
			clrscr();
			printf("\n %s, %s is not in the directory", ln, fn);
			printf("\n\n Press any key to continue.");
		}
                h=h->next;
	    }
	}
  getch();
}


void Search_Delete(nd * head)
{


  nd h=*head,q,temp=NULL ,p=*head;
  char answer ,ln[30] ,fn[30];
  int count=0 ,y=2;
  clrscr();

	fflush(stdin);
	printf("Enter Family Name: ");
	gets(ln);
	clrscr();
	while (h!=NULL){
	    if (strcmpi(ln,h->lname)==0){
		count++;
	    }
	    h=h->next;
	}

	if (count==0){
	    printf("\n %s is not in the directory.", ln);
	    printf("\n\n Press any key to continue.");
	}

	if (count==1){
	    clrscr();
      	    h=*head;
            printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");

	    while(h!=NULL){
		if (strcmpi(ln,h->lname)==0){
			gotoxy( 1,y); printf("%s, %s" ,h->lname, h->fname);
			gotoxy(27,y); printf("|   %s" ,h->telnum);
			gotoxy(50,y); printf("|   %s" ,h->address);
		}

		h=h->next;
	    }
	    gotoxy(1,y+3); printf("Are you sure you want to delete record? (y/n)");
	    scanf(" %c", &answer);
	    if (answer=='y' || answer=='Y'){
		h=*head;
		while(h!=NULL){
	    		if(strcmpi(ln,h->lname)==0 && h==*head){
				*head=p->next;
				p->next=NULL;
				break;

			}
			if(strcmpi(ln,h->lname)==0 && h!=*head){
				h=*head;
				while(strcmpi(ln,h->lname)!=0){
					q=h;
					h=h->next;
				}
				q->next=h->next;
				h->next=NULL;
				break;
			}
			h=h->next;
		}
	    	gotoxy(1,y+6); printf("Record has been deleted!");
	   	printf("\n\nPress any key to continue.");
	    }
	}

	if (count>1){
	    clrscr();
      	    h=*head;
            printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
	    while(h!=NULL){

		if (strcmpi(ln,h->lname)==0){
			gotoxy( 1,y); printf("%s, %s" ,h->lname, h->fname);
			gotoxy(27,y); printf("|   %s" ,h->telnum);
			gotoxy(50,y); printf("|   %s" ,h->address);
		}
		h=h->next;
		y++;
	    }

	    printf("\n\nMore than one record found!");
	    printf("\nEnter firstname: ");
	    gets(fn);

	    gotoxy(1,y+3); printf("Are you sure you want to delete record? (y/n)");
	    scanf(" %c", &answer);
	    if (answer=='y' || answer=='Y'){
		h=*head;
		while(h!=NULL){
	    		if(strcmpi(ln,h->lname)==0 && strcmpi(fn,h->fname)== 0 && h==*head){
				*head=p->next;
				p->next=NULL;
				break;

			}
			if(strcmpi(ln,h->lname)==0 && strcmpi(fn,h->fname)==0 && h!=*head){
				h=*head;
				while(strcmpi(ln,h->lname)!=0 || strcmpi(fn,h->fname)!=0){
					q=h;
					h=h->next;
				}
				q->next=h->next;
				h->next=NULL;
				break;
			}
			h=h->next;
		}
	    gotoxy(1,y+6); printf("Record has been deleted!");
	    printf("\n\nPress any key to continue.");
	    }
	}
	getch();
}

void Display(nd *head)
{
	nd p;
	int y=2;
	clrscr();
	p=*head;

	if(p!=NULL){
	    printf("NAME                      |   TELEPHONE NUMBER   |   ADDRESS\n");
	    while(p!=NULL){
		gotoxy( 1,y); printf("%s, %s" ,p->lname, p->fname);
		gotoxy(27,y); printf("|   %s" ,p->telnum);
		gotoxy(50,y); printf("|   %s" ,p->address);
		p=p->next;
		y++;
	    }
	}
	else{
	    printf("The list is EMPTY");
	}
	getch();
}

thanks for everything.. and please do tell me if you find something wrong with it

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.