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

DS Unit 1 2020 12 July 2022

The document discusses Abstract Data Types (ADT) and Linear Data Structures, particularly focusing on Lists. It defines ADTs, explains their advantages, and describes various operations and implementations for List ADTs, including array and linked list implementations. Additionally, it outlines the advantages and disadvantages of linked lists compared to arrays, along with basic operations performed on singly linked lists.

Uploaded by

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

DS Unit 1 2020 12 July 2022

The document discusses Abstract Data Types (ADT) and Linear Data Structures, particularly focusing on Lists. It defines ADTs, explains their advantages, and describes various operations and implementations for List ADTs, including array and linked list implementations. Additionally, it outlines the advantages and disadvantages of linked lists compared to arrays, along with basic operations performed on singly linked lists.

Uploaded by

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

T.

Krishnakaarthik/AP/IT/Data Structures/Unit 1 1

Unit 1
Linear Data Structure – List
Abstract Data Type - ADT:
Definition 1:
 Abstract Data type is a type (or class) for objects whose behaviour is defined by a set of value and a
set of operations. ADT is a data type, where only behaviour is defined but not implementation.
 ADT as a black box which hides the inner structure and design of the data type.
 ADT only mentions what operations are to be performed but not how these operations will be
implemented.
 Process of providing only the essentials and hiding the details is known as abstraction.
 Example: Lists, Graphs and Sets along with their operations.

Definition 2:
 Abstract Data Types is the collection of data types and their associated operations.
 ADT is basically a special kind of data type created by programmer to a handle a complex data
structure.
 It specifies the following information:
1. The storage of data values in memory
2. The operations that are performed on the data types
3. The behaviour of the operations
Advantages of ADT:
1. It is reusable, robust and reduces the efforts in coding.
2. It provides encapsulation that protects data from being corrupted.
3. It provides robust data structure.
4. Modularity
5. Code is easier to understand
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 2

Data Structure:
 Data Structure is a way of organizing the data, which includes several operations to perform on that data.
 Example: List, Stack, Queue, Tree and Graphs

Types of Data Structure:

Data structures are classified into 2 categories:


1. Linear Data Structure:
Linear Data Structures are the data structures in which data is arranged in a list or in a straight
sequence.
Example: Arrays, Linked List
2. Non-Linear Data Structure:
Non–Linear Data Structures are the data structures in which data may be arranged in hierarchical
manner.
Example: Trees, Graphs

---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 3

The List ADT:


List is a collection of elements in sequential order.

The general form of the list is A1, A2,……, AN

A1 - 1st element of the list

A2 - 2nd element of the list

N – Size of the list


If the element at position i is Ai, then its successor is Ai+1(i.e Next) and its predecessor is Ai-1 (Previous)

Various operations performed on List


1. Insert (X, 5): Insert the element X after the position 5.
2. Delete (X): The element X is deleted
3. Find (X): Returns the position of X.
4. Next (i): Returns the position of its successor element i+1.
5. Previous (i): Returns the position of its predecessor i-1.
6. Print list: Contents of the list is displayed.
7. Makeempty: Makes the list empty.

Implementation of list ADT:


1. Array based Implementation
2. Linked List based Implementation
3. Cursor Implementation
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 4

Arra y based Implementation:


 Array is a collection of specific number of same type of data stored in consecutive memory
locations.
 Array is a static data structure i.e., the memory should be allocated in advance and the size is fixed.
o This will waste the memory space when used space is less than the allocated space.
o Insertion and Deletion operation are expensive as it requires more data movements
o Find and Print list operations takes constant time.

In memory, we can store the list in 2 ways,


 1st way is to store the elements in sequential memory locations. This is known as arrays.

List of sequentially stored elements using Arrays


 2nd way is to use pointers or links to another the elements sequentially; this is known as Linked Lists

List of elements with associated pointers using Linked Lists

A data structure for representing a list of integer is as follows:


type def struct list
{
int data;
int next;
} list;

The basic operations performed on a list of elements are:


a. Creation of List.
b. Insertion of data in the List
c. Deletion of data from the List
d. Display all data’s in the List
e. Searching for a data in the list
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 5

Advantages of array implementation:


1. Arrays are simple to understand
2. The elements are faster to access using random access
3. Searching an element is easier

Limitation of array implementation


1. It requires consecutive memory locations.
2. The number of elements in the array is fixed.
3. Insertion and deletion operation in array are expensive.

Applications of arrays:
1. Arrays are particularly used in programs that require storing large collection of similar type data
elements

1. Declaration of Array:

#define maxsize 10
int list[maxsize], n ;

2. Creating an element in the Array:

Routine to Create an element in the array:


