0% found this document useful (0 votes)
11 views6 pages

Unit-2 2

Uploaded by

ramketha07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Unit-2 2

Uploaded by

ramketha07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays / Linear Lists:

 It is a compact way of collecting data types where all entries must be of the same data
type.
 It is a collection of elements of same datatype referred by a single name.
 All the elements of linear list are stored in sequential/contiguous memory locations
which allow faster/direct access to any element through index.
 The principle of the list is, “Insertion at anywhere and Deletion from anywhere”

Insertion Operation:

The procedure to insert an element into in array at a specified position is:

 First Verify, whether the list is full or not. If the list is full, then insertion not possible.
 Otherwise, get the position at which the element to be inserted
 If the position is valid, Shift the array elements from that position till the end to one
position forward(towards right)
 Insert the element at the given position as this is now empty.
 Increase the no. of elements by one.

Deletion Operation:

The procedure to delete an element from array at a specified position is:

 First Verify, whether the list is empty or not. If the list is empty, then deletion not
possible.
 Otherwise, get the position at which the element to be deleted
 If the position is valid, Delete(print) the element at the given position.
 Shift the array elements from next to that position till the end to one position
backward(towards left)
 Decrease the no. of elements by one.
Display (Traversal) Operation:

The procedure to display all the elements of list are:

 First Verify, whether the list is empty or not. If the list is empty, then there are no
elements to display.
 Otherwise, Print all the elements from firsat element to last element i.e. 0 to n-1.

/* Program from the implementation of void delete()


Linear List*/ {
#include<stdlib.h> int p,,i;
#include<stdio.h> if(n= = 0)
#define MAX 50 printf(“ List is empty, deletion not
void Insert(); possible”);
void delete(); else
void display(); {
void main() printf(“ enter the position at which you
{ wnat to delete:”);
int ch; sacnf(“%d:,&p);
while(1) if(p>n-1) || (p<0)
{ printf(“Invalid Position”);
printf(“\n List Menu”); else
printf(“\n 1. Insert \n 2. Delete \n 3. {
Display \n 4. Exit”); printf(“ The deleted element is: %d”,
printf(“ \nEnter your choice:”); A[p]);
scanf(“%d”,&ch); for( i= p;i<n-1;i++)
switch(ch) A[i]= A[i+1];
{ n--;
case 1: insert(); break; }
case 2: delete(); break; }
case 3: display(); break; }
case 4: exit(0);
default: printf(“\n Invalid Choice”);
}
}
}
void insert() void display()
{ {
int p,,i; int i;
if(n= = MAX) if(n= = 0)
printf(“ List is Full”, Insertion not printf(“ List is empty”);
possible”); else
else {
{ printf(“ The List elements are:”);
printf(“ enter the position at which you for( i= p;i<n-1;i++)
wnat to insert:”); A[i]= A[i+1];
sacnf(“%d:,&p); n--;
if(p>n) || (p<0) }
printf(“Invalid Position”); }
else }
{
for( i= n-1;i>=p;i--)
A[i+1]= A[i];
printf(“ enter the element to insert:”);
scanf(“%d”,&A[p]);
n++;
}
}
}

Linked Lists:
To avoid the disadvantages of linear list( Size of list fixed, time consuming process of
insertion, deletion) the concept of Linked Lists is used.

In Linked list, each element is called a node. So, linked list consists set of node in which each
node contains a field called a link( a pointer) which consists the address of the next element
in the list. Thus successive elements in the list need not occupy adjacent space in memory.
There are three types of linked lists. They are,
 Singly Linked List (SLL)
 Doubly Linked List (DLL)
 Circular Linked List
Singly Linked List:
A singly linked list is a collection of nodes in which every node consists of two parts: one
part contains the data (value) of the element and the other part consists the address of the next
node . The address part of the last node consists a value called the ‘NULL’ and the address of
the starting node is stored in a pointer called “head”.
Singly linked list is represented as,

Ex:

Doubly Linked List:

A Doubly linked list is a collection of nodes in which every node consists of three parts: one
is data part and two are address parts, data part contains the data (value) of the element, one
address part consists the address of the next node and the other address part consists the
address of previous node. The next address part of the last node and previous address part of
the first node consists a value called the ‘NULL’ and the address of the starting node is stored
in a pointer called “head”.
Doubly linked list is represented as,
Ex:

Circular Linked List

A circular linked list is a type of linked list in which the first and the last nodes are also
connected to each other to form a circle.
There are basically two types of circular linked list:

1. Circular Singly Linked List


Here, the address of the last node consists of the address of the first node.

2. Circular Doubly Linked List


Here, in addition to the last node storing the address of the first node, the first node will also
store the address of the last node.
Difference between Array and Linked List

ARRAY / Linear List LINKED LIST

A linked list is a group of entities called a node.


An array is a grouping of data elements of The node includes two segments: data and
equivalent data type. address.

All the data elements are stored in a All the nodes are stored in random memory
contiguous memory locations. locations.

List size is fixed, and it is not possible to


increase/ decrease the size during the run Linked list is dynamic in size i.e. list size can be
time. increased or decreased during run time.

The memory is allocated at compile time. The memory is allocated at run time.

Direct accessing of elements is possible i.e. Sequential accessing of elements only possible
It is easier and faster to access the element i.e. In a linked list, the process of accessing
in an array. elements takes more time.

Insertion and deletion operation takes less time


Insertion and deletion operation takes time than array

Uses more memory than array because it stores


Uses less memory both data and the address of the next node.

You might also like