0
What is linked list? Write a 'C' function for the insertion of 'n' elements

Mumbai University > Information Technology > Sem 3 > Data Structure and Algorithm analysis

Marks: 10 M

Year: May 2014

0
4

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:

• Information of the element.

• Linked list are dynamic data structure. That is, they can grow or shrink during the execution of a program.
• In linked list representation, memory is not pre-allocated. Memory is allocated whenever it is required. And it is deallocated when it is not needed.
• Insertion and deletion are easier and efficient. Linked list provides flexibility in inserting a data item at a specified position and deletion of a data item from the given position.

• More memory is required to store an integer number, a node with integer data and address field is allocated. That is more memory space is needed.
• Access to an arbitrary data item is little bit cumbersome and also time consuming.

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;
}*start;

{
struct node *tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data;
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);
}
printf ("\n");
getch();
}

void main()
{
int choice,n,m,position,i;
start=NULL;
while(1)
{
printf ("2.Display\n");
printf ("3.Quit\n");
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);
}
break;
case 2:
Display();
break;
case 3:
exit(0);
default:
printf ("\n\nWrong choice");
}
}
}

4