void Create ( )
{ int i;
printf(“\nEnter the number of elements to be added in the list:\t”);
scanf(“%d”, &n);
printf(“\nEnter the array elements:\t”);
for(i=0;i<n;i++)
scanf(“%d”, &list[i]);
}
If n=6, the output of creation is as follows: list[6]

20 10 30 40 50 60

A[0] A[1] A[2] A[3] A[4] A[5]


T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 6

3. Inserting an element in the Array:

Routine to insert an element in the array:


void Insert( )
{
int i, data, pos;
printf(“\nEnter the data to be inserted:\t”);
scanf(“%d”, &data);
printf(“\nEnter the position at which element to be inserted:\t”);
scanf(“%d”, &pos);
if (pos==n)
printf (“Array overflow”);
for(i =n-1;i>=pos-1;i--)
list[i+1] = list[i]; list[pos-1] = data;
n=n+1;
Display();
}

Consider an array with 5 elements [max elements = 10]

nd
If data 15 is to be inserted in the 2 position then 50 has to be moved to next index
position, 40 has to be moved to 50 position, 30 has to be moved to 40 position and 20 has
to be moved to 30 position.

nd
After this four data movement, 15 is inserted in the 2 position of the array.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 7

4. Deleting an element in the Array:

Routine to delete an element in the array:


void Delete( )
{ int i, pos ;
printf(“\nEnter the position of the data to be deleted:\t”);
scanf(“%d”, &pos);
printf(“\nThe data deleted is:\t %d”, list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}
Consider an array with 5 elements [ max elements = 10 ]

If data 20 is to be deleted from the array, then 30 has to be moved to data 20 position, 40
has to be moved to data 30 position and 50 has to be moved to data 40 position.

nd
After this 3 data movements, data 20 is deleted from the 2 position of the array.

5. Display Operation/Traversing a list / Print a list of the Array :

Routine to traverse/display elements of the array:


void display( )
{ int i;
printf(“\n**********Elements in the array**********\n”);
for(i=0;i<n;i++)
printf(“%d\t”,list[i]);
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 8

6. Searching of an element in the Array:

Routine to search an element in the array:


void Search( )
{
int search, i, count = 0;
printf(“\nEnter the element to be searched:\t”);
scanf(“%d”, &search);
for(i=0;i<n;i++)
{
if(search == list[i])
count++;
}
if(count==0)
printf("\nElement not present in the list");
else
printf("\nElement present in the list");
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 9

Progra m for array implementation of List

#include<stdio.h> #include<conio.h> void Insert()


#define maxsize 10 { int i, data, pos;
int list[maxsize],n; printf("\nEnter the data to be inserted:\t");
void Create(); void Insert(); void Delete(); scanf("%d", &data);
void Display(); void Search(); void main() printf("\nEnter the position at which element
{ int choice; to be inserted:\t");
clrscr(); scanf("%d",&pos);
do for(i=n-1;i>=pos-1;i--)
{ printf("\n Array Implementation of List\n"); list[i+1] = list[i];
printf("\t1.create\n"); list[pos-1] = data;
printf("\t2.Insert\n"); n+=1; Display();
printf("\t3.Delete\n"); }
printf("\t4.Display\n"); void Delete( )
printf("\t5.Search\n"); { int i,pos;
printf("\t6.Exit\n"); printf("\nEnter the position of the data to be
printf("\nEnter your choice:\t"); deleted:\t");
scanf("%d",&choice); scanf("%d", &pos);
switch(choice) printf("\nThe data deleted is:\t %d", list[pos-
{ 1]);
case 1: Create(); break; for(i=pos-1;i<n-1;i++)
case 2: Insert(); break; list[i]=list[i+1];
case 3: Delete(); break; n=n-1; Display();
case 4: Display(); break; }
case 5: Search(); break; void Display()
case 6: exit(1); { int i;
default: printf("\n***Elements in the array ***\n");
printf("\nEnter option between 1 - 6\n"); for(i=0;i<n;i++)
break; printf("%d\t",list[i]);
} }
} while(choice<7); void Search()
} { int search,i,count = 0;
void Create() printf("\nEnter the element to be searched:\t");
{ int i; scanf("%d",&search);
printf(“\nEnter the number of elements to be for(i=0;i<n;i++)
added in the list:\t”); { if(search == list[i])
scanf(“%d”, &n); { count++;
printf("\nEnter the array elements:\t"); }
for(i=0;i<n;i++) }
scanf("%d", &list[i]); if(count==0)
Display(); printf("\nElement not present in the list");
} else
printf("\nElement present in the list");
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 10

}
Output

Array Implementation of List Array Implementation of List


1.create 1.create
2.Insert 2.Insert
3.Delete 3.Delete
4.Display 4.Display
5.Search 5.Search
6.Exit 6.Exit
Enter your choice: 1 Enter your choice: 3
Enter the number of elements to be added in the Enter the position of the data to be deleted: 4
list: 5 The data deleted is: 3
Enter the array elements: 12345 **********Elements in the array**********
**********Elements in the array********** 3 1 2 4 5
1 2 3 4 5 Array Implementation of List
Array Implementation of List 1.create
1.create 2.Insert
2.Insert 3.Delete
3.Delete 4.Display
4.Display 5.Search
5.Search 6.Exit
6.Exit Enter your choice: 5
Enter your choice: 2 Enter the element to be searched: 1
Enter the data to be inserted: 3 Element present in the list
Enter the position at which element to be Array Implementation of List
inserted: 1 1.create
**********Elements in the array********** 2.Insert
3 1 2 3 4 5 3.Delete
4.Display
5.Search
6.Exit
Enter your choice:6

---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 11

Why Linked List?


Arrays  Arrays are simple to understand and elements of an array are easily accessible.
 But arrays have some Limitations
1. Arrays have fixed dimension
2. Once the size of an array is decided it cannot be increased or decreased during execution.
3. Arrays elements are always stored in contiguous memory locations.
Linked List  To overcome this limitation we use linked list
 Non-Contiguous must scan for element insertion / deletion easy.
Dynamic Memory Allocation:
The process of allocating memory to the variables during execution of the program or at run time.
Linked List Implementation:
Linked Lists:
 It is a linear data structure.
 Linked list is a collection of elements. Each element in the list is referred as a node.
 Each node contains two fields namely,
o Data field  The data field contains the actual data of the elements to be stored in the list
o Next field  The next field contains the address of the next node in the list
 The pointer of the last node contains a special value, called the NULL pointer.

Advantages of Linked list:


1. Insertion and deletion of elements can be done efficiently
2. It uses dynamic memory allocation
3. Memory utilization is efficient compared to arrays
Disadvantages of linked list:
1. Linked list does not support random access
2. Memory is required to store next field
3. Searching takes time compared to arrays
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 12

Types of Linked List


1. Singly Linked List or One Way List
2. Doubly Linked List or Two-Way Linked List
3. Circular Linked List.

Singly Linked List: (SLL)


 A singly linked list is a linked list in which each node contains only one link field pointing to the next
node in the list.

SLL

SLL with a Header


Basic operations on a singly-linked list are:
1. Insert  Inserts a new node in the list.
2. Delete  Deletes any node from the list.
3. Find  Finds the position (address) of any node in the list.
4. FindPrevious  Finds the position (address) of the previous node in the list.
5. FindNext  Finds the position (address) of the next node in the list.
6. Display  display the date in the list
7. Search  find whether a element is present in the list or not

Advantages of SLL
1. The elements can be accessed using the next link
2. Occupies less memory than DLL as it has only one next field.
Disadvantages of SLL
1. Traversal in the backwards is not possible
2. Less efficient to for insertion and deletion.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 13

Declaration of Linked List

void insert(int X, List L, position P);


void find(List L, int X);
void delete(int x, List L);
typedef
struct node *position;
position L, p, newnode;
struct node
{ int data;
position next;
};
1. Creation of the List

This routine creates a list by getting the number of nodes from the user. Assume n=4 for this
example.
void create()
{ int i,n;
L=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the number of nodes to be inserted\n");
scanf("%d",&n);
printf("\n Enter the data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
L=newnode;
p=L;
for(i=2;i<=n;i++)
{ newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
p->next=newnode;
p=newnode;
} }
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 14

 Initially the list is empty

 Insert(10,List L):
 A new node with data 10 is inserted and the next field is updated to NULL.
 The next field of previous node is updated to store the address of new node.

 Insert(20,L):
 A new node with data 20 is inserted and the next field is updated to NULL.
 The next field of previous node is updated to store the address of new node.

 Insert(30,L):
 A new node with data 30 is inserted and the next field is updated to NULL.
 The next field of previous node is updated to store the address of new node.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 15

2. Insertion

Case 1: Routine to insert an element in list at the beginning


void insert(int X, List L, position p)
{ p=L;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);
newnode->next=L;
L=newnode;
}
Case 2: Routine to insert an element in list at Position
void Insert(int X, List L, position p)
{ position newnode;
newnode =(struct node*) malloc( sizeof( struct node ));
if( newnode = = NULL )
Fatal error( “ Out of Space ” );
else
{ Newnode -> data = x ;
Newnode -> next = p ->next ;
P -> next = newnode ;
}
}
Insert(25,L, P):
 A new node with data 25 is inserted after the position P and the next field is updated
to NULL.
 The next field of previous node is updated to store the address of new node
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 16

Case 3: Routine to insert an element in list at the end of the list


void insert(int X, List L, position p)
{ p=L;
newnode=(struct node*)malloc(size of(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);
while(p->next!=NULL)
p=p->next;
newnode->next=NULL;
p->next=newnode;
p=newnode;
}

3. Check whether a list is Empty:

Routine to check whether a list is Empty


int IsEmpty( List L )
{ if ( L -> next = = NULL )
return(1);
}

4. Check whether the Current Position is Last in the List:

Routine to check whether the current position is last in the List


int IsLast(List L , position p)
{ if( p -> next= =NULL)
return(1);
}

}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 17

5. Counting the element in the List:

Routine to Count the Element in the List:


void count(List L)
{ P = L -> next;
while( p != NULL )
{ count++;
p = p -> next;
}
print count;
}

6. Finding the element in the list:

Routine to Find the Element in the List: This routine returns the position of X in the list L
position find(List L, int X)
{ position p;
p=L->next;
while(p!=NULL && p->data!=X)
p=p->next;
return(p);
}
Find(List L, 20):
To find an element X traverse from the first node of the list and move to the next with the
help of the address stored in the next field until data is equal to X or till the end of the list.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 18

Find Previous: It returns the position of its predecessor.


position FindPrevious (int X, List L)
{ position p;
p = L;
while ( p -> next ! = NULL && p -> next -> data! = X )
p = p -> next;
return P;
}

Routine to find next Element in the List: It returns the position of successor.
void FindNext(int X, List L)
{ position P;
P=L->next;
while(P!=NULL && P->data!=X)
P = P->next;
return P->next;
}

7. Deleting an element in the list:

Routine to Delete an Element in the List:


It delete the first occurrence of element X from the list L
void Delete( int x , List L)
{ position p, Temp;
p = FindPrevious( X, L);
if( ! IsLast (p, L))
{ temp = p -> next;
P -> next = temp -> next;
free ( temp );
}
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 19

Routine to Delete the List


This routine deleted the entire list.
void Delete_list(List L)
{
position P,temp;
P=L->next;
L->next=NULL;
while(P!=NULL)
{ temp=P->next;
free(P);
P=temp;
}
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 20

Program for Singly Linked List

#include<stdio.h> void create()


#include<conio.h> {
#include<stdlib.h> int i,n;
void create(); L=NULL;
void display(); newnode=(struct node*)malloc(sizeof(struct node ));
void insert(); printf("\n Enter the number of nodes to be inserted\
void find(); n");
void delete(); scanf("%d", &n);
typedef struct node *position; printf("\n Enter the data\n");
position L, p, newnode; scanf("%d", &newnode >data);
struct node newnode->next=NULL;
{ L=newnode; p=L;
int data; for(i=2;i<=n;i++)
position next; {
}; newnode=(struct node*)malloc(sizeof(struct node));
void main() scanf("%d",&newnode->data);
{ newnode->next=NULL;
int choice; p->next=newnode;
clrscr(); p=newnode;
do }
{ }
printf("1.create\n2.display\n3.insert\n4.find\ void display()
n5.delete\n\n\n"); {
printf("Enter your choice\n\n"); p=L;
scanf("%d", &choice); while(p!=NULL)
switch(choice) { printf("%d -> ",p->data);
{ p=p->next;
case 1: create(); break; }
case 2: display(); break; printf("Null\n");
case 3: insert(); break; }
case 4: find(); break; void insert()
case 5: delete(); break; {
case 6: exit(0); int ch;
} printf("\nEnter ur choice\n");
} printf("\n1.first\n2.middle\n3.end\n");
while(choice<7); scanf("%d",&ch);
getch(); switch(ch)
} {
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 21

case 2: void find()


{ int pos,i=1; { int search,count=0;
p=L; printf("\n Enter the element to be found:\n");
newnode=(struct node*)malloc(sizeof(struct node)); scanf("%d",&search);
printf("\nEnter the data to be inserted\n"); p=L;
scanf("%d",&newnode->data); while(p!=NULL)
printf("\nEnter the position to be inserted\n"); {
scanf("%d",&pos); if(p->data==search)
newnode->next=NULL; { count++;
while(i<pos-1) break;
{ p=p->next; }
i++; p=p->next;
} if(count==0)
newnode->next=p->next; printf("\n Element Not present\n");
p->next=newnode; else
p=newnode; printf("\n Element present in the list \n\n")
display(); }
break; }
} void delete()
case 1: { position p,temp; int x; p=L;
{ p=L; if(p==NULL)
newnode=(struct node*)malloc(sizeof(struct node)); { printf("empty list\n");
printf("\nEnter the data to be inserted\n"); }
scanf("%d",&newnode->data); else
newnode->next=L; { printf("\nEnter the data to be deleted\n");
L=newnode; scanf("%d",&x);
display(); if(x==p->data)
break; { temp=p;
} L=p->next;
case 3: free(temp);
{ p=L; display();
newnode=(struct node*)malloc(sizeof(structnode)); }
printf("\nEnter the data to be inserted\n"); else
scanf("%d",&newnode->data); { while(p->next!=NULL && p->next->data!=x)
while(p->next!=NULL) { p=p->next;
p=p->next; }
newnode->next=NULL; temp=p->next;
p->next=newnode; p->next=p->next->next;
p=newnode; free(temp);
display(); display();
break; }
} }
}} }
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 22

Output: Enter ur choice: 2


Implementation of Doubly Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> Null
1.create 1.create
2.display 2.display
3.insert 3.insert
4.find 4.find
5.delete 5.delete
Enter the number of nodes to be inserted 5 Enter your choice: 3
Enter the data 1 2 3 4 5 1.first
1.create 2.middle
2.display 3.end
3.insert Enter the data to be inserted 7
4.find 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> Null
5.delete
----------------------------------------------------------------------------------------------------------------------------------------------------
Sample Example:
Creation of element:

Step 1: Initially create the NULL Node Step 2: Insert 93

Step 3: Insert 48

Step 4: insert 57

Step 4: insert 57
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 23

Insertion at the top of the list.

Insert: 8
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 24

Doubly-Linked List [DLL]:

 A doubly linked list is a linked list in which each node has three fields namely Data, Next, Prev.
o Data This field stores the value of the element
o Next This field points to the successor node in the list
o Previous This field points to the predecessor node in the list

Doubly-Linked List Node

Doubly-Linked List

Basic operations of a doubly -linked list are:


1. Insert – Inserts a new element at the end of the list.
2. Delete – Deletes any node from the list.
3. Find – Finds any node in the list.
4. Print – Prints the list

Advantage
1. Deletion operation is easier.
2. Finding the predecessor & Successor of a node is easier.

Disadvantage
1. More Memory Space is required since it has two pointers.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 25

1. Declaration of DLL Node:

typedef struct node *position;


struct node
{
int data;
position prev;
position next;
};

2. Creation an element of list in DLL:

Initially the list is empty. Then assign the first node as head.
newnode->data=X;
newnode->next=NULL;
newnode->prev=NULL;
L=newnode;
If we add one more node in the list,then create a newnode and attach that
node to the end of the list
L->next=newnode;
newnode->prev=L;

3. Insertion an element of DLL Node:


1. Insert an element in a DLL at the Beginning
2. Insert an element in a DLL at any Position
3. Insert an element in a DLL at the End
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 26

Routine to Insert an element in a DLL at the Beginning


void Insert (int x, list L, position P)
{ struct node *Newnode;
if(pos==1)
P=L;
Newnode = (struc node*)malloc (sizeof(struct node));
if (Newnode! = NULL)
Newnode->data= X;
Newnode ->next= L ->next;
L->next ->prev=Newnode
L->next = Newnode;
Newnode ->prev = L;
}
Routine to Insert an element in a DLL at any Position :
void Insert (int x, list L, position P)
{ struct node *Newnode;
Newnode = (struc node*)malloc (sizeof(struct node));
if (Newnode! = NULL)
Newnode->data= X;
Newnode ->next=
P ->next;
P->next ->prev=Newnode
P ->next = Newnode;
Newnode ->prev = P:
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 27

Routine to Insert an element in a DLL at the End:


void insert(int X, List L, position p)
{ p=L;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);
while(p->next!=NULL)
p=p->next;
newnode->next=NULL;
p->next=newnode;
newnode->prev=p;
}
4. Deletion an element of list in DLL:

Routine for deleting an element:


void Delete (int x ,List L)
{ Position p , temp;
P = Find( x, L );
if(P==L->next)
temp=L;
L->next=temp->next;
temp->next->prev=L;
free(temp);
else if( IsLast( p, L ) )
{ temp = p;
p -> prev -> next = NULL;
free(temp);
}
else { temp = p;
p -> prev -> next = p -> next;
p -> next -> prev = p -> prev;
free(temp);
}
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 28

5. Display an element of list in DLL:

Routine to display the elements in the list:


void Display( List L )
{ P = L -> next ;
while ( p != NULL)
{ printf(“%d”, p -> data ;
p = p -> next ;
}
printf(“ NULL”);
}

6. Searching an element of list in DLL:

Routine to search whether an element is present in the list


void find()
{ int a,flag=0,count=0;
if(L==NULL)
printf(“\nThe list is empty”);
else
{
printf(“\nEnter the elements to be searched”);
scanf(“%d”,&a);
for(P=L;P!=NULL;P=P->next)
{
count++;
if(P->data==a)
{
flag=1;
printf(“\nThe element is found”);
printf(“\nThe position is %d”,count);
break;
}
}
if(flag==0)
printf(“\nThe element is not found”);
}
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 29

Program for Implementation of Doubly Linked List:

#include<stdio.h> void insert()


#include<conio.h> {
void insert(); int pos,I;
void deletion(); newnode=(struct node*)malloc(sizeof(struct node));
void display(); printf(“\nEnter the data to be inserted”);
void find(); scanf(“%d”,&newnode->data);
typedef struct if(L==NULL)
node *position; { L=newnode;
position L->next=NULL;
newnode,temp L->prev=NULL;
,L=NULL,P; }
struct node else
{ {
int data; printf(“\nEnter the position where the data is to
position next; be inserted”);
position prev; scanf(“%d”,&pos);
}; if(pos==1)
void main() { newnode->next=L;
{ newnode->prev=NULL;
int choice; L->prev=newnode;
clrscr(); L=newnode;
do }
{ else
printf(“\n1.INSERT”); {
printf(“\n2.DELETE”); P=L;
printf(“\n3.DISPLAY”); for(i=1;i<pos-1&&P->next!=NULL;i++)
printf(“\n4.FIND”); {
printf(“\n5.EXIT”); P=P->next;
printf(“\nEnter ur option”); }
scanf(“%d”,&choice); newnode->next=P->next;
switch(choice) P->next=newnode;
{ newnode->prev=P;
case 1: insert(); break; P->next->prev=newnode;
case 2: deletion(); break; }
case 3: display(); break; }
case 4: find(); break; }
case 5: exit(1);
}
}
while(choice!=5);
getch();
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 30

void deletion() void find()


{ {
int pos,I; int a,flag=0,count=0;
if(L==NULL) if(L==NULL)
printf(“\nThe list is empty”); printf(“\nThe list is empty”);
else else
{ {
printf(“\nEnter the position of the data to printf(“\nEnter the elements to be searched”);
be deleted”); scanf(“%d”,&a);
scanf(“%d”,&pos); for(P=L;P!=NULL;P=P->next)
if(pos==1) {
{ count++;
temp=L; if(P->data==a)
L=temp->next; {
L->prev=NULL; flag=1;
printf(“\nThe deleted element is %d”, printf(“\nThe element is found”);
temp->data); printf(“\nThe position is %d”,count);
free(temp); break;
} }
else }
{ if(flag==0)
P=L; printf(“\nThe element is not found”);
for(i=1;i<pos-1;i++) }
P=P->next; }
temp=P->next;
printf(“\nThe deleted element is
%d”,temp->data);
P->next=temp->next;
temp->next->prev=P;
free(temp);
}
}
}
void display()
{
if(L==NULL)
printf(“\nNo of elements in the list”);
else
{
printf(“\nThe elements in the list are\
n”);
for(P=L;P!=NULL;P=P-
>next) printf(“%d”,P-
>data);
}
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 31

Output: Enter ur option 2


Implementation of Doubly Linked List: Enter the position of the data to be deleted
1.INSERT 2
2.DELETE The deleted element is 20
3.DISPLAY 1.INSERT
4.FIND 2.DELETE
5.EXIT 3.DISPLAY
Enter ur option1 4.FIND
Enter the data to be inserted10 5.EXIT
1.INSERT Enter ur option 3
2.DELETE The elements in the list are 10 30
3.DISPLAY 1.INSERT
4.FIND 2.DELETE
5.EXIT 3.DISPLAY
Enter ur option1 4.FIND
Enter the data to be inserted 20 5.EXIT
Enter the position where the data is to be Enter ur option4
inserted 2 Enter the elements to be searched 20
1.INSERT The element is not found
2.DELETE 1.INSERT
3.DISPLAY 2.DELETE
4.FIND 3.DISPLAY
5.EXIT 4.FIND
Enter ur option1 5.EXIT
Enter the position where the data is to be Enter ur option 4
inserted 3 Enter the elements to be searched 30
1.INSERT The element is found
2.DELETE The position is 2
3.DISPLAY 1.INSERT
4.FIND 2.DELETE
5.EXIT 3.DISPLAY
Enter ur option3 4.FIND
The elements in the list are 10 20 30 5.EXIT
1.INSERT Enter ur option5
2.DELETE Press any key to continue
3.DISPLAY
4.FIND
5.EXIT

----------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 32

Circularly-Linked List [CLL]:

 Circular Linked list is a linked list in which the pointer of the last node points to the first node.
Types of Circularly Linked List:
CLL can be implemented as circular singly linked list and circular doubly linked list.
1. Singly linked circular list:
A Singly linked circular list is a linked list in which the last node of the list points to the first node.

2. Doubly Linked Circular List:


A doubly linked circular list is a Doubly linked list in which the forward link of the last node points
to the first node and backward link of the first node points to the last node of the list.

Advantages of Circular Linked List


1. It allows to traverse the list starting at any point.
2. It allows quick access to the first and last records.
3. Circularly doubly linked list allows to traverse the list in either direction.

Applications:
1. Widely used in Operating Systems for task maintenance.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 33

1. Singly linked circular list:


A Singly linked circular list is a linked list in which the last node of the list points to the first node.

1. Declaration of Node:

typedef struct node *position;


struct node
{
int data;
position next;
};
2. Insertion:
1. Insert an element in the beginning (Insert 5)

2. Insert an element in the middle (Insert 25)


T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 34

3. Insert an element in the last (Insert 35)

Routine to insert an element in the Routine to insert an element in the Routine to insert an element in the
beginning middle last
void insert_beg(int X,List L) void insert_mid(int X,List L,Position p) void insert_last(int X, List L)
{ { {
position Newnode; position Newnode; position Newnode;
Newnode=(struct node*)malloc Newnode=(struct node*)malloc Newnode=(struct node*)malloc
(sizeof(struct node)); (sizeof(struct node)); (sizeof(struct node));
if(Newnode!=NULL) if(Newnode!=NULL) if(Newnode!=NULL)
{ { {
Newnode->data=X; Newnode->data=X; P=L;
Newnode->next=L->next; Newnode->next=p->next; while(P->next!=L) P=P->next;
L->next=Newnode; p->next=Newnode; Newnode->data=X;
} } P->next=Newnode;
} } Newnode->next=L;
}
}
3. Deletion:
1. Delete an element in the beginning (Delete 10)

Before Delete 10

After Delete 10
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 35

2. Delete an element in the middle (Delete 30)

Before Delete 30

After Delete 30
3. Delete an element in the last (Delete 30)

Before Delete 30

After Delete 3 0
Routine to delete an element in the Routine to delete an element in the Routine to delete an element in
beginning middle the last
void del_first(List L) void del_mid(int X,List L) void del_last(List L)
{ { {
position temp; position p, temp; position p, temp;
temp=L->next; p=findprevious(X,L); if(!Islast(P,L)) p=L;
L->next=temp->next; { while(p->next->next!=L)
free(temp); temp=p->next; p=p->next;
} p->next=temp->next; temp=p->next;
free(temp); p->next=L
} free(temp);
} }
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 36

2. Doubly Linked Circular List:


A doubly linked circular list is a Doubly linked list in which the forward link of the last node points to the
first node and backward link of the first node points to the last node of the list.

1. Declaration of Node:
typedef struct node *position;
struct node
{
int data;
position next;
position prev;
};

2. Insertion:
1. Insert an element in the beginning (Insert 5)

Before Insert 5

After Insert 5
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 37

2. Insert an element in the middle

Before Insert 25

After Insert 25
3. Insert an element in the last

Before Insert 40

After Insert 40
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 38

Routine to insert an element in the Routine to insert an element in the Routine to insert an element in the
beginning middle last
void insert_beg(int X,List L) void insert_mid(int X,List L,Position p) void insert_last(int X, List L)
{ { {
position Newnode; position Newnode; position Newnode,p;
Newnode=(struct Newnode=(struct Newnode=(struct
node*)malloc(sizeof(struct node)); node*)malloc(sizeof(struct node)); node*)malloc(sizeof(struct node));
if(Newnode!=NULL) if(Newnode!=NULL) if(Newnode!=NULL)
{ { { p=L;
Newnode->data=X; Newnode->data=X; while(p->next!=L)
Newnode->next=L->next; Newnode->next=p->next; p=p->next;
L->next->prev=Newnode; p->next->prev=Newnode; Newnode->data=X;
L->next=Newnode; p->next=Newnode; p->next =Newnode;
Newnode->prev=L; Newnode->prev=p; Newnode->next=L;
} } Newnode->prev=p;
} } L->prev=newnode;
}
}
3. Deletion:
1. Delete an element in the beginning (Delete 10)

Before Delete 10

After Delete 10
2. Delete an element in the middle (Delete 20)
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 39

Before Delete 20

After Delete 20
3. Delete an element in the last (Delete 30)

Before Delete 20

After Delete 2 0
Routine to delete an element in the Routine to delete an element in the Routine to delete an element in
beginning middle the last
void del_mid(int X,List L) void del_last(List L)
void del_first(List L) { {
{ Position p, temp; position p, temp;
position temp; p=FindPrevious(X); p=L;
if(L->next!=NULL) if(!IsLast(p,L)) while(p->next!=L)
{ { p=p->next;
temp=L->next; temp=p->next; temp=p;
L->next=temp->next; p->next=temp-next; p->next->prev=L;
temp->next->prev=L; temp->next->prev=p; L->prev=p->prev;
free(temp); free(temp); free(temp);
} } }
} }

----------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 40
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 41

Cursor Implementation of Linked List


Cursor implementation is very useful where linked list concept has to be implemented without using
pointers.

Comparison on Pointer Implementation and Cursor Implementation of Linked List

Pointer Implementation Cursor Implementation


Data are stored in a collection of structures. Data are stored in a global array of array o f
Each structure contains a data and next structures. Here array index is considered as
pointer. pointer.
Malloc function is used to create a node It maintains a list of free cells called cursors
and free function is used to released the space in which slot 0 is considered as a
cell header and Next is equivalent to the pointer
which points to the next slot.

The initialization configuration is:

Slot Element Next


0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 0
 The list will use cell 0 as a header. The value of 0 for Next is the equivalent of a NULL pointer.
 The cursor implementation of linked list is straight forward. For consistency; we will implement our
lists with a header node.
 In the following example, if the value of L is 5 and the value of M is 3, then L represents
 The list a, b, e and M represents the list c, d, f.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 42

Slot Element Next


0 -- 1
1 b 2
2 f 3
3 Header 4
4 -- 5
5 Header 6
6 -- 7
7 c 8
8 d 9
9 e 0
10 a 1

 The list L
o 5  10  1  9  0
o a bc
 The list M
o 3 7820
o c df

Declaration: Routine for Cursor Alloc & Cursor Free


typedef int ptrtoNode; Static position CursorAlloc (void)
typedef ptrtoNode position; {
void Initialize cursorspace (void); position P;
int IsEmpty (List L); P = CursorSpace [0].Next;
int IsLast (position P, List L); CursorSpace [0].Next = CursorSpace [P].Next;
position Find (int X, List L); return P;
void Delete (int X, List L); }
void Insert (int X, List L, position P); Static void CursorFree (position P)
Struct Node {
{ CursorSpace [P].Next = CursorSpace [0].Next;
int Element; CursorSpace [0].Next = P;
position Next; }
}; Struct Node Cursorspace [space size];
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 43

Routine To check whether the list IsEmpty Routine for IsLast


int IsEmpty (List L) int IsLast (Position P, List L)
{ /* Returns 1 if the list is Empty */. { /* Returns 1 if the p is in last position */
if (Cursorspace [0]. Next = = 0) if (CursorSpace [P].Next = = 0)
return (1); return (1);
} }
Routine for Find an Element Routine for Insertion
position Find (int X, List L) void Insert (int X, List L, position P)
{ {
position P; position newnode;
P = CursorSpace [L].Next; newnode = CursorAlloc ( );
while (P && CursorSpace [P].Element ! = X) if (newnode ! = 0)
P = CursorSpace [P].Next; CursorSpace [newnode].Element = X;
return P; CursorSpace [newnode]. Next = CursorSpace [P].Next;
} CursorSpace [P].Next = newnode;
}
Routine to Delete an Element
void Delete (int X, List L)
{
position P, temp;
P = Findprevious (X, L);
if (! IsLast (P, L))
{
temp = CursorSpace [P].Next;
CursorSpace [P].Next = CursorSpace [temp].Next;
CursorFree (temp);
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 44

Differences between Array Based and Linked Based implementation


Array Linked List
Definition Array is a collection of elements having same Linked list is an ordered collection of
data type with common name elements which are connected by
Access Elements can be accessed using Sequential access
index/subscript, random access
Memory structure Elements are stored in contiguous memory Elements are stored at available memory
Insertion & locations and deletion takes more time in array Insertion
Insertion space and deletion are fast and easy
Memory
Deletion Memory is allocated at compile time Memory is allocated at run time
Allocation i.e static memory allocation i.e dynamic memory allocation
Types 1D,2D,multi-dimensional SLL, DLL circular linked list

Dependency Each elements is independent Each node is dependent on each other as


address part contains address of next node in
the list

You might also like