Please make sure that all code is written in C ONLY. Try to use the most element
ID: 3918845 • Letter: P
Question
Please make sure that all code is written in C ONLY. Try to use the most elementary manner of doing this.
Include output.
Create a Code Blocks project named LinkedLists. This program will use a set of data structures into which the user will enter contact information.
Globally declare a ContactInfo struct.
Create a function that asks for contact information and places the information into a struct.
ContactInfo *getContactInfo( void );
Call getContactInfo ten times from a for loop, and each time add the new data structure to the end of the list (use a function named addContactInfoToList( ContactInfo *info ) to add the new data structure to the list)..
Write a function that displays all of the data in the list.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct ContactInfo
{
char name[20];
int mobile_no;
struct ContactInfo *next;
}*current,*first;
struct ContactInfo* getContactInfo (void)
{
struct ContactInfo *new;
new= (struct ContactInfo *) malloc (sizeof(struct ContactInfo));
printf(" Enter the name:");
scanf("%s",new->name);
printf("Enter the mobile number:");
scanf("%d",&new->mobile_no);
new->next=NULL;
return new;
}
void display(struct ContactInfo* first)
{
int i;
for(i=0;i<10;i++)
{
printf("%s %d ",first->name,first->mobile_no);
first=first->next;
}
}
void addContactInfoToList( struct ContactInfo *info )
{
current->next=info;
current=current->next;
}
void main()
{
int i;
first=getContactInfo();
current=first;
for (i=1;i<10;i++)
{
addContactInfoToList( getContactInfo() );
}
printf(" The entered details are: ");
display(first);
}