Notes CP
Notes CP
Notes CP
/*****************************************************************************
int main()
{
int n;
printf("Enter number of node\n\n");
scanf("%d", &n);
createnode(n);
printf("Inserting a new node with value 40 at the begning of list\n\n");
inseratbegning();
printf("Inserting a new node with value 70 at the end of list\n\n");
inseratend();
printf("\nTraversing the created list\n");
traverselist();
printf("Inserting a new node with value 90 after node 2 in the linked list\n\n");
inseratanyindex();
printf("After inserting current list is as under\n\n ");
traverselist();
printf("Deleteing firt node from list\n");
deletnodefrombegning();
printf("After deleteing current list values are\n\n ");
traverselist();
printf("----------------------------------Delete node from last--------------------\n");
deletelastnode();
printf("After deleteing last node the current list values are\n\n ");
traverselist();
printf("----------------------------------Delete node WITH VALUE =3 IN THE LIST-------
-------------\n");
deleteatanyindex();
printf("After deleteing node WITH VALUE =3; the current list values are\n\n
");
traverselist();
printf("Move last node to front of node\n\n ");
movelastnodetofront();
printf("----------------------AfterMoving last node to front the list is --------------\n\n
");
traverselist();
printf("----------------------An Iterative verison to reverse data in linked list ----------
----\n\n ");
//*Reverseiterative function will return a pointer and takes a pointer
head= (*Reverseiterative)(head);
traverselist();
int data ;
struct node *temp;
/* lets create first node using malloc */
head=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Memory not allocated ");
return;
}
printf("Enter first node data ");
scanf("%d", &data);
temp=head;
head-> data = data;
head->link=NULL;
/*****
traverselist function
*****/
void traverselist()
{
struct node *temp=head;
int i=1;
while(temp)
{
printf("Printing %d Element of Linked List %d\n",i,temp->data);
i++;
temp= temp->link;
}
}
/*****
inseratbegning function
*****/
void inseratbegning()
{
printf("\nLets insert a node at the begninig\n");
struct node *mynode;
mynode=(struct node *)malloc(sizeof(struct node));
mynode->link=head;
mynode->data=40;
head=mynode;
printf("New Node Inserted Successfuly at the begning!\n");
}
/*****
inseratEnd function
*****/
void inseratend()
{
printf("\nLets insert a node at the End\n");
struct node *mynode;
struct node *temp=head;
mynode=(struct node *)malloc(sizeof(struct node));
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=mynode;
mynode->link=NULL;
mynode->data=70;
printf("New Node Inserted Successfuly at the End!\n");
}
/*****
inseratanyindex function
*****/
void inseratanyindex()
{
printf("\nLets insert a node at the End\n");
struct node *mynode;
struct node *temp=head;
mynode=(struct node *)malloc(sizeof(struct node));
while(temp->data!=2)
{
temp=temp->link;
}
mynode->link=temp->link;
temp->link=mynode;
mynode->data=90;
printf("New Node Inserted Successfuly at the after node 2!\n");
}
/*****
deletnodefrombegning()
*****/
deletnodefrombegning()
{
struct node *temp=head;
if(head== NULL)
{
printf("THERE IS NO NODE TO DELETE\n");
}
if(head->link==NULL) // there is only one node
{
free(head);
head=NULL;
}
head=head->link;
free(temp);
}
/*****
deletelastnode()
*****/
deletelastnode()
{
struct node *temp=head;
while(temp->link->link!=NULL)
{
temp=temp->link;
}
temp->link->link=NULL;
free(temp->link);
temp->link=NULL;
}
/*****
deleteatanyindex()
*****/
deleteatanyindex()
{
struct node *temp=head;
while(temp->link->data!=3)
{
temp=temp->link;
}
struct node *temp1=temp->link;
temp->link=temp1->link->link;
free(temp1);
temp1= NULL;
}
/*****
Moving Last node to the front of linked list
movelastnodetofront()
*****/
// Lets have two pointer (p,q) one will be one step ahead of other;
// the moment one pointer (p) reaches last node ; point its link to head,
// and put null into q ;
movelastnodetofront()
{
q=p;
p=p->link;
}
q->link= NULL;
p->link=head;
head=p;
}
/*****
Reverseing content of linked list using Iterative version
Reverseiterative() is a function pointer which returns a pointer to a structure
and it takes head as its initial pointer as parameter
*****/
struct node *Reverseiterative(struct node *curr)
{
/*****
Reverseing content of linked list using Recursion
ReverseusingRecursion() is a function which returns a pointer to a structure
and it takes NULL in prev node and head in curr.
*****/
void ReverseusingRecursion(struct node *prev, struct node *curr)
{
if(curr)
{
ReverseusingRecursion(curr,curr->link);
curr->link=prev;
}
else
head=prev;
}
if(head == NULL)
{
printf("Memory not allocated ");
return;
}
printf("Enter first node data ");
scanf("%d", &x);
temp=head;
temp->prev=NULL;
temp->next=NULL;
temp->data = x;
int k;
for(int i=2; i<=n; i++)
{
/*****
traverselist function
*****/
void traverselist()
{
struct node *temp;
temp=head;
// printf("Value in temp is %d and temp points to %d", temp,temp->data);
int i=1;
while(temp!=NULL)
{
printf("Printing %d Element of Linked List - %d\n",i,temp->data);
i++;
temp=temp->next;
}
return;
}
As you know that a program is supposed to take some input and is expected to give some
output ! The input and the output to a program is given in some format, that format is what we
called as data structure!
You can think if we give a unsorted array to a sorting program than the time taken to sort the
elements is more as compared to a sorted input array ! this means that the format of input data
plays an important role as far as far as performance of an algorithm is considered. !
In this respect the first and traditional way of giving the data was using - STACK.
A stack is a data structure ( i.e., a type of input format) which follows LIFO property, that is the
last element is popped out first from the top of the stack.
The main application of stacks are in
1. Recursion :- Remember how recursive calls for a function is made by creating a stack
activation record
3. Brower's use stacks for remembering which page is visited and then going back( i,e, POP )
In this article we will study how stack push and pop operation.
Data Structure- 2. Stacks Push and POP Operation
As you know that in stack we always perform push and pop f an element from
the top of the stack , here is the code to do the same
/******************************************************************************
Push and Pop Operation in Stack !
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#include <stdio.h>
int top=-1; // Delaring a Top variable initially pointing to -1
int stack [10]; // a stack array of 10 elements
int main()
{
return 0;
}
Data Structure- 3. Stacks Push and POP Operation using
Linked List
As you know
that in a stack the push and pop operation always done from the top, so if you imagine each
index of stack a linked list than the push operation is nothing more than inserting a node at the
front of list and pop operation is nothing but deletion a node from the front of list ( i.e., TOP)
here is the code for you ! Note that we have used power of recursion here to push the
elements in the stack !
/******************************************************************************
Stacks Push and POP Operation using Linked List
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#include <stdio.h>
/* Lets create structure of the node*/
struct node
{
int data;
struct node *link;
if(temp == NULL)
{
printf("Memory not allocated ");
return;
int main()
{
int n, item;
printf("Enter number of elemens to be pushed in stack\n\n");
scanf("%d", &n);
printf("Enter the item \n\n");
scanf("%d", &item);
struct node *temp1=createnode(n,item); / holding returned value of head
which is a pointer
for(int i=0;i<n;i++)
{
head= temp1;
printf("Top %d in the stack is %d\n", i, temp1->data);
temp1= temp1->link;
}
return 0;
}
// The deletion of an element from the stack using concept of linked list is left as an
exercise for you
Concept to code !- We maintain two variables called front and Rear, The Front will always point to the
first element in the queue and the Rear will element will always point to the current element that is just
inserted into the queue.
If you will follow this than lets suppose we are given the following list of element
Thus here is the Code to push and pop an element from the queue !
/******************************************************************************
Queue Concept using an Array !
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
/*
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
implement the queue concept because as we can see the in linear array there is
a no way to store new item even if there is a space after deleting an element !
with circular array w will use MAX-1 index of array and initially we will put front
and rear to index zero position !
1. when we need to insert a data we just increment the rear and insert
the data !
2. When we need to delete the data we just increase the front and print
the data!
Only important point is that when during insertion if rear== front then it means the queue is
full now and we will put rear one position back , if rear =0 then one position back is MAX-1; and
if rear != 0 then one position back is rear-1;
/******************************************************************************
Queue Concept using Circular Array !
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#define MAX 4
enqueue(int);
//void delete();
void display();
int queue_array[MAX];
int rear = 0;
int front = 0;
enqueue(int item)
{
//int item;
rear=(rear+1)%MAX;
if (front==rear)
{
printf("Queue Overflow \n");
if(rear==0)
{
rear=MAX-1;
}
else {
rear=rear-1;}
return;
}
else
{
queue_array[rear]= item;
printf("iTEM INSERTED is %d\n",queue_array[rear]);
return;
}
} /* End of Enqueue() */
deqeue()
{
if(front==rear)
{
printf("Queue is empty on");
return -1;
}
else
{
front=(front+1)%MAX;
int item= queue_array[front];
printf("Deleted Item is %d\n",item);
return item;
}
} /* End of deqeue() */
main()
{
// Here we try to insert 3 item using a loop
// initially front and rear are point to zero and we will utilize only max-1 index of
circular array
for (int i = front; i <= MAX-1; i++){
enqueue(i);
}
#1.Trees basics
Tree
In this unit Let me briefly tell you that why we are studying this ADT
As you know that Searching an element using in a linked list takes ~0(n) time. How?
Lets assume that you wish to search an element named "A" in a given linked list ( Obviously LL
is a non linear data Structure) which contains elements A,B,D,E . What will be the worst case (
This is when The Element is placed at the last in the linked list) so how much time it will take to
search this A now, Obviously you have to traverse each element and then you will be able to
find A ., How many elements are there in the list ? 4 here , so if elements are n then how much
time you will take in worst case = ~0(n). Fine!
Now if we can reduce this time to some Log time Don't you think it is more wonderful ?
How, Lets use the concept of one more non linear ADT called Trees. A tree is a non
linear structure in which each node is connected with some other node (forming parent child
relationship) to form a hierarchical structure. the point is to reduce the searching time !
But even before that lets try to understand different types of trees with their terminologies !
1. We have discussed linear data structures, such as, Arrays, Strings, Stacks, and
Queues.
2. Now, we will learn about a Non-Linear Data Structure called TREE.
3. A tree is a structure which is mainly used to store data that is hierarchical in
nature. First, we will understand general trees and then Binary Trees.
4. These binary trees are used to form binary search trees and heaps. They are
widely used to manipulate Arithmetic Expressions, Construct Symbol Tables, and for
Syntax Analysis.
Now Hope you understood The definition ! Please keep in mind that some
books follow slightly different convention while defining the types of trees
Now lets try to explore why the complexity turns out to be log(n) in
searching for an element.
But ! wait what if the tree is skewed !
So, What will be the advantage of using trees if its complexity turns out to be ~0(n); Yes you are
right their is no advantage if the tree is skewed but if it is complete or almost complete than
you can take log(n) time advantage over ~o(n). Ok!
Hope You are now clear about different terminologies used in Trees ! and the reason we are
using this ADT
But wait I know that you reduced the time by using Trees but it is on the cost of inserting and
deletion ! in Trees both operation involves traversing a node and then deletion/insertion. So in
wort case A node is present at last and thus you need to traverse all the nodes in order to reach
that node.
If visiting a node takes C amount then to visit n nodes will take ~=Cx o(n) =O()n
Conclusion - Trees reduces searching (log (n) , in linked list = O(n)) nut insertion and deletion
time increases that is = O(n) which is O(1) in case of lionked list.
2#Trees Traversal
In this Unit we will Learn about Trees ADT and how to Implement them
Why to use Tree ADT. Theoretical understanding regarding its time has already been
seen why we are using trees! Remember the time complexity in searching log (n) over
the cost of insertion and deletion (o(n) ) . Have you observed in your computer how files
are organised in a tree structure
Fine !
See there are two scenarios - 1) if you are given some nodes without label and 2) with
labels
A binary tree is one which is having degree of at most 2 , thus node with degree 0,1,2 are
binary
3_Tree Traversals Lab
/******************************************************************************
Trees Concepts ! Inoder Traversal
@Mukesh Mann dated 23-03-2020
*******************************************************************************/
/******************************************************************************
*******************************************************************************/
#include <stdio.h>
int data;
}Node;
int * create()
int x;
Node *newnode=(Node*)malloc(sizeof(Node));
if(x==-1)
return NULL;
newnode->data=x;
newnode->left=create();
newnode->right=create();
return newnode;
if(!t==NULL)
{
printf("Left child data is %d ",t->data);
traverse(t->left);
t=t->right;
if(!t==NULL)
traverse(t->right);
//t=root;
if(t)
{
Inorder(t->left);
printf(" %d",t->data);
Inorder(t->right);
int main()
Node *root=create();
Node *t=root;
traverse(t);
Inorder(t);
return 0;