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

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

Marks: 10 M

Year: May 2014

ADD COMMENTlink
modified 2.7 years ago  • written 2.7 years ago by gravatar for Yashbeer Yashbeer ♦♦ 130
4

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:

  • Information of the element.
  • Address of the next node in the linked list.

iii. Types of linked list:

  • Singly linked list
  • Doubly linked list
  • Circular linked list

iv. Advantages of linked list:

  • 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.

v. Disadvantages of linked list:

  • 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;
    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");
    }
}
}
ADD COMMENTlink
modified 2.7 years ago  • written 2.7 years ago by gravatar for Yashbeer Yashbeer ♦♦ 130
Please log in to add an answer.