Timetable
Timetable
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
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.
Every item is related to its previous and next item. Every item is attached with many other items.
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.
UNIT 1 Page 4
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 5
16CS3202 - Data Structures II Yr CSE / III Sem
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 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
UNIT 1 Page 11
16CS3202 - Data Structures II Yr CSE / III Sem
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
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.
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.
UNIT 1 Page 16
16CS3202 - Data Structures II Yr CSE / III Sem
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.
UNIT 1 Page 18
16CS3202 - Data Structures II Yr CSE / III Sem
Figure
UNIT 1 Page 19
16CS3202 - Data Structures II Yr CSE / III Sem
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.
If a doubly linked list contains the elements a1, a2, a3, a4 and a5, then the representation is shown in
figure 11 .
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.
UNIT 1 Page 20
16CS3202 - Data Structures II Yr CSE / III Sem
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:
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 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
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.
UNIT 1 Page 28
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 29
16CS3202 - Data Structures II Yr CSE / III Sem
UNIT 1 Page 30