0
4.3kviews
What is linked list? Write a 'C' function for the insertion of 'n' elements
1 Answer
written 8.4 years ago by | • modified 8.4 years ago |
1. Linked List
i. A linked list is a linear collection of data items called as nodes, linked to one another by means of pointers.
ii. Each node is divided into following two parts:
iii. Types of linked list:
iv. Advantages of linked list:
v. Disadvantages of linked list:
2. āCā function for the insertion of ānā elements
Following program demonstrates the use of the function AddAtBeg which is used to insert n elements at the beginning of linked list.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *link;
}*start;
void AddAtBeg(int data)
{
struct node *tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
}
void Display()
{
struct node *q;
if(start == NULL)
{
printf ("\n\nList is empty");
return;
}
q=start;
printf("\n\nList is : ");
while(q!=NULL)
{
printf ("%d ", q->info);
q=q->link;
}
printf ("\n");
getch();
}
void main()
{
int choice,n,m,position,i;
start=NULL;
while(1)
{
printf ("1.Add at beginning\n");
printf ("2.Display\n");
printf ("3.Quit\n");
printf ("\nEnter your choice:");
scanf ("%d",&choice);
switch (choice)
{
case 1:
printf ("\n\nHow many nodes you want to insert:");
scanf ("%d",&n);
for(i = 0;i<n;i++)
{
printf ("\nEnter the element:");
scanf ("%d",&m);
AddAtBeg(m);
}
break;
case 2:
Display();
break;
case 3:
exit(0);
default:
printf ("\n\nWrong choice");
}
}
}