0% found this document useful (0 votes)
20 views30 pages

Timetable

The document discusses linear data structures and describes lists as an ordered collection of elements that can be implemented using arrays or linked lists. It provides details on abstract data types, common list operations, and an example of implementing a list using an array.

Uploaded by

karen
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)
20 views30 pages

Timetable

The document discusses linear data structures and describes lists as an ordered collection of elements that can be implemented using arrays or linked lists. It provides details on abstract data types, common list operations, and an example of implementing a list using an array.

Uploaded by

karen
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/ 30

16CS3202 - Data Structures II Yr CSE / III Sem

UNIT I
LINEAR STRUCTURES
1.1 Abstract Data Types (ADT)
1.2 List ADT
1.3 Array-based implementation
1.4 Linked list implementation
1.5 Doubly-linked lists
1.6 Cursor-based linked lists
1.7 Applications of lists

INTRODUCTION
A DATA is a collection of facts, concepts, figures, observation, occurrences or instruction in
formalized manner.
If we arrange some data in an appropriate sequence, then it forms a Structure and gives us a
meaning. This meaning is called Information (i.e, processed data)
So, we found two things in Information: One is Data and the other is Structure.
DATA STRUCTURE
A data structure is a systematic way of organizing, storing, manipulating and accessing data in
such a way that some meaningful operations can be performed on these data in an effective way.
Applications of data structures
Compiler design
Operating System
Database Management system
Network analysis
Expert Systems
CLASSIFICATION OF DATA STRUCTURE

Classification Description Example

Primitive The primitive data structures are known as basic data Integer, Float, Character, Pointer,
data structures. Boolean, String
structure
These data structures are directly operated by the
machine instructions.
Normally, primitive data structures have different
representation on different computers.

UNIT 1 Page 1
16CS3202 - Data Structures II Yr CSE / III Sem

Non- The non-primitive data structures are highly Arrays, Lists, Files
Primitive developed complex data structures.
data Basically, these are developed from the primitive
structure data structure.
The non-primitive data structure is responsible for
organizing the group of homogeneous and
heterogeneous data elements.
A non primitive data structure is further divided into
linear and non linear data structure.

Linear Data In Linear data structures, the data items are arranged Array, Linked List, Stack, Queue
Structure in a linear sequence.
Data elements in a liner data structure are traversed
one after the other and only one element can be
directly reached while traversing.

Non Linear The data structure where data items are not Trees, Graphs
Data organized sequentially is called non linear data
Structure structure.
The data elements of the non linear data structure
could be connected to more than one element to
reflect a special relationship among them.

Figure 1 Classification of Data Structure


Difference Between Linear and Non Linear Data Structure

Linear Data Structure Non-Linear Data Structure

Every item is related to its previous and next item. Every item is attached with many other items.

Data is arranged in linear sequence. Data is not arranged in sequence.

UNIT 1 Page 2
16CS3202 - Data Structures II Yr CSE / III Sem

Data items can be traversed in a single run. Data cannot be traversed in a single run.

Examples: Array, Stack, Queue, Linked List. Examples: Tree, Graph.

Implementation is Easy. Implementation is Difficult.

1.1 ABSTRACT DATA TYPE


Abstract Data Types (ADT) is a mathematical model together with the possible set of operations.
E.g. List ADT, Tree ADT, Stack ADT, Set ADT and so on.
'Abstract ' implies that we give an implementation-independent view of the data structure
ADTs specify the type of data stored and the operations that support the data.
The basic idea of implementing ADT is that the operations are written once in program and can be
called by any part of the program.
Objects such as list, set and graph along with their operations can be viewed as ADT's
For example in list ADT we have the operations such as insert, delete, and find, count and so on.
In the Set ADT, the operations are Union, Intersection, size, complement and so on.

1.2 LIST ADT


