DS NOTES UNIT-1 (1)
DS NOTES UNIT-1 (1)
Data
Structure
Notes
lOMoARcPSD|16756265
UNIT-1
1.1 BASIC TERMINOLOGY: ELEMENTARY DATA ORGANIZATION
1.1.1 Data and Data Item
Data are simply collection of facts and figures. Data are values or set of values. A data item
refers to a single unit of values. Data items that are divided into sub items are group items; those
that are not are called elementary items. For example, a student’s name may be divided into three
sub items – [first name, middle name and last name] but the ID of a student would normally be
treated as a single item.
In the above example ( ID, Age, Gender, First, Middle, Last, Street, Area ) are elementary data
items, whereas (Name, Address ) are group data items.
1.1.3 Variable
It is a symbolic name given to some known or unknown quantity or information, for the purpose
of allowing the name to be used independently of the information it represents. A variable name
in computer source code is usually associated with a data storage location and thus also its
contents and these may change during the course of program execution.
1.1.4 Record
Collection of related data items is known as record. The elements of records are usually called
fields or members. Records are distinguished from arrays by the fact that their number of fields
is typically fixed, each field has a name, and that each field may have a different type.
lOMoARcPSD|16756265
1.1.5 Program
A sequence of instructions that a computer can interpret and execute is termed as program.
1.1.6 Entity
An entity is something that has certain attributes or properties which may be assigned some
values. The values themselves may be either numeric or non-numeric.
Example:
1.1.8 Field
A field is a single elementary unit of information representing an attribute of an entity, a record
is the collection of field values of a given entity and a file is the collection of records of the
entities in a given entity set.
1.1.9 File
File is a collection of records of the entities in a given entity set. For example, file containing
records of students of a particular class.
1.1.10 Key
A key is one or more field(s) in a record that take(s) unique values and can be used to distinguish
one record from the others.
1.2 ALGORITHM
A well-defined computational procedure that takes some value, or a set of values, as input and
produces some value, or a set of values, as output. It can also be defined as sequence of
computational steps that transform the input into the output.
If we need an algorithm that requires less memory space, then we choose the first algorithm at
the cost of more execution time. On the other hand if we need an algorithm that requires less
time for execution, then we choose the second algorithm at the cost of more memory space.
An implementation of ADT consists of storage structures to store the data items and algorithms
for basic operation. All the data structures i.e. array, linked list, stack, queue etc are examples of
ADT.
b) The other way is to have the linear relationship between the elements represented by means of
pointers or links. These linear structures are called linked lists.
The common examples of linear data structure are arrays, queues, stacks and linked lists.
1.8.3 Tree
Data frequently contain a hierarchical relationship between various elements. The data structure
which reflects this relationship is called a rooted tree graph or, simply, a tree.
1.8.4 Graph
Data sometimes contains a relationship between pairs of elements which is not necessarily
hierarchical in nature, e.g. an airline flights only between the cities connected by lines. This data
structure is called Graph.
1.8.5 Queue
A queue, also called FIFO system, is a linear list in which deletions can take place only at one
end of the list, the Font of the list and insertion can take place only at the other end Rear.
1.8.6 Stack
It is an ordered group of homogeneous items of elements. Elements are added to and removed
from the top of the stack (the most recently added items are at the top of the stack). The last
element to be added is the first to be removed (LIFO: Last In, First Out).
Traversing: accessing each record/node exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called “visiting” the
record.)
Searching: Finding the location of the desired node with a given key value, or finding
the locations of all such nodes which satisfy one or more conditions.
Inserting: Adding a new node/record to the structure.
Deleting: Removing a node/record from the structure.
C programming language provides a data structure called the array, which can store a fixed-size
sequential collection of elements of the same type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables. A specific element in an array is accessed by an
index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Multidimensional array
That’s means the structure contains a set of data elements, numbered (N), for example called
(X), its defined as type of element, the second type is the index type, is the type of values used to
access individual element of the array, the value of index is
1<= I =< N
By this definition the compiler limits the storage region to storing set of element, and the first
location is individual element of array , and this called the Base Address, let’s be as 500. Base
Address (501) and like for the all elements and used the index I, by its value are range 1<= I =>
N according to Base Index (500), by using this relation:
= 500 +3
= 503
So the address of forth element is 503 because the first element in 500.
When the program indicate or dealing with element of array in any instruction like (write (X [I]),
read (X [I] ) ), the compiler depend on going relation to bounding the requirement address.
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be think as a table which will have x number of rows and y number of
columns. A 2-dimensional array a, which contains three rows and four columns can be shown as
below:
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is
the name of the array, and i and j are the subscripts that uniquely identify each element in a.
For example:
Given an array [1…5,1…7] of integers. Calculate address of element T[4,6], where BA=900.
Sol) I = 4 , J = 6
M= 5 , N= 7
= 900+ (7 x 3) +5
= 900+ 21+5
= 926
Column Major Order: Order elements of first column stored linearly and then comes
elements of next column.
For example:
Given an array [1…6,1…8] of integers. Calculate address element T[5,7], where BA=300
Sol) I = 5 , J = 7
M= 6 , N= 8
= 300+ (6 x 6) +4
lOMoARcPSD|16756265
= 300+ 36+4
= 340
By the same way we can determine address of element for three and four dimensional array:
For example:
Given an array [ 1..8, 1..5, 1..7 ] of integers. Calculate address of element A[5,3,6], by using
rows &columns methods, if BA=900?
Rows- wise
= 900 + 40 x 5 +5 x 4 + 2
= 1122
Columns- wise
= 900 + 40 x 5 +8 x 2 + 4
This program will traverse each element of the array to calculate the sum and then
calculate & print the average of the following array of integers.
( 4, 3, 7, -1, 7, 2, 0, 4, 2, 13)
lOMoARcPSD|16756265
#include <iostream.h>
#define size 10 // another way int const size = 10
int main()
{ int x[10]={4,3,7,-1,7,2,0,4,2,13}, i, sum=0,LB=0, UB=size;
float av;
for(i=LB,i<UB;i++) sum = sum + x[i];
av = (float)sum/size;
cout<< “The average of the numbers= “<<av<<endl;
return 0;
}
Bubble Sort:
The technique we use is called “Bubble Sort” because the bigger value gradually bubbles their
way up to the top of array like air bubble rising in water, while the small values sink to the
bottom of array. This technique is to make several passes through the array. On each pass,
successive pairs of elements are compared. If a pair is in increasing order (or the values are
identical), we leave the values as they are. If a pair is in decreasing order, their values are
swapped in the array.
lOMoARcPSD|16756265
/* This program sorts the array elements in the ascending order using bubble sort method */
#include <iostream.h>
int const SIZE = 6
void BubbleSort(int [ ], int);
int main()
{
int a[SIZE]= {77,42,35,12,101,6};
int i;
cout<< “The elements of the array before sorting\n”;
for (i=0; i<= SIZE-1; i++) cout<< a[i]<<”, “;
BubbleSort(a, SIZE);
cout<< “\n\nThe elements of the array after sorting\n”;
for (i=0; i<= SIZE-1; i++) cout<< a[i]<<”, “;
return 0;
}
void BubbleSort(int A[ ], int N)
{
int i, pass, hold;
for (pass=1; pass<= N-1; pass++)
{
for (i=0; i<= SIZE-pass; i++)
{
if(A[i] >A[i+1])
{
hold =A[i];
A[i]=A[i+1];
A[i+1]=hold;
}
}
}
}
Arrays are used to implement mathematical vectors and matrices, as well as other kinds of
rectangular tables. Many databases, small and large, consist of (or include) one-dimensional
arrays whose elements are records.
Arrays are used to implement other data structures, such as heaps, hash
tables, deques, queues, stacks, strings, and VLists.
One or more large arrays are sometimes used to emulate in-program dynamic memory allocation,
particularly memory pool allocation. Historically, this has sometimes been the only way to
allocate "dynamic memory" portably.
Arrays can be used to determine partial or complete control flow in programs, as a compact
alternative to (otherwise repetitive) multiple IF statements. They are known in this context
as control tables and are used in conjunction with a purpose built interpreter whose control
flow is altered according to values contained in the array.
Matrix with maximum zero entries is termed as sparse matrix. It can be represented as:
Tri-diagonal matrix: It has non-zero entries on diagonal and at the places immediately
above or below diagonal.
In many programming environments memory allocation to variables can be of two types static
memory allocation and dynamic memory allocation. Both differ on the basis of time when
memory is allocated. In static memory allocation memory is allocated to variable at compile time
whereas in dynamic memory allocation memory is allocated at the time of execution. Other
lOMoARcPSD|16756265
The Head is a special pointer variable which contains the address of the first node of the list. If
there is no node available in the list then Head contains NULLvalue that means, List is empty.
The left part of the each node represents the information part of the node, which may contain an
lOMoARcPSD|16756265
entire record of data (e.g. ID, name, marks, age etc). the right part represents pointer/link to the
next node. The next pointer of the last node is null pointer signal the end of the list.
1.12.5 Advantages
List of data can be stored in arrays but linked structures (pointers) provide several advantages:
A linked list is appropriate when the number of data elements to be represented in data structure
is unpredictable. It also appropriate when there are frequently insertions & deletions occurred in
the list. Linked lists are dynamic, so the length of a list can increase or decrease as necessary.
LIST is sorted list (Sorted in ascending order) in memory. This algorithm finds the
location LOC of the node where ITEM first appears in LIST or sets LOC=NULL.
Return
Return
[End of If structure]
4. Return
Write: OVERFLOW
Return
node]
Step 6: Return
Algorithm: Concatenate(INFO,LINK,START1,START2)
lOMoARcPSD|16756265
Set PTR:=LINK[PTR]
Step 4: Return
void Traverse()
{
for(Curr = Head; Curr != NULL ; Curr = Curr ->next )
cout<< Curr ->info<<”\t”;
} // end of Traverse function
int main()
{ int inf, ch;
while(1)
{ cout<< " \n\n\n\n Linked List Operations\n\n";
cout<< " 1- Add Node \n 2- Delete Node \n”;
cout<< " 3- Traverse List \n 4- exit\n";
cout<< "\n\n Your Choice: "; cin>>ch;
switch(ch)
{ case 1: cout<< "\n Put info/value to Add: ";
cin>>inf);
AddNode(inf);
break;
case 2: DeleteNode(); break;
case 3: cout<< "\n Linked List Values:\n";
Traverse(); break;
case 4: exit(0);
} // end of switch
} // end of while loop
return 0;
} // end of main ( ) function
A doubly linked list is a list that contains links to next and previous nodes. Unlike singly
lOMoARcPSD|16756265
linked lists where traversal is only one way, doubly linked lists allow traversals in both
ways.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
struct node *previous;
int data;
struct node *next;
}*head, *last;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
{
struct node *temp;
temp=last;
if(temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
printf("\nData deleted from list is %d \n",last-
>data); last=temp->previous;
last->next=NULL;
free(temp);
return 0;
}
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf("data deleted from list is %d",value);
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List is Empty");
}
while(temp!=NULL)
{
printf("-> %d ",temp->data);
temp=temp->next;
}
}
int main()
{
int value, i, loc;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) insert at begning\n2.) insert at at\n3.) insert at middle"); printf("\
n4.) delete from end\n5.) reverse the link list\n6.) display list\n7.)exit");
lOMoARcPSD|16756265
while(1)
{
printf("\n\nenter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("enter the value you want to insert in node ");
scanf("%d",&value);
insert_begning(value);
display();
break;
}
case 2:
{
printf("enter the value you want to insert in node at last ");
scanf("%d",&value);
insert_end(value);
display();
break;
}
case 3:
{
printf("after which data you want to insert data ");
scanf("%d",&loc);
printf("enter the data you want to insert in list ");
scanf("%d",&value);
insert_after(value,loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
lOMoARcPSD|16756265
break;
}
case 5:
{
printf("enter the value you want to delete");
scanf("%d",value);
delete_from_middle(value);
display();
break;
}
case 6 :
{
display();
break;
}
case 7 :
{
exit(0);
break;
}
}
}
printf("\n\n%d",last->data);
display();
getch();
}
1.13.8 Circular Linked List
A circular linked list is a linked list in which last element or node of the list points to first node.
For non-empty circular linked list, there are no NULL pointers. The memory declarations for
representing the circular linked lists are the same as for linear linked lists. All operations
performed on linear linked lists can be easily extended to circular linked lists with following
exceptions:
• While inserting new node at the end of the list, its next pointer field is made to point to
the first node.
• While testing for end of list, we compare the next pointer field with address of the first
node
lOMoARcPSD|16756265
Circular linked list is usually implemented using header linked list. Header linked list is a
linked list which always contains a special node called the header node, at the beginning of the
list. This header node usually contains vital information about the linked list such as number of
nodes in lists, whether list is sorted or not etc. Circular header lists are frequently used instead of
ordinary linked lists as many
• operations are much easier to state and implement using header listThis comes from the
following two properties of circular header linked lists:
• The null pointer is not used, and hence all pointers contain valid addresses
• Every (ordinary) node has a predecessor, so the first node may not require a special case.
linked list.
Step 1: CALL FINDBHL(INFO,LINK,START,ITEM,LOC,LOCP)
Step 2: If LOC=NULL, then:
Write: ‘item not in the list’
Exit
Step 3: Set LINK[LOCP]:=LINK[LOC] [Node deleted]
Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOC
[Memory retuned to Avail list]
Step 5: Return
Searching in circular list
Algorithm: FINDBHL(NFO,LINK,START,ITEM,LOC,LOCP)
This algorithm finds the location of the node to be deleted
and the location of the node preceding the node to be
deleted
Step 1: Set SAVE:=START and PTR:=LINK[START]
Step 2: Repeat while INFO[PTR]≠ITEM and PTR≠START
Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Loop]
Step 3: If INFO[PTR]=ITEM, then:
Set LOC:=PTR and LOCP:=SAVE
Else:
Set LOC:=NULL and LOCP:=SAVE
[End of If Structure]
Step 4: Return
Insertion in a circular header linked list
Algorithm: INSRT(INFO,LINK,START,AVAIL,ITEM,LOC)
This algorithm inserts item in a circular header linked
list after the location LOC
Step 1:If AVAIL=NULL, then
Write: ‘OVERFLOW’
Exit
Step 2: Set NEW:=AVAIL and
AVAIL:=LINK[AVAIL] Step 3: Set
INFO[NEW]:=ITEM
Step 4: Set LINK[NEW]:=LINK[LOC]
Set LINK[LOC]:=NEW
Step 5: Return
lOMoARcPSD|16756265
assigned a negative number, in this case -1. The array representation of List will require three
linear arrays as COEFF, EXP and LINK.
Set SUMPOLY:=LINK[SUMPOLY]
[End of If structure]
[End of Loop]
Step3: If POLY1=NULL , then:
Repeat while POLY2≠NULL
Set COEFF[SUMPOLY]:=COEFF[POLY2]
Set POWER[SUMPOLY]:=POWER[POLY2]
Set POLY2:=LINK[POLY2]
lOMoARcPSD|16756265
float co;
printf("Enter the number of
terms : "); scanf("%d",&n);
for(i=1;i<=n;i++)
lOMoARcPSD|16756265
{
printf("Enter coeficient for term
%d : ",i); scanf("%f",&co);
printf("Enter exponent for term
%d : ",i); scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}/*End of create()*/
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp;
tmp=(struct node *)malloc(sizeof(struct
node)); tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first
one */ if(start==NULL || ex > start-
>expo)
{
tmp-
>link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link-
>expo >= ex) ptr=ptr->link;
tmp->link=ptr-
>link; ptr-
>link=tmp;
}
return start;
}/*End of insert()*/
tmp-
>coef=co;
tmp-
>expo=ex;
/*If list is
empty*/
if(start==NUL
L)
{
tmp-
>link=start;
start=tmp;
}
else /*Insert at the end of the list*/
{
ptr=start;
while(ptr->link!
=NULL) ptr=ptr-
>link;
tmp->link=ptr-
>link; ptr-
>link=tmp;
}
return start;
}/*End of insert()*/
=NULL)
printf(" +
");
else
printf("\n");
}
}/*End of display()*/
lOMoARcPSD|16756265
printf("Added polynomial is
: "); display(start3);
}/*End of poly_add() */
UNIT 2
2 .1 STACKS
It is an ordered group of homogeneous items of elements. Elements are added to and removed
from the top of the stack (the most recently added items are at the top of the stack). The last
element to be added is the first to be removed (LIFO: Last In, First Out).
one end, called TOP of the stack. The elements are removed in reverse order of that
in which they were inserted into the stack.
Push Operation
Push an item onto the top of the stack (insert an item)