Data Structures U1
Data Structures U1
asia
DATA STRUCTURE
INFORMATION TECHNOLOGY
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
UNIT – I
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
x =x + 2; n
end; n
--------------------------
Total Frequency Count 3n+1
Total Frequency Count of Program Segment C
The total frequency counts of the program segments A, B and C given by 1, (3n+1) and (3n 2+3n+1)
respectively are expressed as O(1), O(n) and O(n2). These are referred to as the time complexities of the
program segments since they are indicative of the running times of the program segments. In a similar
manner space complexities of a program can also be expressed in terms of mathematical notations,
which is nothing but the amount of memory they require for their execution.
Asymptotic Notations:
It is often used to describe how the size of the input data affects an algorithm’s usage of computational
resources. Running time of an algorithm is described as a function of input size n for large n.
Big oh(O): Definition: f(n) = O(g(n)) (read as f of n is big oh of g of n) if there exist a positive integer n0 and
a positive number c such that |f(n)| ≤ c|g(n)| for all n ≥ n0 . Here g(n) is the upper bound of the function
f(n).
f(n) g(n)
3 2 3 3
16n + 45n + n f(n) = O(n )
12n
34n – 40 n f(n) = O(n)
50 1 f(n) = O(1)
Omega(Ω): Definition: f(n) = Ω(g(n)) ( read as f of n is omega of g of n), if there exists a positive integer n0
and a positive number c such that |f(n)| ≥ c |g(n)| for all n ≥ n0. Here g(n) is the lower bound of the
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
function f(n).
f(n) g(n)
3 2 3 3
16n +8n + 2 n f(n) = Ω(n )
24n + 9 n f(n) = Ω(n)
Theta(Θ): Definition: f(n) = Θ(g(n)) (read as f of n is theta of g of n), if there exists a positive integer n0
and two positive constants c1 and c2 such that c1 |g(n)| ≤ |f(n)| ≤ c2 |g(n)| for all n ≥ n0. The function g(n)
is both an upper bound and a lower bound for the function f(n) for all values of n, n ≥ n0 .
f(n) g(n)
16n3 + 30n2 –
n2 f(n) = Θ(n2 )
90
7. 2n + 30n 2n f(n) = Θ (2n)
Little oh(o): Definition: f(n) = O(g(n)) ( read as f of n is little oh of g of n), if f(n) = O(g(n)) and f(n) ≠
Ω(g(n)).
f(n) g(n)
2
f(n) = o(n2) since f(n) = O(n2 ) and
18n + 9 n
f(n) ≠ Ω(n2 ) however f(n) ≠ O(n).
Relations Between O, Ω, Θ:
Theorem : For any two functions g(n) and f(n),
f(n) = (g(n)) iff
f(n) = O(g(n)) and f(n) = (g(n)).
Time Complexity:
Constant O(1) Constant number of operations, not depending on the input data
size.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Quadratic O(n2 ) Number of operations proportional to the square of the size of the
input data.
Cubic O(n3 ) Number of operations proportional to the cube of the size of the
input data.
O(kn )
O(n!)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Recursion Definition:
1. Recursion is a technique that solves a problem by solving a smaller problem of the same type.
2. A recursive function is a function invoking itself, either directly or indirectly.
3. Recursion can be used as an alternative to iteration.
4. Recursion is an important and powerful tool in problem solving and programming.
5. Recursion is a programming technique that naturally implements the divide and conquer
problem solving methodology.
Four criteria of a Recursive Solution:
1. A recursive function calls itself.
2. Each recursive call solves an identical, but smaller problem.
3. A test for the base case enables the recursive calls to stop.
There must be a case of the problem(known as base case or stopping case) that is
handled differently from the other cases.
In the base case, the recursive calls stop and the problem is solved directly.
4. Eventually, one of the smaller problems must be the base case.
Example: To define n! recursively, n! must be defined in terms of the factorial of a smaller number.
n! = n * (n-1)!
Base case: 0! =1
n! = 1 if n=0
n! = n * (n-1)! if n >0
Designing Recursive Algorithms:
The Design Methodology:
1. The statement that solves the problem is known as the base case.
2. Every recursive algorithm must have a base case. The rest of the algorithm is known as the
general case.
3. The general case contains the logic needed to reduce the size of the problem.
Example: in factorial example, the base case is fact(0) and the general case is n * fact(n-1).
Rules for designing a Recursive Algorithm:
1. First, determine the base case.
2. Then determine the general case.
3. Combine the base case and the general cases into an algorithm.
Limitations of Recursion:
1. Recursion works best when the algorithm uses a data structure that naturally supports recursion.
E.g. Trees.
2. In other cases, certain algorithms are naturally suited to recursion. E.g. binary search, towers of
hanoi.
3. On the other hand, not all looping algorithms can or should be implemented with reursion.
4. Recursive solutions may involve extensive overhead (both time and memory) because they use
calls. Each call takes time to execute. A recursive algorithm therefore generally runs more slowly
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Fibonacci Design: To start a fibonacci series, we need to know the first two numbers.
Fibonacci(n) = 0 if n=0 Base case
1 if n=1
General case
Fibonacci(n-1) + fibonacci(n-2)
Tracing a Recursive Function:
1. A stack is used to keep track of function calls.
2. Whenever a new function is called, all its parameters and local variables are pushed onto the
stack along with the memory address of the calling statement.
3. For each function call, an activation call is created on the stack.
Difference between Recursion and Iteration:
1. A function is said to be recursive if it calls itself again and again within its body whereas iterative
functions are loop based imperative functions.
2. Reursion uses stack whereas iteration does not use stack.
3. Recursion uses more memory than iteration as its concept is based on stacks.
4. Recursion is comparatively slower than iteration due to overhead condition of maintaining
stacks.
5. Recursion makes code smaller and iteration makes code longer.
6. Iteration terminates when the loop-continuation condition fails whereas recursion terminates
when a base case is recognized.
7. While using recursion multiple activation records are created on stack for each call where as in
iteration everything is done in one activation record.
8. Infinite recursion can crash the system whereas infinite looping uses CPU cycles repeatedly.
9. Recursion uses selection structure whereas iteration uses repetetion structure.
Types of Recursion:
Recursion is of two types depending on whether a function calls itself from within itself or whether two
functions call one another mutually. The former is called direct recursion and the later is called indirect
recursion. Thus there are two types of recursion:
Direct Recursion
Indirect Recursion
Recursion may be further categorized as:
Linear Recursion
Binary Recursion
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Multiple Recursion
Linear Recursion:
It is the most common type of Recursion in which function calls itself repeatedly until base condition
[termination case] is reached. Once the base case is reached the results are return to the caller function.
If a recursive function is called only once then it is called a linear recursion.
fact(int f)
{
if (f == 1) return 1;
return (f * fact(f - 1)); //called in function only once
}
int main()
{
int fact;
fact = fact(5);
printf("Factorial is %d", fact);
return 0;
}
Binary Recursion:
Some recursive functions don't just have one call to themselves; they have two (or more). Functions with
two recursive calls are referred to as binary recursive functions.
Example1: The Fibonacci function fib provides a classic example of binary recursion. The Fibonacci
numbers can be defined by the rule:
fib(n) = 0 if n is 0,
= 1 if n is 1,
= fib(n-1) + fib(n-2) otherwise
For example, the first seven Fibonacci numbers are
Fib(0) = 0
Fib(1) = 1
Fib(2) = Fib(1) + Fib(0) = 1
Fib(3) = Fib(2) + Fib(1) = 2
Fib(4) = Fib(3) + Fib(2) = 3
Fib(5) = Fib(4) + Fib(3) = 5
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Tail Recursion:
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last thing the function
does. Often, the value of the recursive call is returned. As such, tail recursive functions can often be
easily implemented in an iterative manner; by taking out the recursive call and replacing it with a loop,
the same effect can generally be achieved. In fact, a good compiler can recognize tail recursion and
convert it to iteration in order to optimize the performance of the code.
A good example of a tail recursive function is a function to compute the GCD, or Greatest Common
Denominator, of two numbers:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Time-Complexity: O(ln n)
Fibonacci(n)
Input: integer n ≥ 0
Output: Fibonacci Series: 1 1 2 3 5 8 13………………………………..
1. if n=1 or n=2
2. then Fibonacci(n)=1
3. else Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
Towers of Hanoi
Input: The aim of the tower of Hanoi problem is to move the initial n different sized disks from needle A
to needle C using a temporary needle B. The rule is that no larger disk is to be placed above the smaller
disk in any of the needle while moving or at any time, and only the top of the disk is to be moved at a
time from any needle to any needle.
Output:
Linear Search:
1. Read search element.
2. Call function linear search function by passing N value, array and search element.
3. If a[i] ==k, return i value, else return -1, returned value is stored in pos.
4. If pos ==-1 print element not found, else print pos+1 value.
Source Code: (Recursive)
#include<stdio.h>
#include<conio.h>
void linear_search(int n,int a[20],int i,int k)
{
if(i>=n)
{
printf("%d is not found",k);
return;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
}
if(a[i]==k)
{
printf("%d is found at %d",k,i+1);
return;
}
else
linear_search(n,a,i+1,k);
}
void main()
{
int i,a[20],n,k;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter search element:");
scanf("%d",&k);
linear_search(n,a,0,k);
getch();
}
Enter no of elements:5
Enter elements:1 2 3 4 5
Enter search element:3
3 is found at position 3
Time Complexity of Linear Search:
If input array is not sorted, then the solution is to use a sequential search.
Unsuccessful search: O(N)
Successful Search: Worst case: O(N)
Average case: O(N/2)
Binary Search:
1. Read search data.
2. Call binary_search function with values N, array, and data.
3. If low is less than high, making mid value as mean of low and high.
4. If a [mid] ==data, make flag=1 and break, else if data is less than a[mid] make high=mid-1,else
low=mid+1.
5. If flag ==1, print data found at mid+1, else not found.
Source Code: (Recursive)
#include<stdio.h>
#include<conio.h>
void binary_search(int a[20],int data,int low,int high)
{
int mid;
if(low<=high)
{
mid=(low+high)/2;
if(a[mid]==data)
printf("Data found at %d",mid+1);
else
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
if(a[mid]>data)
binary_search(a,data,low,mid-1);
else
binary_search(a,data,mid+1,high);
}
}
void main()
{
int i,a[20],n,data;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter search element:");
scanf("%d",&data);
binary_search(a,data,0,n-1);
getch();
}
Enter no of elements:3
Enter elements:1 2 3
Enter search element:3
Data found at 3
Time Complexity of Binary Search:
Time Complexity for binary search is O(log2 N)
Fibonacci Search:
Source Code: (Recursive)
#include<stdio.h>
#include<conio.h>
void fib_search(int a[],int n,int search,int pos,int begin,int end)
{
int fib[20]={0,1,1,2,3,5,8,13,21,34,55,89,144};
if(end<=0)
{
printf("\nNot found");
return;//data not found
}
else
{
pos=begin+fib[--end];
if(a[pos]==search && pos<n)
{
printf("\n Found at %d",pos);
return;//data found
}
if((pos>=n)||(search<a[pos]))
fib_search(a,n,search,pos,begin,end);
else
{
begin=pos+1;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
end--;
fib_search(a,n,search,pos,begin,end);
}
}
}
void main()
{
int n,i,a[20],search,pos=0,begin=0,k=0,end;
int fib[20]={0,1,1,2,3,5,8,13,21,34,55,89,144};
clrscr();
printf("Enter the n:");
scanf("%d",&n);
printf("Enter elements to array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search element:");
scanf("%d",&search);
while(fib[k]<n)
{
k++;
}
end=k;
printf("Max.no of passes : %d",end);
fib_search(a,n,search,pos,begin,end);
getch();
}
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
LINKED LIST
Introduction to Linked List:
A linked list is a linear collection of data elements, called nodes, where the linear order is given by
means of pointers.Each node is divided into two parts:
1. The first part contains the information of the element and
2. The second part contains the address of the next node (link /next pointer field) in the list.
The data items in the linked list are not in consecutive memory locations. They may be anywhere, but
the accessing of these data items is easier as each data item contains the address of the next data item.
Linked lists using dynamic variables:
1. In array implementation of the linked lists a fixed set of nodes represented by an array is
established at the beginning of the execution
2. A pointer to a node is represented by the relative position of the node within the array.
3. In array implementation, it is not possible to determine the number of nodes required for the
linked list. Therefore;
a. Less number of nodes can be allocated which means that the program will have
overflow problem.
b. More number of nodes can be allocated which means that some amount of the
memory storage will be wasted.
4. The solution to this problem is to allow nodes that are dynamic, rather than static.
5. When a node is required storage is reserved /allocated for it and when a node is no longer
needed, the memory storage is released /freed.
Allocating and freeing dynamic variables:
1. C library function malloc() is used for dynamically allocating a space to a pointer. Note that the
malloc() is a library function in <stdlib.h> header file.
2. The following lines allocate an integer space from the memory pointed by the pointer p.
int *p;
p = (int *) malloc(sizeof(int));
Note that sizeof() is another library function that returns the number of bytes required for the operand.
In this example, 4 bytes for the int.
Advantages of linked lists:
1. Linked lists are dynamic data structures. i.e., they can grow or shrink during the execution of
a program.
2. Linked lists have efficient memory utilization. Here, memory is not pre-allocated. Memory is
allocated whenever it is required and it is de-allocated (removed) when it is no longer
needed.
3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in inserting a
data item at a specified position and deletion of the data item from the given position.
4. Many complex applications can be easily carried out with linked lists.
Disadvantages of linked lists:
1. It consumes more space because every node requires a additional pointer to store address
of the next node.
2. Searching a particular element in list is difficult and also time consuming.
Types of Linked Lists:
Basically we can put linked lists into the following four items:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
A double linked list is one in which all nodes are linked together by multiple links which helps in
accessing both the successor node (next node) and predecessor node (previous node) from any arbitrary
node within the list. Therefore each node in a double linked list has two link fields (pointers) to point to
the left node (previous) and the right node (next). This helps to traverse in forward direction and
backward direction.
A circular linked list is one, which has no beginning and no end. A single linked list can be made a circular
linked list by simply storing address of the very first node in the link field of the last node.
A circular double linked list is one, which has both the successor pointer and predecessor pointer in the
circular manner.
1. Linked lists are used to represent and manipulate polynomial. Polynomials are expression
containing terms with non zero coefficient and exponents. For example:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
2. Represent very large numbers and operations of the large number such as addition,
multiplication and division.
The simplest kind of linked list is a singly-linked list, which has one link per node. This link points to the
next node in the list, or to a null value or empty list if it is the final node.
A singly linked list's node is divided into two parts. The first part holds or points to information
about the node, and second part holds the address of next node. A singly linked list travels one way.
The beginning of the linked list is stored in a "start" pointer which points to the first node. The first node
contains a pointer to the second node. The second node contains a pointer to the third node, ... and so
on. The last node in the list has its next field set to NULL to mark the end of the list. Code can access any
node in the list by starting at the start and following the next pointers.
The start pointer is an ordinary local pointer variable, so it is drawn separately on the left top to show
that it is in the stack. The list nodes are drawn on the right to show that they are allocated in the heap.
1. Creating a structure with one data item and a next pointer, which will be pointing to next
node of the list. This is called as self-referential structure.
2. Initialize the start pointer to be NULL.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Creation.
Insertion.
Deletion.
Traversing.
Insertion of a Node:
The new node can then be inserted at three different places namely:
1. Inserting a node at the beginning.
2. Inserting a node at the end.
3. Inserting a node at intermediate position.
Inserting a node at the beginning:
The following steps are to be followed to insert a new node at the beginning of the list:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
1. The following steps are followed to insert a new node at the end of the list:
newnode = getnode();
temp = start;
while(temp -> next != NULL)
1. temp = temp -> next;
temp -> next = newnode;
1. The following steps are followed, to insert a new node in an intermediate position in the list:
4. Store the starting address (which is in start pointer) in temp and prev pointers. Then traverse
the temp pointer upto the specified position followed by prev pointer.
5. After reaching the specified position, follow the steps given below:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Deletion of a node:
A node can be deleted from the list from three different places namely.
The following steps are followed, to delete a node at the beginning of the list:
i. temp = start;
ii. start = start -> next;
iii. free(temp);
1. The following steps are followed to delete a node at the end of the list:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
The following steps are followed, to delete a node from an intermediate position in the list (List must
contain more than two node).
ctr = 1;
prev = temp;
ctr++;
free(temp);
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct linklist
int data;
};
node *start=NULL;
int menu()
int ch;
printf("\n\t ---------------------------------------\n");
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d",&ch);
return ch;
node* getnode()
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("Enter data:\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
int countnode(node*start)
if(start==NULL)
return 0;
else
return 1+countnode(start->next);
void createlist(int n)
int i;
node *newnode;
node *temp;
for(i=0;i<n;i++)
newnode=getnode();
if(start==NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
start=newnode;
else
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
void traverse()
node *temp;
temp=start;
if(start==NULL)
return;
else
while(temp!=NULL)
printf("%d\t",temp->data);
temp=temp->next;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
printf("X");
if(start==NULL)
return;
else
rev_traverse(start->next);
printf("%d\t",start->data);
void insert_at_beg()
node *newnode;
newnode=getnode();
if(start==NULL)
start=newnode;
newnode->next=start;
start=newnode;
void insert_at_end()
node *newnode,*temp;
newnode=getnode();
if(start==NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
start=newnode;
else
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
void insert_at_mid()
node *newnode,*pre,*temp;
int pos,ctr=1,nodectr;
printf("Enter position:");
scanf("%d",&pos);
nodectr=countnode(start);
newnode=getnode();
temp=pre=start;
while(ctr<pos)
pre=temp;
temp=temp->next;
ctr++;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
pre->next=newnode;
newnode->next=temp;
else
void del_at_beg()
node *temp;
if(start==NULL)
printf("List is empty");
return;
else
temp=start;
start=temp->next;
free(temp);
printf("Node is deleted");
void del_at_end()
node *pre,*temp;
if(start==NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
printf("List is empty");
return;
else
temp=start;
while(temp->next!=NULL)
pre=temp;
temp=temp->next;
pre->next=NULL;
free(temp);
printf("\Node deleted");
void del_at_mid()
int pos,ctr=1,nodectr;
node *temp,*pre;
nodectr=countnode(start);
if(start==NULL)
printf("List is empty");
return;
else
printf("Enter position:");
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d",&pos);
pre=temp=start;
while(ctr<pos)
pre=temp;
temp=temp->next;
ctr++;
pre->next=temp->next;
free(temp);
else
void main(void)
int ch,n;
clrscr();
while(1)
ch=menu();
switch(ch)
case 1:
if(start==NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d",&n);
createlist(n);
printf("List is created");
break;
else
break;
case 2:traverse();
break;
case 3:
rev_traverse(start);
printf("X");
break;
break;
case 5: insert_at_beg();
break;
case 6: insert_at_end();
break;
case 7: insert_at_mid();
break;
case 8: del_at_beg();
break;
case 9: del_at_end();
break;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
break;
Output:
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Enter data:
Enter data:
Enter data:
Enter data:
Enter data:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
List is created
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
1 2 3 4 5 X
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
10.Deletion at Middle
5 4 3 2 1 X
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Number of nodes:5
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
9.Deletion at End
10.Deletion at Middle
Enter data:
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
0 1 2 3 4 5 X
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Enter data:
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
0 1 2 3 4 5 6 X
---------------------------------------
1.Create list
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Enter position:4
Enter data:
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
0 1 2 7 3 4 5 6 X
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Node is deleted
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
1 2 7 3 4 5 6 X
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Node deleted
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
1 2 7 3 4 5 X
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
Enter position:3
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
1 2 3 4 5 X
---------------------------------------
1.Create list
4.Number of nodes
5.Insertion at Beginning
6.Insertion at End
7.Insertion at Middle
8.Deletion at Beginning
9.Deletion at End
10.Deletion at Middle
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct slinkedlist
int data;
};
node *start=NULL;
int nodectr;
node *getnode()
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
node *newnode;
newnode=(node *)malloc(sizeof(node));
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
node *temp,*newnode;
int i;
for(i=0;i<n;i++)
newnode=getnode();
if(start==NULL)
start=newnode;
else
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
return start;
node *temp;
temp=start1;
while(temp->next!=NULL)
temp=temp->next;
temp->next=start2;
temp=start1;
while(temp!=NULL)
printf("%5d",temp->data);
temp=temp->next;
int menu()
int ch;
printf("\n*********************");
printf("\n1.Create lists");
printf("\n2.Mergelists");
printf("\n3.Exit");
scanf("%d",&ch);
return ch;
void main()
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
int n,ch;
do
ch=menu();
switch (ch)
scanf("%d",&n);
start1=createlist(start,n);
scanf("%d",&n);
start2=createlist(start,n);
break;
case 2:merge(start1,start2);
break;
}while(ch!=3);
getch();
Output:
*********************
1.Creat list 1
2.Create list 2
3.Mergelists
4.Exit
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Enter data:1
Enter data:2
Enter data:3
Enter data:4
Enter data:5
Enter data:6
Enter data:7
1 2 3 4 5 6 7
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
node *start=NULL;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
node * getnode()
node * ptr;
ptr=(node *)malloc(sizeof(node));
printf("Enter data:\n");
scanf("%d",&ptr->data);
ptr->link=NULL;
return ptr;
void create_ll(int N)
node *ptr,*t;
int i;
scanf("%d",&N);
for(i=0;i<N;i++)
ptr=getnode();
if(start==NULL)
start=ptr;
else
t=start;
while(t->link!=NULL)
t=t->link;
t->link=ptr;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
if(start==NULL)
return;
else
reverse_ll(start->link);
printf("%d ",start->data);
int menu()
int ch;
printf("1.Create list\n");
printf("3.Exit\n");
scanf("%d",&ch);
return ch;
void main()
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
int n,ch;
while(1)
ch=menu();
switch(ch)
case 1:
create_ll(n);
printf("List created\n");
break;
case 2:
reverse_ll(start);
printf("\n");
break;
case 3:
exit(0);
Output:
1.Create list
3.Exit
Enter data:
12
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Enter data:
56
Enter data:
33
List created
1.Create list
3.Exit
1.Create list
3.Exit
The computer implementation requires implementing polynomials as a list of pairs of coefficient and
exponent. Each of these pairs will constitute a structure, so a polynomial will be represented as a list of
structures. A linked list structure that represents polynomials 5x4 – 8x3 + 2x2 + 4x1 + 9x0 illustrates in
figure ----
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
struct link
float coef;
int expo;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
};
node *getnode()
node *temp;
temp=(node*)malloc(sizeof(node));
printf("Enter coefficient:");
fflush(stdin);
scanf("%f",&temp->coef);
printf("Enter exponent:");
fflush(stdin);
scanf("%d",&temp->expo);
temp->next=NULL;
return temp;
char ch;
node *temp,*newnode;
while(1)
ch=getch();
if(ch=='n')
break;
newnode=getnode();
if(p==NULL)
p=newnode;
else
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
temp=p;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
return p;
node *t=p;
while(t!=NULL)
printf("+%.f",t->coef);
printf("x^%d",t->expo);
t=t->next;
void main()
node *poly1=NULL;
poly1=create_poly(poly1);
printf("Polynomial 1:");
display(poly1);
Output:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Enter coefficient:12
Enter exponent:3
Enter coefficient:45
Enter exponent:4
Polynomial 1:+12x^3+45x^4
A sparse matrix is a matrix populated primarily with zeros as elements of the table.
[ 11 22 0 0 0 0 0 ]
[ 0 33 44 0 0 0 0 ]
[ 0 0 55 66 77 0 0 ]
[ 0 0 0 0 0 88 0 ]
[ 0 0 0 0 0 0 99 ]
The above sparse matrix contains only 9 nonzero elements of the 35, with 26 of those elements as zero.
The basic data structure for a matrix is a two-dimensional array. Each entry in the array represents an
element ai,j of the matrix and can be accessed by the two indices i and j. Traditionally, i indicates the
row number (top-to-bottom), while j indicates the column number (left-to-right) of each element in the
table. For an m×n matrix, enough memory to store up to (m×n) entries to represent the matrix is
needed.
Source Code:
#include<stdio.h>
void main()
int matrix[20][20],m,n,total_elements,total_zeros=0,i,j;
scanf("%d %d",&m,&n);
total_elements=m*n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d",&matrix[i][j]);
if(matrix[i][j]==0)
total_zeros++;
if(total_zeros>total_elements/2)
printf("Row\tColumn\tValue\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(matrix[i][j]!=0)
printf(" %d \t %d \t %d\n",i,j,matrix[i][j]);
else
Output:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
10
0 0 2
1 2 3
2 1 10
It is just a single linked list in which the link field of the last node points back to the address of the first
node. A circular linked list has no beginning and no end. It is necessary to establish a special pointer
called start pointer always pointing to the first node of the list.
1. Creation.
2. Insertion.
3. Deletion.
4. Traversing.
Creating a circular single Linked List with ‘n’ number of nodes:
The following steps are to be followed to insert a new node at the beginning of the circular list:
start = newnode;
The following steps are followed to insert a new node at the end of the list:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
The following steps are followed, to delete a node at the beginning of the list:
The following steps are followed to delete a node at the end of the list:
prev = start;
prev = temp;
3. After deleting the node, if the list is empty then start = NULL.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
The following steps are followed, to traverse a list from left to right:
do
printf("%d ",
} while(temp != start);
Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct clinklist
int data;
};
node *start=NULL;
int nodectr;
int menu()
int ch;
printf("\n\t ---------------------------------------\n");
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d",&ch);
return ch;
node *getnode()
node *newnode;
newnode=(node *)malloc(sizeof(node));
printf("Enter data:\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
void createlist(int n)
int i;
node *newnode;
node *temp;
nodectr=n;
for(i=0;i<n;i++)
newnode=getnode();
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
if(start==NULL)
start=newnode;
else
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
newnode->next=start;
void display()
node *temp;
temp=start;
if(start==NULL)
return;
else
do
printf("%d\t",temp->data);
temp=temp->next;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
}while(temp!=start);
printf("X");
void insert_at_beg()
node *newnode,*last;
newnode=getnode();
if(start==NULL)
start=newnode;
else
last=start;
while(last->next!=start)
last=last->next;
newnode->next=start;
start=newnode;
last->next=start;
nodectr++;
void insert_at_end()
node *newnode,*temp;
newnode=getnode();
if(start==NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
start=newnode;
else
temp=start;
while(temp->next!=start)
temp=temp->next;
temp->next=newnode;
newnode->next=start;
nodectr++;
void insert_at_mid()
node *newnode,*pre,*temp;
int pos,ctr=1;
printf("Enter position:");
scanf("%d",&pos);
newnode=getnode();
temp=pre=start;
while(ctr<pos)
pre=temp;
temp=temp->next;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
ctr++;
pre->next=newnode;
newnode->next=temp;
nodectr++;
else
void del_at_beg()
node *temp,*last;
if(start==NULL)
printf("List is empty");
return;
else
last=temp=start;
while(last->next!=start)
last=last->next;
start=temp->next;
last->next=start;
free(temp);
nodectr--;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
printf("Node is deleted");
if(nodectr==0)
start=NULL;
void del_at_end()
node *temp,*pre;
if(start==NULL)
printf("List is empty");
return;
else
temp=start;
pre=start;
while(temp->next!=start)
pre=temp;
temp=temp->next;
pre->next=NULL;
free(temp);
nodectr--;
printf("Node is deleted");
if(nodectr==0)
start=NULL;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
void del_at_mid()
int pos,ctr=0;
node *temp,*pre;
if(start==NULL)
printf("List is empty");
return;
else
printf("Enter position:");
scanf("%d",&pos);
pre=temp=start;
while(ctr<pos-1)
pre=temp;
temp=temp->next;
ctr++;
pre->next=temp->next;
free(temp);
nodectr--;
printf("\nNode deleted");
else
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
void main(void)
int ch,n;
clrscr();
while(1)
ch=menu();
switch(ch)
case 1:if(start==NULL)
scanf("%d",&n);
createlist(n);
printf("List is created");
break;
else
case 2: display();break;
case 4: insert_at_beg();break;
case 5: insert_at_end();break;
case 6: insert_at_mid();break;
case 7: del_at_beg();break;
case 8: del_at_end();break;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
case 9: del_at_mid();break;
Output:
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter data:
Enter data:
Enter data:
Enter data:
Enter data:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
List is created
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
2 3 4 6 7 X
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Number of nodes:5
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter data:
Node is inserted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
1 2 3 4 6 7 X
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter data:
Node is inserted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
9.Deletion at Middle
1 2 3 4 6 7 8 X
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter position:5
Enter data:
Node is inserted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
1 2 3 4 5 6 7 8 X
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Node is deleted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Node is deleted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter position:7
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
8.Deletion at End
9.Deletion at Middle
Number of nodes:6
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter position:3
Node deleted
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
8.Deletion at End
9.Deletion at Middle
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
9.Deletion at Middle
Enter position:7
---------------------------------------
1.Create list
3.Number of nodes
4.Insertion at Begining
5.Insertion at End
6.Insertion at Middle
7.Deletion at Beginning
8.Deletion at End
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
9.Deletion at Middle
Enter position:3
Node deleted
Doubly-linked list is a more sophisticated form of linked list data structure. Each node of the list contain
two references (or links) – one to the previous node and other to the next node. The previous link of the
first node and the next link of the last node points to NULL. In comparison to singly-linked list, doubly-
linked list requires handling of more pointers but less information is required as one can use the
previous links to observe the preceding element. It has a dynamic size, which can be determined only at
run time.
A double linked list is a two-way list in which all nodes will have two links. This helps in accessing both
successor node and predecessor node from the given node position. It provides bi-directional traversing.
Each node contains three fields:
1. Left link.
2. Data.
3. Right link.
The left link points to the predecessor node and the right link points to the successor node. The data
field stores the required data. The basic operations in a double linked list are:
1. Creation.
2. Insertion.
3. Deletion.
4. Traversing.
The beginning of the double linked list is stored in a "start" pointer which points to the first node. The
first node’s left link and last node’s right link is set to NULL.
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
Creating a double linked list starts with creating a node. Sufficient memory has to be allocated for
creating a node. The information is stored in the memory, allocated by using the malloc() function.
The following steps are to be followed to insert a new node at the beginning of the list:
start = newnode;
The following steps are followed to insert a new node at the end of the list:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
temp = start;
The following steps are followed, to insert a new node in an intermediate position in the list:
1. Get the new node using getnode().
newnode=getnode();
2. Ensure that the specified position is in between first node and last node. If not, specified
position is invalid. This is done by countnode() function.
3. Store the starting address (which is in start pointer) in temp and prev pointers. Then traverse
the temp pointer upto the specified position followed by prev pointer.
4. After reaching the specified position, follow the steps given below:
newnode -> left = temp;
The following steps are followed, to delete a node at the beginning of the list:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
temp = start;
free(temp);
The following steps are followed to delete a node at the end of the list:
free(temp);
The following steps are followed, to delete a node from an intermediate position in the list (List must
contain more than two nodes).
temp = start;
i = 1;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
i++;
free(temp);
The following steps are followed, to traverse a list from left to right:
temp = start;
while(temp != NULL)
The following steps are followed, to traverse a list from right to left:
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
while(temp != NULL)
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct dlinklist
int data;
};
node* getnode()
node * newnode;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
return newnode;
if(start == NULL)
return 0;
else
int menu()
int ch;
clrscr();
printf("\n 1.Create");
printf("\n------------------------------");
printf("\n------------------------------");
printf("\n------------------------------");
printf("\n------------------------------");
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
scanf("%d", &ch);
return ch;
void createlist(int n)
int i;
node *newnode;
node *temp;
newnode = getnode();
if(start == NULL)
start = newnode;
else
temp = start;
void traverse_left_to_right()
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
node *temp;
temp = start;
if(start == NULL )
else
while(temp != NULL)
void traverse_right_to_left()
node *temp;
temp = start;
if(start == NULL)
else
while(temp != NULL)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
void dll_insert_beg()
node *newnode;
newnode = getnode();
if(start == NULL)
start = newnode;
else
start = newnode;
void dll_insert_end()
newnode = getnode();
if(start == NULL)
start = newnode;
else
temp = start;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
void dll_insert_mid()
node *newnode,*temp;
newnode = getnode();
scanf("%d", &pos);
nodectr = countnode(start);
return;
temp = start;
ctr++;
else
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
void dll_delete_beg()
node *temp;
if(start == NULL)
getch();
return ;
else
temp = start;
free(temp);
void dll_delete_last()
node *temp;
if(start == NULL)
getch();
return ;
else
temp = start;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
free(temp);
temp = NULL;
void dll_delete_mid()
node *temp;
if(start == NULL)
getch();
return;
else
scanf("%d", &pos);
nodectr = countnode(start);
getch();
return;
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
temp = start;
i = 1;
i++;
free(temp);
else
getch();
void main(void)
int ch, n;
clrscr();
while(1)
ch = menu();
switch( ch)
jntuworldupdates.org Specworld.in
Smartzworld.com Smartworld.asia
case 1 :
scanf("%d", &n);
createlist(n);
break;
case 2 :
dll_insert_beg();
break;
case 3 :
dll_insert_end();
break;
case 4 :
dll_insert_mid();
break;
case 5 :
dll_delete_beg();
break;
case 6 :
dll_delete_last();
break;
case 7 :
dll_delete_mid();
break;
case 8 :
traverse_left_to_right();
break;
case 9 :
traverse_right_to_left();
break;
jntuworldupdates.org Specworld.in