Академический Документы
Профессиональный Документы
Культура Документы
MANUAL BY:
MS SARIKA ARORA
ASSISTANT PROFESSOR, IT DEPT
KCL-IMT, JALANDHAR
DATA STRUCTURES:
ALGORITHMS:
So, algorithm is a finite set of instructions or logic, written in order, to accomplish a certain
predefined task. Algorithm is not the complete code or program, it is just the core logic (solution)
of a problem, which can be expressed either as an informal high level description
as pseudocode or using a flowchart.
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following
properties :
1. Time Complexity
2. Space Complexity
Space Complexity
Its the amount of memory space required by the algorithm, during the course of its execution.
Space complexity must be taken seriously for multi-user systems and in situations where limited
memory is available.
An algorithm generally requires space for following components:
Instruction Space: Its the space required to store the executable version of the program.
This space is fixed, but varies depending upon the number of lines of code in the program.
Data Space : Its the space required to store all the constants and variables value.
Environment Space : Its the space required to store the environment information needed to
resume the suspended function.
Time Complexity
Time Complexity is a way to represent the amount of time needed by the program to run to
completion.
1. ARRAYS:
i)
Matrix Addition
Algorithm
Matadd(a,b,m,n)
1. For i=1to m
2. For j=1 to n
3. C[i][j]=a[i][j]+b[i][j]
4. Exit
Output
ii)
Traversing of arrays.
for(i=1;i<=n;i++)
{
printf("Element number [%d] is ",i);
scanf("%d",&A[i]);
}
LB=1;
UB=n;
k=LB;
printf("\n");
printf("Traversing of array is:\n");
while(k<=UB)
{
printf("Element number [%d] is %d\n",k,A[k]);
k=k+1;
}
getch();
}
Output
iii)
a) At any position
Write a program to insert element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,n,p,el;
clrscr();
printf("Enter the array element\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the positon and element\n");
scanf("%d",&p);
printf("Enter element\n");
scanf("%d",&el);
for(i=5;i>(p-1);i--)
{
a[i]=a[i-1];
}
a[i]=el;
printf(After insertion\n);
for(i=0;i<6;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Output
b) At end of array.
Write a program to insert element in end of the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,p=0,el,j,z;
printf("Enter how many element you want to enter\n");
scanf("%d",&z);
printf("Elemet the array element\n");
for(i=0;i<z;i++)
{
scanf("%d",&a[i]);
p++;
}
printf("Element inserted\n");
if(p>9)
printf("Array is full");
else
{
printf("Enter element for insert in end\n");
scanf("%d",&el);
a[p]=el;
p++;
printf("Array after insertion at end\n");
for(i=0;i<p;i++)
printf("%d\n",a[i]);
}
getch();
}
Output
iv)
Output
v)
Searching
a) Linear Search:
getch();
}
Output
b) Binary Search
Output
vi)
Sorting
Sorting is storage of data in sorted order , it can be in ascending or descending order. The term
Sorting comes into picture with the term Searching. There are so many things in our real life that
we need to search, like a particular record in database, roll numbers in merit list, a particular
telephone number, any particular page in a book etc.
Sorting arranges data in a sequence which makes searching easier. Every record which is going
to be sorted will contain one key. Based on the key the record will be sorted.
Sorting Efficiency
There are many techniques for sorting. Implementation of particular sorting technique depends
upon situation. Sorting techniques mainly depends on two parameters. First parameter is the
execution time of program, which means time taken for execution of program. Second is the
space, which means space, taken by the program .
a) Bubble Sort
Output
b) Insertion Sort
Algorithm:
Output
c) Selection Sort
printf("%d,",a[i]);
}
Output
2. STACKS
Stack is an abstract data type with a bounded (predefined) capacity. It is a simple data structure
that allows adding and removing elements in a particular order. Every time an element is added,
it goes on the top of the stack, the only element that can be removed is the element that was at
the top of the stack, just like a pile of objects.
Applications of Stack
The simplest application of a stack is to reverse a word. You push a given word to stack - letter
by letter - and then pop letters from the stack.
There are other uses also like : Parsing, Expression Conversion(Infix to Postfix, Postfix to
Prefix etc) and many more.
Data Structures using C
i)
Recursion.
a) Factorial of a number
Output
OUTPUT:
Hello
Hello
Hello
Hello
Hello
Hello
ii)
Output
iii)
{
x=pop(&s);
printf("%c",x);
}
push(&s,token);
}
}
}
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
getch();
}
int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
Data Structures using C
Output
iv)
QUANTITY
001
450
002
003
487
004
101
005
500
006
007
359
**Delete the items having quantity zero and update the stack.
#include<stdio.h>
#include<conio.h>
void main()
{
int top,a[]={001,002,003,004,005,006,007};
int top1,b[]={450,0,487,101,500,0,359};
clrscr();
printf("\n \t ITEMS IN STACK ARE:");
for(top=0;top<7;top++)
{
printf("\n \t \t %d ",a[top]);
printf("\t \t %d ",b[top]);
} printf("\n \n \t AFTER DELETION ITEMS IN STACK
ARE:");
for(top=0;top<7;top++)
{
if(b[top]==0)
{
top1=top;
while(top1<7)
{
a[top1]=a[top1+1];
b[top1]=b[top1+1];
top1++;
}
Data Structures using C
}
{
}
for(top=0;top<5;top++)
printf("\n\t \t %d ",a[top]);
printf("\t \t %d ",b[top]);
}
getch();
}
OUTPUT:
Analysis of Stacks
Below mentioned are the time complexities for various operations that can be performed on the Stack data
structure.
3. Queues
Queue is also an abstract data type or a linear data structure, in which the first element is
inserted from one end called REAR(also called tail), and the deletion of exisiting element
takes place from the other end called as FRONT(also called head). This makes queue as
FIFO data structure, which means that element inserted first will also be removed first.
The process to add an element into queue is called Enqueue and the process of removal
of an element from queue is called Dequeue.
Applications of Queue
Queue, as the name suggests is used whenever we need to have any group of objects in an order
in which the first one coming in, also gets out first while the others wait for there turn, like in the
following scenarios :
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as
they arrive, First come first served.
i)
{
printf("Select option\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(q.r>MAX-1)
{
printf("\nQueue overflow");
break;
}
else
{
printf("Enter Element\n");
scanf("%d",&n);
q.x[++q.r]=n;
printf("Element Inserted\n");
break;
}
case 2:
if(q.f>q.r)
printf("Queue is empty\n");
else
{
printf("\n\nYou are deleting %d",q.x[q.f]);
q.f++;
}
break;
case 3:
printf("\nQueue\n");
printf("||\t");
for(i=q.f;i<=q.r;i++)
printf("%d\t",q.x[i]);
printf("||");
break;
case 4:
break;
}
}while(choice!=4);
}
Data Structures using C
Output
ii)
void insert();
void delet();
void display();
printf("Circular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid option\n");
} } }
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
} } }
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit(0);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit(0);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();
}
Output
iii)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
char data;
struct list *next;
};
typedef struct list node;
void qinsert(node**,node**);
void disp(node*);
void main()
{
int opt;
char ch;
node *front;
node *rear;
clrscr();
rear=front=NULL; //initially front and rear are assigned NULL
do
{ printf("\nEnter your option to perform Queue operation:::\n");
printf("1. Insert\n");
printf("2. Display the Queue\n");
scanf("%d",&opt);
switch(opt)
{
case 1: qinsert(&front,&rear);
break;
case 2: disp(front);
break;
}
printf("\nDo you wish to continue[y/n]:");
ch=(char)getche();
}while(ch=='Y' || ch=='y');
printf("\nPress any key to exit from the program");
getch();
}
void qinsert(node **front,node **rear)
{
node *new_element; /* New element to be inserted */
new_element=(node*)malloc(sizeof(node)); //memory allocated
new_element->next=NULL;
printf("\nEnter the character to be inserted in the queue:");
fflush(stdin);
scanf("%c",&(new_element->data));
if(*front==NULL && *rear==NULL)
{
*front=new_element;
*rear=new_element;
}
else
{
(*rear)->next=new_element;
*rear=new_element;
}
}
void disp(node *f) //Displaying the queue
{
printf("\nElements in the Queue are:");
while(f!=NULL)
{
printf(" %c ",f->data);
f=f->next;
}
}
OUTPUT:
Analysis of Queue
Enqueue : O(1)
Dequeue : O(1)
Size : O(1)
4. LINKED LIST
Introduction to Linked Lists
Linked List is a linear data structure and it is very common data structure which consists of
group of nodes in a sequence which is divided in two parts. Each node consists of its own data
and the address of the next node and forms a chain. Linked Lists are used to create trees and
graphs.
They are a dynamic in nature which allocates the memory when required.
Linked lists let you insert elements at the beginning and end of the list.
Singly Linked List : Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in sequence of nodes. The operations we
can perform on singly linked lists are insertion, deletion and traversal.
Doubly Linked List : In a doubly linked list, each node contains two links the first link
points to the previous node and the next link points to the next node in the sequence.
Circular Linked List : In the circular linked list the last node of the list contains the address
of the first node and forms a circular chain.
i)
POINTERS:
Write a program using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*c,*d;
printf("Enter value of a and b\n");
scanf("%d%d",&a,&b);
c=&a;
d=&b;
*c=*c+*d;
printf("Sum is %d",*c);
getch();
}
Output
ii)
Single linked list:
a) Insertion Operation
Algorithm:
printf(NULL);
}
void main()
{
creat();
display();
}
Output
b) Deletion Operation:
void delete_front();
void delete_end();
void delete_any();
void display();
void main()
{
int choice;
int cont = 1;
//Allocate memory for header node.
header = (struct node *) malloc(sizeof(struct node));
clrscr();
//Set the content of header node
header->data = NULL;
header->link = NULL;
while(cont == 1)
{
//Display menu to the user
printf("\n1. Insert at end\n");
printf("\n2. Delete from front\n");
printf("\n3. Delete from end\n");
printf("\n4. Delete from anywhere\n");
printf("\n5. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert_end();
break;
case 2:
delete_front();
break;
case 3:
delete_end();
break;
case 4:
delete_any();
break;
case 5:
display();
break;
}
printf("\n\nDo you want to continue? (1 / 0): ");
scanf("%d", &cont);
}
getch();
}
//Function to insert a node at the end of a single linked list.
void insert_end()
{
int data_value;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));
//Traverse to the end of the linked list.
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
//Function to delete a node from the front of a linked list.
void delete_front()
{
//If the list is already empty
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not possible.\n");
}
else
{
ptr = header->link;
header->link= ptr->link;
free(ptr);
printf("\nNode deleted from the front.\n");
}
}
//Function to delete a node from the end of a linked list.
void delete_end()
{
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not possible.\n");
}
else
{
//Traverse to the end of the list.
ptr = header;
while(ptr->link != NULL)
{
ptr1 = ptr;
ptr = ptr->link;
}
ptr1->link = ptr->link;
free(ptr);
printf("\nNode deleted from the end.\n");
}
}
//Function to delete any node from linked list.
void delete_any()
{
int key;
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not possible.\n");
}
else
{
printf("\nEnter the data of the node to be deleted: ");
scanf("%d", &key);
ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf("\nNode with data %d deleted.\n", key);
}
else
{
printf("\nValue %d not found. Deletion not possible.\n", key);
}
}
}
//Function to display the contents of the linked list.
void display()
{
printf("\nContents of the linked list are: \n");
//Print the contents of the linked list starting from header
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ", ptr->data);
}
}
OUTPUT: