Professional Documents
Culture Documents
I B.SC CS DS Unit I
I B.SC CS DS Unit I
Data:
A collection of facts, concepts, figures, observations, occurrences or instructions in a
formalized manner.
Information:
The meaning that is currently assigned to data by means of the conventions applied to those
data(i.e. processed data)
Record:
Collection of related fields.
Data type:
Set of elements that share common set of properties used to solve a program.
Data Structures:
Data Structure is the way of organizing, storing, and retrieving data and their relationship with
each other.
Characteristics of data structures:
1. It depicts the logical representation of data in computer memory.
2. It represents the logical relationship between the various data elements.
1. Traversal
2.Search
3.Insertion
4.Deletion
CLASSIFICATION OF DATA STRUCTURES
DATA
STRUCTURES
Static Ds:
If a ds is created using static memory allocation, ie. ds formed when the number of data items
are known in advance ,it is known as static data static ds or fixed size ds.
Dynamic Ds:
If the ds is created using dynamic memory allocation i.e ds formed when the number of data
items are not known in advance is known as dynamic ds or variable size ds.
Compiler design
Operating system
Statistical analysis package
DBMS
Numerical analysis
Simulation
Artificial intelligence
Graphics
An abstract Data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form a example of an ADT. An ADT consists of data together with functions that
operate on that data.
Advantages/Benefits of ADT:
1. Modularity
2.Reuse
3. code is easier to understand
4.Implementation of ADTs can be changed without requiring changes to the program that uses the
ADTs.
If the element at position i is Ai, then its successor is Ai+1 and its predecessor is Ai-1
Various operations performed on List
Insertion and Deletion operation are expensive as it requires more data movements
#define maxsize 10
int list[maxsize], n ;
Create Operation:
Create operation is used to create the list with „ n „ number of elements .If „ n „ exceeds the
array‟s maxsize, then elements cannot be inserted into the list. Otherwise the array elements are
stored in the consecutive array locations (i.e.) list [0], list [1] and so on.
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]);
}
Insert Operation:
Insert operation is used to insert an element at particular position in the existing list. Inserting the
element in the last position of an array is easy. But inserting the element at a particular position
in an array is quite difficult since it involves all the subsequent elements to be shifted one
position to the right.
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 ]
10 20 30 40 50
If data 15 is to be inserted in the 2nd 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.
10 20 30 40 50
10 20 30 40 50
After this four data movement, 15 is inserted in the 2nd position of the array.
10 15 20 30 40 50
Deletion Operation:
Deletion is the process of removing an element from the array at any position.
Deleting an element from the end is easy. If an element is to be deleted from any particular
position ,it requires all subsequent element from that position is shifted one position towards
left.
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();
}
10 20 30 40 50
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.
10 20 30 40 50
After this 3 data movements, data 20 is deleted from the 2nd position of the array.
10 30 40 50
Search Operation:
Search( ) operation is used to determine whether a particular element is present in the list or not.
Input the search element to be checked in the list.
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");
}
Advantages of array implementation:
1. The elements are faster to access using random access
2.Searching an element is easier
Linked Lists:
A Linked list is an ordered collection of elements. Each element in the list is referred as a node.
Each node contains two fields namely,
Data field-The data field contains the actual data of the elements to be stored in the list
Next field- The next field contains the address of the next node in the list
DATA NEXT
10 40 20 30 Null
L
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
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 or One way chain
Singly linked list can be defined as the collection of ordered set of elements. The number of
elements may vary according to need of the program. A node in the singly linked list consist of
two parts: data part and link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address of its immediate
successor.
One way chain or singly linked list can be traversed only in one direction. In other words, we can
say that each node contains only next pointer, therefore we can not traverse the list in the reverse
direction.
Consider an example where the marks obtained by the student in three subjects are stored in a
linked list as shown in the figure.
There are various operations which can be performed on singly linked list. A list of all such
operations is given below.
Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
Insertion
The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following categories.
S Operation Description
N
The Deletion of a node from a singly linked list can be performed at different positions. Based on
the position of the node being deleted, the operation is categorized into the following categories.
SN Operation Description
Circular Linked List:- Circular Linked List is a special type of linked list in which all the
nodes are linked in continuous circle. Circular list can be singly or doubly linked list. Note
that, there are no Nulls in Circular Linked Lists. In these types of lists, elements can be added
to the back of the list and removed from the front in constant time.
Both types of circularly-linked lists benefit from the ability to traverse the full list beginning
at any given node. This avoids the necessity of storing first Node and last node, but we need
a special representation for the empty list, such as a last node variable which points to some
node in the list or is null if it's empty. This representation significantly simplifies adding and
removing nodes with a non-empty list, but empty lists are then a special case. Circular
linked lists are most useful for describing naturally circular structures, and have the
advantage of being able to traverse the list starting at any point. They also allow quick
access to the first and last records through a single pointer (the address of the last element)
Circular linked list are one they of liner linked list. In which the link fields of last node
of the list contains the address of the first node of the list instead of contains a null
pointer.
Advantages:- Circular list are frequency used instead of ordinary linked list because in
circular list all nodes contain a valid address. The important feature of circular list is as
follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more efficient in
circular list.
Disadvantages: Without some conditions in processing it is possible to get into an
infinite Loop.
Circular Double Linked List :- These are one type of double linked list. In which the
rpt field of the last node of the list contain the address of the first node ad the left points
of the first node contains the address of the last node of the list instead of containing null
pointer.
Advantages:- circular list are frequently used instead of ordinary linked list because in
circular list all nodes contained a valid address. The important feature of circular list is
as follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more
efficient in circular list.
Disadvantage:-Without some conditions in processes it is possible to get in to an infant glad.
DOUBLE LINKED LIST (Or) TWO WAY LINKED LIST
In certain applications it is very desirable that list be traversed in either forward direction
or Back word direction. The property of Double Linked List implies that each node must
contain two link fields instead of one. The links are used to denote the preceding and
succeeding of the node. The link denoting the preceding of a node is called Left Link.
The link denoting succeeding of a node is called Right Link. The list contain this type of
node is called a “Double Linked List” or “Two Way List”. The Node structure in the
Double Linked List is as follows:
Lptr contains the address of the before node. Rptr contains the address of next node.
Data Contains the Linked List is as follows.
In the above diagram Last and Start are pointer variables which contains the address of
last node and starting node respectively.
Insertion in to the Double Linked List:Let list be a double linked list with successive
modes A and B as shown in the following diagram. Suppose a node N is to be inserted
into the list between the node s A and B this is shown in the following diagram.
As in the new list the right pointer of node A points to the new node N ,the
Lptr of the node „N‟ points to the node A and Rptr of node „N‟ points to the
node „B‟ and Lpts of node B points the new node „N‟
Deletion Of Double Linked List :- Let list be a linked list contains node N
Support node N is to be deleted from the list diagram will appear as the above mention
double linked list. The deletion occurs as soon as the right pointer field of node A
charged, so that it points to node B and the lift point field of node B is changed. So that
it pointes to node A.
Application of Linked List
Polynomial Manipulation
Polynomial manipulations are one of the most important applications of linked lists.
Polynomials are an important part of mathematics not inherently supported as a data type by
most languages. A polynomial is a collection of different terms, each comprising coefficients,
and exponents. It can be represented using a linked list. This representation makes polynomial
manipulation efficient.
While representing a polynomial using a linked list, each polynomial term represents a node in
the linked list. To get better efficiency in processing, we assume that the term of every
polynomial is stored within the linked list in the order of decreasing exponents. Also, no two
terms have the same exponent, and no term has a zero coefficient and without coefficients. The
coefficient takes a value of 1.
o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown below:
Consider a polynomial P(x) = 7x2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients,
and 4,3,2,0 are the exponents of the terms in the polynomial. On representing this polynomial
using a linked list, we have
Observe that the number of nodes equals the number of terms in the polynomial. So we have 4
nodes. Moreover, the terms are stored to decrease exponents in the linked list. Such
representation of polynomial using linked lists makes the operations like subtraction, addition,
multiplication, etc., on polynomial very easy.
Addition of Polynomials:
To add two polynomials, we traverse the list P and Q. We take corresponding terms of the list P
and Q and compare their exponents. If the two exponents are equal, the coefficients are added to
create a new coefficient. If the new coefficient is equal to 0, then the term is dropped, and if it is
not zero, it is inserted at the end of the new linked list containing the resulting polynomial. If
one of the exponents is larger than the other, the corresponding term is immediately placed into
the new linked list, and the term with the smaller exponent is held to be compared with the next
term from the other list. If one list ends before the other, the rest of the terms of the longer list is
inserted at the end of the new linked list containing the resulting polynomial.
Let us consider an example an example to show how the addition of two polynomials is
performed,
Q (x) = 5x3 + 4 x2 - 5
These polynomials are represented using a linked list in order of decreasing exponents as
follows:
To generate a new linked list for the resulting polynomials that is formed on the addition of
given polynomials P(x) and Q(x), we perform the following steps,
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first term
of polynomials P and Q contain exponents 4 and 3, respectively. Since the exponent of the first
term of the polynomial P is greater than the other polynomial Q, the term having a larger
exponent is inserted into the new list. The new list initially looks as shown below:
We then compare the exponent of the next term of the list P with the exponents of the present
term of list Q. Since the two exponents are equal, so their coefficients are added and appended
to the new list as follows:
Then we move to the next term of P and Q lists and compare their exponents. Since exponents
of both these terms are equal and after addition of their coefficients, we get 0, so the term is
dropped, and no node is appended to the new list after this,
Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the
same exponents equal to 0. We add their coefficients and append them to the new list for the
resulting polynomial as shown below:
Polynomial of Multiple Variables
We can represent a polynomial with more than one variable, i.e., it can be two or three
variables. Below is a node structure suitable for representing a polynomial with three variables
X, Y, Z using a singly linked list.
Terms in such a polynomial are ordered accordingly to the decreasing degree in x. Those with
the same degree in x are ordered according to decreasing degree in y. Those with the same
degree in x and y are ordered according to decreasing degrees in z.