Unit 1
Unit 1
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.
1
Operations on Data Structures:
1. Traversal
2. Search
3. Insertion
4. Deletion
2
Non Linear Structures:
In non-linear data structures, there is definite order or sequence in which data elements are
arranged. For instance, a non-linear data structures could arrange data elements in a hierarchical
fashion. Examples of non-linear data structures are trees and graphs.
4
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
Declaration of Array:
#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]);
}
5
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.
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();
}
6
7
8
Advantages of array implementation:
1. The elements are faster to access using random access
2. Searching an element is easier
Applications of arrays:
Arrays are particularly used in programs that require storing large collection of similar type data
elements.
9
10
11
1. SINGLY LINKED LIST
12
13
Inserting a New Node in a Linked List
How a new node is added into an already existing linked list. We will take four cases and then
see how insertion is done in each case.
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.
OVERFLOW
Overflow is a condition that occurs when AVAIL = NULL or no free memory cell is present in
the system. When this condition occurs, the program must give an appropriate message.
14
In Step 1, we first check whether memory is available for the new node. If the free memory
has exhausted, then an OVERFLOW message is printed.
Otherwise, if a free memory cell is available, then we allocate space for the new node. Set its
DATA part with the given VAL and the next part is initialized with the address of the first
node of the list, which is stored in START.
Now, since the new node is added as the first node of the list, it will now be known as the
START node, that is, the START pointer variable will now hold the address of the
NEW_NODE.
15
Case 3: Inserting a Node after a Given Node in a Linked List
16
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.
Underflow is a condition that occurs when we try to delete a node from a linked list that is
empty. This happens when START = NULL or when there are no more nodes to delete.
Note that when we delete a node from a linked list, we actually have to free the memory
occupied by that node.
The memory is returned to the free pool so that it can be used to store other programs and
data.
Whatever be the case of deletion, we always change
2. Create a pointer last and set it to NULL. This will keep track of the last node in the merged
list.
3. Iterate over the nodes of both linked lists until either of them becomes NULL. Inside the
loop, compare the data of the current nodes pointed by first and second
- If the data of the node in the first list is smaller, append it to the merged list
- If the data of the node in the second list is smaller or equal, append it to the merged list
4. After the loop, check if there are remaining nodes in either of the two lists. If yes, append the
18
A doubly linked list is a more complex data structure than a singly linked list, but it
offers several advantages. The main advantage of a doubly linked list is that it allows for
efficient traversal of the list in both directions. This is because each node in the list contains a
pointer to the previous node and a pointer to the next node. This allows for quick and easy
insertion and deletion of nodes from the list, as well as efficient traversal of the list in both
directions.
19
Node Definition:
Here is how a node in a Doubly Linked List is typically represented:
struct Node {
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node
in the list, and a pointer to the previous node in the list. By linking these nodes together
through the next and prev pointers, we can traverse the list in both directions (forward and
backward), which is a key feature of a Doubly Linked List.
20
Traversal in Doubly Linked List:
To Traverse the doubly list, we can use the following steps:
a. Forward Traversal:
Initialize a pointer to the head of the linked list.
While the pointer is not null:
Visit the data at the current node.
Move the pointer to the next node.
b. Backward Traversal:
Initialize a pointer to the tail of the linked list.
While the pointer is not null:
Visit the data at the current node.
Move the pointer to the previous node.
21
Representation of Circular singly linked list
Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly
linked list and circular linked list in which two consecutive elements are linked or
connected by the previous and next pointer and the last node points to the first node by the
next pointer and also the first node points to the last node by the previous pointer.
Note: We will be using the singly circular linked list to represent the working of the circular
linked list.
22
Applications of Linked lists
o Polynomial Manipulation representation
o Addition of long positive integers
o Representation of sparse matrices
o Addition of long positive integers
o Symbol table creation
o Mailing list
o Memory management
o Linked allocation of files
o Multiple precision arithmetic etc
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).
23
The structure of a node of a linked list that represents a polynomial is shown below:
Consider a polynomial P(x) = 7x4 + 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.
struct poly
{
int coeff;
int power;
struct poly *next;
}*list1,*list2,*list3;
24
Creation of the Polynomial
poly create(poly*head1,poly*newnode1)
{
poly *ptr;
if (head1==NULL)
{
}
else
{
}
head1=newnode1;
return (head1);
ptr=head1;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode1;
return(head1);
}
25
{
newnode -> coeff = ptr1 -> coeff + ptr2 -> coeff;
newnode -> power = ptr1 -> power ;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else if(ptr1 -> power > ptr2 -> power )
{
}
else
{
ptr2 = ptr2 -> next;
}}}
newnode -> coeff = ptr1 -> coeff;
newnode -> power = ptr1 -> power;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;
newnode -> coeff = ptr2 -> coeff;
newnode -> power = ptr2 -> power;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr2 = ptr2 -> next;
}}}
26