List is an ordered set of elements. The general form of the list is A1, A2, A3, ..... ,AN
A1 - First element of the list
AN - Last element of the list
N - Size of the list
If the element at position i is Ai then its successor is Ai+1 and its predecessor is Ai-1.
The set of operations that can be performed on the list ADT are
Insert (X, 5) - Insert the element X after the position 5.
Delete (X) - The element X is deleted
Find (X) - Returns the position of X.
Next (i) - Returns the position of its successor element i+1.
Previous (i) - Returns the position of its predecessor i-1.
Print list - Contents of the list is displayed.
Makeempty - Makes the list empty.
Example:
If the list is 34, 12, 52, 16, 12, then find(52) might return 3.
insert(x,3) might make the list into 34, 12, 52, x, 16,12 (if we insert after the position given); and delete(3)
might turn that list into 34, 12, x, 16, 12.
UNIT 1 Page 3
16CS3202 - Data Structures II Yr CSE / III Sem

Implementation of List ADT:


The list can be implemented in three different ways.
1. Array based Implementation
2. Linked List Implementation
3. Cursor Based Implementation.

1.3. ARRAY BASED IMPLEMENTATION


Array is a collection of same set of elements stored in consecutive memory locations.
Example:
A linear array A[8] consisting of numbers is pictured in following figure.

Figure 2 Array Representation


All the list operation can be implemented by using the array. The list implementation using array has some
merits and demerits in it as follows:
Insertion and Deletion operation are expensive as it requires more data movement
Find and Printlist operations takes constant time.
Even if the array is dynamically allocated, an estimate of the maximum size of the list is required
which considerably wastes the memory space.
Types of Array
The types of array are:
One dimensional array
Two dimensional array
Multidimensional array
One Dimensional array
Syntax:
datatype array_name[array_size];
Two Dimensional array
Syntax:
Datatype array_name[row_size][column_size];

ARRAY IMPLEMENTATION OF LIST ABSTRACT DATA TYPE

UNIT 1 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem

/*PROGRAM FOR ARRAY IMPLEMENTATION OF LIST ADT*/


#include<iostream.h>
#include<conio.h>
#define MAX 5
class Arraylist
{
private:
int list[MAX];
int pos;
public:
Arraylist();
void insert();
void del();
void display();
void search();
int isempty();
int isfull();
};
Arraylist::Arraylist()
{
pos=0;
}
void Arraylist::insert()
{
int num,p,i;
if(isfull())
{
cout<<"List is Full Cannot insert\n";
}
else
{
cout<<"Enter the element to insert:";
cin>>num;

UNIT 1 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem

if(isempty())/* if empty list create list*/


{
list[pos]=num;
pos++;
}
else
{
cout<<"Enter the position to insert:";
cin>>p;
if(p-1>pos)
{
cout<<"Position incorrect cannot insert\n";
}
else if(p-1==pos)/*insert at end of list*/
{
list[pos]=num;
pos++;
}
else /*insert at header or middle of list*/
{
for(i=pos;i>p-1;i--)
{
list[i]=list[i-1];
}
list[p-1]=num;
pos++;
}
}
}
display();
}
void Arraylist::del()
{

UNIT 1 Page 6
16CS3202 - Data Structures II Yr CSE / III Sem

int p,i;
if(isempty())
{
cout<<"List is Empty Cannot delete\n";
}
else
{
cout<<"Enter the position to delete:";
cin>>p;
if(p-1>=pos)
{
cout<<"Position incorrect cannot delete\n";
}
else
{
cout<<"Element deleted at position "<<p<<" is "<<list[p-1]<<"\n";
if(p-1==pos-1)
{
pos--;
}
else
{
for(i=p-1;i<pos-1;i++)
{
list[i]=list[i+1];
}
pos--;
}
}
}
display();
}
void Arraylist::display()

UNIT 1 Page 7
16CS3202 - Data Structures II Yr CSE / III Sem

{
int i;
if(isempty())
{
cout<<"List is Empty \n";
}
else
{
cout<<"List Elements are\n";
for(i=0;i<pos;i++)
{
cout<<list[i]<<" ";
}
}
cout<<"\n";
}
int Arraylist::isempty()
{
if(pos==0)
return 1;
else
return 0;
}
int Arraylist::isfull()
{
if(pos==MAX)
return 1;
else
return 0;
}
void Arraylist::search()
{
int data,i;

UNIT 1 Page 8
16CS3202 - Data Structures II Yr CSE / III Sem

int check=0;/*to find an item exist or not*/


if(isempty())
{
cout<<"List is Empty cannot find element\n";
}
else
{
cout<<"Enter the element to search:";
cin>>data;
for(i=0;i<pos;i++)
{
if(list[i]==data)
{
cout<<"Element found at "<<i+1<<" position\n";
check=1;
break;
}
}
if(check==0)
{
cout<<"Element not found\n";
}

}
}
int main()
{
int choice;/*to select multiple menu choices*/
clrscr();
Arraylist obj;
do
{
cout<<"*******MENU*******\n1.Insert\n2.Delete\n3.Display\n4.Search\nEnter your Choice:";

UNIT 1 Page 9
16CS3202 - Data Structures II Yr CSE / III Sem

cin>>choice;
switch(choice)
{
case 1:
obj.insert();
break;
case 2:
obj.del();
break;
case 3:
obj.display();
break;
case 4:
obj.search();
break;
default:
cout<<"wrong choice \n";
}
}while(choice<5);
getch();
return 0;
}
OUTPUT:
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:10
List Elements are
10
*******MENU*******

UNIT 1 Page 10
16CS3202 - Data Structures II Yr CSE / III Sem

1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:20
Enter the position to insert:2
List Elements are
10 20
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:30
Enter the position to insert:1
List Elements are
30 10 20
*******MENU*******
1.Insert
2.Delete
3.Display
4.Search
Enter your Choice:1
Enter the element to insert:40
Enter the position to insert:2
List Elements are
30 40 10 20

1.4 LINKED LIST IMPLEMENTATION


The linked list consists of series of nodes, which are not necessarily adjacent in memory. Each
node contains the element and a pointer to its successor (next) node. The pointer of the last node points
to NULL. Insertion and deletion operations are easily performed using linked list.

UNIT 1 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem

Types of Linked List


1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List.
Basic operations
The basic operations supported by a list are:
Insertion
Elements can be added either at the beginning, or as a middle element or as the last
element.
Deletion
Elements can be deleted at the beginning, or from the middle or the last element.
Display
Display the entire list of elements.
Search
Search the entire list using the given key element.

1.4.1 SIMPLE LINKED LIST (or) SINGLY LINKED LIST


Simple linked list can be visualized as a chain of nodes, where every node points to the next node.
If a linked list contains the elements a1, a2, a3, a4 and a5, then the representation is shown in figure 3.

Figure 3 Linked list


Figure 4 shows the actual representation of the list in Figure 3. The list contains five structures, which
happen to reside in memory locations 1000, 800,712, 992, and 692 respectively.

Figure 4 Linked list with actual pointer values


The next pointer in the first structure has the value 800, which provides the indication of where the second
structure is.
The other structures each have a pointer that serves a similar purpose. Of course, inorder to access this list,
we need to know where the first cell can be found. A pointer variable can be used for this purpose which
is called as head or first.
Basic operations in Simple linked list
The basic operations in a simple linked list are:
1. Create

UNIT 1 Page 12
16CS3202 - Data Structures II Yr CSE / III Sem

2. Insert
3. Delete
4. Display
1. Create
The linked list structure will be
struct NODE
{
int data;
NODE *next;
};
NODE *first,*last,*curr,*temp;
Create function will create a node initially as per the structure definition and get the data from the

element then the first and last pointer is assigned to the node created. If the list contains elements then the
temporary pointer is moved to the last element and link with newly created node.
The codes to create the singly linked list are as follows:
curr=new node();

cin>>curr data;
curr next=NULL;
if(first==NULL && last==NULL)
{
fisrt=curr;
last=curr;
temp=curr;
}
else
{
temp next=curr;
temp=curr;
last=curr;
}
Example:
Assume the list created using the above code contains 5 elements as shown in figure 5.

UNIT 1 Page 13
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 5 Singly linked list after creation


2. Insert
In a singly linked list, the insertion operation can be performed in three ways. They are as follows:
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


The steps to insert a new node at beginning of the single linked list are:
Step 1: Create a new node with given value.
Step 2: Set curr = first and first = curr.
The C++ code to insert at the beginning of a list is:
curr=new node();
cout<<"Enter the data for the newnode";
cin>>curr->data;
curr->next=first;
first=curr;
Example:
Assume the list contains 5 elements and the new element 5 is inserted at the beginning of a list.

Figure 6 After inserting element 5 at the beginning


Inserting At End of the list
The steps to insert a new node at end of the singly linked list are
Step 1:Keep moving the temp to its next node until it reaches to the last node in the list (until
is equal to NULL).

UNIT 1 Page 14
16CS3202 - Data Structures II Yr CSE / III Sem

Step 2: Create a new node with given value and curr as NULL.
Step 3: Set = curr and last=curr.
The codes to insert at the end of a list are as follows:
while(temp->next!=NULL)
temp=temp->next;
curr=new node();
cout<<"\nEnter the data for the newnode";
cin>>curr->data;
curr->next=NULL;
temp->next=curr;
last=curr;
Example:
Assume the list contains 6 elements and the new element 60 is inserted at the last of a list.

Figure 7 After inserting element 60 at last


Inserting At Specific location in the list (After a Node)
The steps to insert a new node after a node in the single linked list are
Step 1: Keep moving the temp to its next node until it reaches to the node after which we want to insert
the newNode (until is equal to the key value).
Step 2: Create a newode with given value.
Step 3: = ' and ' = curr'
The codes to insert at the specific location of a list are as follows:
cout<<"\nEnter the key data";
cin>>n;
while(temp->data!=n)
{
temp=temp->next;
}
curr=new node();
cout<<"\nEnter the data for the newnode";

UNIT 1 Page 15
16CS3202 - Data Structures II Yr CSE / III Sem

cin>>curr->data;
curr->next=temp->next;
temp->next=curr;
Example:
Assume the list contains 6 elements and the new element 35 is inserted after the element 30 in a list.

Figure 8 After inserting element 35 after element 30


3. Delete
In a singly linked list, the deletion operation can be performed in three ways. They are as follows:
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
The steps to delete a node from beginning of the single linked list are:
Step 1: Define a Node pointer 'temp' and initialize with first.
Step 2: Check whether list is having only one node ( == NULL) and if it is TRUE then set
first = NULL and delete temp (Setting Empty list conditions)
Step 3: If it is FALSE then set first = , and delete temp.
The codes to delete from the beginning of a list are as follows:
if(temp==first)
{
first=temp->next;
\ ->data;
free(temp);
}
Example:
Assume the list contains 7 elements and the first element 5 is deleted in a list.

UNIT 1 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 9 Singly linked list after deleting the first element


Deleting from End of the list and at the specific position
The steps to delete a node from end of the singly linked list are
Step 1:Define two Node pointers 'prev' and 'temp' and initialize 'temp' with first.
Step 2: Check whether list has only one Node ( == key data)
Step 3: If it is TRUE, then set first = NULL and delete temp. And terminate the function. (Setting Empty
list condition)
Step 4: If it is FALSE, then set 'prev = temp ' and move temp to its next node. Repeat the same until it
reaches to the last node in the list. (until temp == NULL)
Step 5: Finally, Set prev = NULL and delete temp.
The codes to delete from the beginning of a list are as follows:
temp=first;
while(temp!=NULL)
{
if(temp->data==num)
{
break;
}
else
{
prev=temp;
temp= temp->next;
}
}
if(temp==first)
{
first=NULL;
\ ->data;
free(temp);

UNIT 1 Page 17
16CS3202 - Data Structures II Yr CSE / III Sem

}
else
{
prev->next=temp->next;
\ ->data;
free(temp);
}
Example:
Assume the list contains 6 elements and the element 60 is deleted in a list.

Figure 10 Singly linked list after deleting last element


4. Display
The steps to display the elements of a single linked list are:
Step 1: Check whether list is Empty (first == NULL)
Step 2: If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with first.
Step 4: Keep displaying with an arrow (--->) until temp reaches to the last node
Step 5: Finally display with arrow pointing to NULL ( ---> NULL).
The codes to display the list are as follows:
if(first==NULL)
{
cout<< 'List is Empty!!!
}
else
{
temp=first;
while(temp!=NULL)
{
cout<< temp->data << ;

UNIT 1 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem

temp = temp next;


}
cout<<
}
Comparison between array and linked list are summarized in following table
Characteristic ARRAY LINKED LIST
Linked list is an ordered collection of
Array is a collection of elements having
Define elements which are connected by
same data type with common name.
links/pointers.
In array, elements can be accessed using
index/subscript value, i.e. elements can accessed
Access be randomly accessed like arr[0], arr[3], randomly but can be accessed
etc. So array provides fast and random only sequentially.
access.
In linked list, elements can be stored at any
Memory In array, elements are stored
available place as address of node is stored
Structure in consecutive manner in memory.
in previous node.
Insertion & deletion takes more time in Insertion & deletion are fast & easy in
Insertion &
array as elements are stored in linked list as only value of pointer is needed
Deletion
consecutive memory locations. to change.
Memory In array, memory is allocated at compile In linked list, memory is allocated at run
Allocation time i.e. Static Memory Allocation. time i.e. Dynamic Memory Allocation.
Array can be single dimensional, two Linked list can
Types
dimension or multidimensional. be singly, doubly or circular linked list.
In array, each element is independent, no In Linked list, location or address of
Dependency connection with previous element or with elements is stored in the link part of
its location. previous element/node.
In linked list, adjacency between the
In array, no pointers are used like linked
elements are maintained using pointers or
Extra Space list so no need of extra space in memory
links, so pointers are used and for that extra
for pointer.
memory space is needed.

Figure

UNIT 1 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem

1.5 DOUBLY LINKED LIST


A doubly linked list is a list that contains links to next and previous nodes. Unlike singly linked
lists where traversal is only one way, doubly linked lists allow traversals in both ways. Following are the
important terms to understand the concept of doubly linked list.

link of a linked list contains a link to the next node called next.
ontains a link to the previous node called prev.
Advantage:
* Deletion operation is easier.
* Finding the predecessor & Successor of a node is easier.
Disadvantage:
* More Memory Space is required since it has two pointers.

A generic doubly linked list node can be designed as:


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

If a doubly linked list contains the elements a1, a2, a3, a4 and a5, then the representation is shown in
figure 11 .

Figure 11 A Doubly linked list

Figure 12 shows the actual representation of the list. The list contains four structures, which happen to
reside in memory locations 1000, 800,712, 992 respectively.

Figure 12 Doubly Linked list with actual pointer values

UNIT 1 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem

Basic operations in Doubly linked list


