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();
}