The basic operations in a doubly linked list are:
1. Create
2. Insert
3. Delete
4. Display
1. Create
The create operation will create new list of elements by using the structure definition.
Step 1: Define a new node by using the structure node.
Step 2: If the first pointer is NULL, then assign the new node to first to indicate the node as first element
in a list.
to the last node in the list and link with the newly
created node.
The code used to create a doubly linked list is as follows:
void double_llist::create_list(int value)
{
node *first,*temp;
curr = new node;
curr->data = value;
curr->next = NULL;
if (first == NULL)
{
curr->prev = NULL;
first = curr;
}
else
{
temp = first;
while (temp->next != NULL)
temp = temp->next;
temp->next = curr;
curr->prev = temp;
last=curr;
}

UNIT 1 Page 21
16CS3202 - Data Structures II Yr CSE / III Sem

2. Insertion
There are three situation for inserting element in list.
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Insertion at the front of a list
The steps to insert a new element at the front of a list are:
Step 1: Check whether the list is empty. If it is TRUE, then print to create a new list.

Step 3: Create a new node with the definition of structure node and assign the value to curr->data and
NULL to curr->prev.
Step 4: Assign curr-
Step 5: Also assign the temp->prev to curr in order to link the previous node and make the curr node to
first.
The code used to insert a new element at the beginning of doubly linked list is as follows:
void double_llist::add_begin(int value)
{
if (first == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
temp=first;
node *curr;
curr = newnode;
curr->prev = NULL;
curr->data = value;
curr->next = temp;
temp->prev = curr;
first=curr;
cout<<"Element Inserted"<<endl;
}

UNIT 1 Page 22
16CS3202 - Data Structures II Yr CSE / III Sem

Example:
The doubly linked list after inserting the element 10 at the beginning is as follows:

Figure 13 Doubly Linked listafter inserting the element at the beginning


Insertion at a particular position of a list
The steps to insert a new element at the front of a list are:
Step 1: Check whether the list is empty. If it is TRUE, then print to create a new list.

Step 3: Keep moving the temp pointer until the required position is reached by using temp->temp->next.
Step 4: Create a new node using the structure definition and assign the value to data.
Step 5: If the temp reached the position, check whether the temp->next is equal to NULL. If it is
TRUE, then assign temp-> next to curr and curr->prev to temp. Also assign curr->next as NULL to
indicate that node as a last node.
Step 6: If it is FALSE, then assign the address stored in temp->next to curr->next to connect in chain.
Step 7: To make the chain as a doubly linked list, assign the temp->next->prev to curr and curr->prev to
temp.
The code used to insert any new element at any position of a doubly linked list is as follows:
void double_llist::add_after(int value, intpos)
{
if (first == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
node *temp;
int i;
temp = first;
for (i = 1;i < pos;i++)

UNIT 1 Page 23
16CS3202 - Data Structures II Yr CSE / III Sem

{
temp = temp->next;
}
curr = newnode;
curr->data = value;
if (temp->next == NULL) /*Insert at Last position*/
{
temp->next = curr;
curr->next = NULL;
curr->prev = temp;
}
else /*Insert at middle of the list*/
{
curr->next = temp->next;
curr->prev = temp;
curr->next->prev = curr;
temp->next = curr;
}
cout<<"Element Inserted"<<endl;
}
Example:
The new element 30 inserted after the element 30 is shown below:

Figure 14 Doubly Linked list after inserting the element at the position

The element 50 inserted at the last of a doubly linked list is shown below:

UNIT 1 Page 24
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 15 Doubly Linked list after inserting the element at the last

3. Deletion
Similar to the insertion, the deletion operation in a doubly linked list is performed in three ways:
1. Delete from the beginning
2. Delete at the middle
3. Delete at the last
Steps to follow for deletion operation
Step1: If the given key element
first and NULL to first->prev to indicate no element previous. Then deallocate the memory used by temp.
Step 2: Else, keep moving the temp pointer until it reaches the node previous to the required position.

Step 4: Before deleting the temp1 node, link the temp node and to the node next to temp1.
Step 5: Deallocate the memory used by temp1.
Step 6: If both the conditions in step 1 & 2 fails, then the element to be deleted is the last node. Assign
NULL to the next of previous node and deallocate the memory.
The code used to perform the deletion operation in a doubly linked list is as follows:
void double_llist::delete_element(int value)
{
node *temp, *q;
if (first->data == value) /*first element deletion*/
{
temp = first;
first = temp->next;
first->prev = NULL;
cout<<"Element Deleted"<<endl;

UNIT 1 Page 25
16CS3202 - Data Structures II Yr CSE / III Sem

free(tmp);
return;
}
temp = first;
while (temp->next->next != NULL)
{
if (temp->next->data == value) /*Element deleted in between*/
{
temp1 = temp->next;
temp->next = temp1->next;
temp1->next->prev = temp;
cout<<"Element Deleted"<<endl;
free(tmp1);
return;
}
temp= temp->next;
}
if (temp->next->data == value) /*last element deleted*/
{
temp1= temp->next;
free(temp1);
temp->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
}

Example:
Assume the doubly linked list contains the 4 elements after the create operation as shown below:

UNIT 1 Page 26
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 16 Doubly Linked list with 4 elements


The list after deleting the first element 10 is as follows:

Figure 17 Doubly Linked list after deleting the element from the beginning
The list after deleting the middle element 30 is as follows:

Figure 18 Doubly Linked list after deleting the element from the middle
4. Display
The display function is used to display the element present in the list from first to last using the next
pointer and from last to first using prev pointer.
The code used to display the list of elements in a doubly linked list is as follows:
void display()
{
temp=first;
//Display the elements from first to last
\

for(temp=first;temp!=NULL;temp=temp->next)
cout - temp->data << -
cout>> \n

UNIT 1 Page 27
16CS3202 - Data Structures II Yr CSE / III Sem

//Display the elements from last to first


\
for(temp=last;temp!=NULL;temp=temp->prev)
cout - temp->data << -

1.4.2 CIRCULAR LINKED LIST


In the circular linked list the previous element stores the address of the next element and the last
element stores the address of the starting element. The elements points to each other in a circular way
which forms a circular chain. Circular linked list 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.
If a linked list contains the elements a1, a2, a3, a4 and a5, then the representation is shown in figure 19.

Figure 19 Circular Singly linked list

Figure 20 shows the actual representation of the circular doubly link list in memory. The list contains five
nodes, which happen to reside in memory locations 1000, 800,712, 992, and 692 respectively.

Figure 20 Circular Singly Linked list with actual pointer values


The next pointer in the first node has the value 800, which provides the indication of where the second
node is. The next pointer in the last element has the value of 1000, shows the circular chained list.

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 as shown in the figure 21.

UNIT 1 Page 28
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 21 Circular Doubly linked list


Figure 22 shows the actual representation of the doubly circular link list in memory. The list contains
three nodes with elements 10, 15 and 20, which happen to reside in memory locations 1000, 2000, and
3000 respectively.

Figure 22 Circular Doubly Linked list with actual pointer values

Advantages of Circular Linked List


It allows to traverse the list starting at any point.
It allows quick access to the first and last records.

1.6 CURSOR-BASED LINKED LISTS


If linked lists are required and pointers are not available, then an alternate implementation must be
used. The alternate method we will describe is known as a cursor implementation.
The two important items present in a pointer implementation of linked lists are
1. The data is stored in a collection of structures. Each structure contains the data and a pointer to the
next structure.
2. A new structure can be obtained from the system's global memory by a call to malloc and released
by a call to free.
Cursor implementations satisfy condition 1 by a global array of structures. For any cell in the array, its
array index can be used in place of an address.
It satisfies the condition 2 by allowing the equivalent of malloc, new and free for cells in the
CURSOR_SPACE array. To do this, a list of cells that are not in any list is noted. The list will use cell 0
as a header. The initial configuration is shown in Figure 23.

UNIT 1 Page 29
16CS3202 - Data Structures II Yr CSE / III Sem

Figure 23 An initialized CURSOR_SPACE


Declaration for cursor implementation of linked list
typedef int node_ptr;
struct node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
struct node CURSOR_SPACE[ SPACE_SIZE ];

Routines used for cursor-alloc and cursor-free


position cursor_alloc( void )
{
position p;
p = CURSOR_SPACE[O].next;
CURSOR_SPACE[0].next = CURSOR_SPACE[p].next;
return p;
}
void cursor_free( position p)
{
CURSOR_SPACE[p].next = CURSOR_SPACE[0].next;

UNIT 1 Page 30

You might also like