Professional Documents
Culture Documents
Experiment No. 1
Program :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct info
{
char name[30];
int eno;
struct info *next;
};
struct info *head=NULL,*temp,*disp;
void addrecord();
void deleterecord();
void disrecord();
void main()
{
int ch;
clrscr();
while (1)
{
1
Data Structure lab manual
2
Data Structure lab manual
}
printf("\n Would you like to enter another name(y\\n): \n");
ans = getchar();
fflush(stdin);
}
} void deleterecord()
{
struct info *delete;
int teno, present=0;
if (head==NULL)
{
printf("\n No records to delete\n");
return;
}
printf("\n Enter the enrollment number to be deleted \n");
scanf("%d",&teno);
fflush(stdin);
for (delete=head;delete!=NULL;delete=delete->next)
{
if (delete->eno==teno)
{
if (head->eno==teno)
{
delete=head;
head=head->next;
free(delete);
return;
}
else
{
temp->next=delete->next;
free(delete);
return;
}
}
temp=delete;
}
if (present==0)
printf("\nNo such enrollment number present\n");
}
void disrecord()
{
if (head==NULL)
3
Data Structure lab manual
{
printf("\n No records to view\n");
return;
}
for (disp=head;disp!=NULL;disp=disp->next)
{
printf("\n\n Name : %s",disp->name);
printf("\n\n Number : %d",disp->eno);
}
}
4
Data Structure lab manual
Experiment No.2
The disadvantage is that more pointers needs to be handled and more links need to updated.
Program :
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void insert(node *pointer, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int find(node *pointer, int key)
{
pointer = pointer -> next; //First node is dummy node.
/* Iterate through the entire linked list and search for the key. */
while(pointer!=NULL)
5
Data Structure lab manual
{
if(pointer->data == key) //key is found.
{
return 1;
}
pointer = pointer -> next;//Search in the next node.
}
/*Key is not found */
return 0;
}
void delete(node *pointer, int data)
{
/* Go to the node for which the node next to it has to be deleted */
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
/* Now pointer points to a node and the node next to it has to be removed */
node *temp;
temp = pointer -> next;
/*temp points to the node which has to be removed*/
pointer->next = temp->next;
temp->prev = pointer;
/*We removed the node which is next to the pointer (which is also temp) */
free(temp);
/* Beacuse we deleted the node, we no longer require the memory used for it .
free() will deallocate the memory.
*/
return;
}
void print(node *pointer)
{
if(pointer==NULL)
{
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
6
Data Structure lab manual
7
Data Structure lab manual
else
{
printf("Element Not Found\n");
}
}
}
8
Data Structure lab manual
Experiment No.3
One way to think about this implementation is to think of functions as being stacked on top of
each other; the last one added to the stack is the first one taken off. In this way, the data structure
itself
enforces the proper order of calls.
Conceptually, a stack is simple: a 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. Consequently, a stack is said
to have "first in last out" behavior (or "last in, first out"). The first item added to a stack will be
the last item removed from a stack.
So what's the big deal? Where do stacks come into play? As you've already seen, stacks are a
useful way to organize our thoughts about how functions are called. In fact, the "call stack" is the
term used for the list of functions either executing or waiting for other functions to return.
In a sense, stacks are part of the fundamental language of computer science. When you want to
express an idea of the "first in last out" variety, it just makes sense to talk about it using the
common terminology. Moreover, such operations show up an awful lot, from theoretical
computer science tools such as a push-down automaton to AI, including implementations of
depth-first search.
9
Data Structure lab manual
10
Data Structure lab manual
Program :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("\nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]);
}
}
11
Data Structure lab manual
int main() {
int item, choice;
char ans;
st.top = -1;
printf("\n\tImplementation Of Stack");
do {
printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("\nEmpty stack!Underflow !!");
else {
item = pop();
printf("\nThe popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf("\nDo You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');
return 0;
}
12
Data Structure lab manual
Experiment No.4
Theory :
Average case complexity of Search, Insert, and Delete Operations is O(log n), where n is
the number of nodes in the tree.
DEF: A binary tree in which the nodes are labeled with elements of an ordered dynamic
set and the following BST property is satisfied: all elements stored in the left subtree of
any node x are less than the element stored at x and all elements stored in the right
subtree of x are greater than the element at x.
An Example: Figure shows a binary search tree. Notice that this tree is obtained by
inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting
from an empty tree.
Note that inorder traversal of a binary search tree always gives a sorted sequence of the
values. This is a direct consequence of the BST property. This provides a way of sorting a
given sequence of keys: first, create a BST with these keys and then do an inorder
traversal of the BST so created.
Note that the highest valued element in a BST can be found by traversing from the root in
the right direction all along until a node with no right link is found (we can call that the
rightmost element in the BST).
The lowest valued element in a BST can be found by traversing from the root in the left
direction all along until a node with no left link is found (we can call that the leftmost
element in the BST).
Search is straightforward in a BST. Start with the root and keep moving left or right
using the BST property. If the key we are seeking is present, this search procedure will
lead us to the key. If the key is not present, we end up in a null link.
Insertion in a BST is also a straightforward operation. If we need to insert an element x,
we first search for x. If x is present, there is nothing to do. If x is not present, then our
search procedure ends in a null link. It is at this position of this null link that x will be
included.
If we repeatedly insert a sorted sequence of values to form a BST, we obtain a completely
skewed BST. The height of such a tree is n - 1 if the tree has n nodes. Thus, the worst
case complexity of searching or inserting an element into a BST having n nodes is O(n).
13
Data Structure lab manual
Program :
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
14
Data Structure lab manual
};
typedef struct bin_tree node;
15
Data Structure lab manual
void main()
{
node *root;
node *tmp;
16
Data Structure lab manual
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
17
Data Structure lab manual
Experiment No.5
Graphs are used to represent many real life applications: Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network. Graphs are
also used in social networks like linkedIn, facebook. For example, in facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person
id, name, gender and locale. This can be easily viewed by
http://graph.facebook.com/barnwal.aashish where barnwal.aashish is the profile name. See this
for more applications of graph.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to
represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with
weight w.
18
Data Structure lab manual
Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time.
Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done
O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges),
it consumes the same space. Adding a vertex is O(V^2) time.
AdjacencyList:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be
array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be
stored in nodes of linked lists. Following is adjacency list representation of the above graph.
19
Data Structure lab manual
Program :
void main()
{
int option;
clrscr();
do
{ printf("\n A Program to represent a Graph by using an ");
printf("Adjacency Matrix method \n ");
printf("\n 1. Directed Graph ");
printf("\n 2. Un-Directed Graph ");
printf("\n 3. Exit ");
printf("\n\n Select a proper option : ");
scanf("%d", &option);
switch(option)
{
case 1 : dir_graph();
break;
case 2 : undir_graph();
break;
case 3 : exit(0);
} // switch
}while(1);
} // main
int dir_graph()
{
int adj_mat[50][50];
int n;
int in_deg, out_deg, i, j;
20
Data Structure lab manual
if ( adj_mat[j][i] == 1 )
in_deg++;
} // for
for ( j = 1 ; j <= n ; j++ )
if (adj_mat[i][j] == 1 )
out_deg++;
printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
} // for
return;
} // dir_graph
int undir_graph()
{
int adj_mat[50][50];
int deg, i, j, n;
read_graph(adj_mat, n);
printf("\n Vertex \t Degree ");
21
Data Structure lab manual
} // if
printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
flushall();
scanf("%c", &reply);
if ( reply == 'y' || reply == 'Y' )
adj_mat[i][j] = 1;
else
adj_mat[i][j] = 0;
} // for
} // for
return;
} // read_graph
22
Data Structure lab manual
Experiment No.6
Theory : Insertion sort iterates, consuming one input element each repetition, and growing a
sorted output list. Each iteration, insertion sort removes one element from the input data, finds
the location it belongs within the sorted list, and inserts it there. It repeats until no input elements
remain.
Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At
each array-position, it checks the value there against the largest value in the sorted list (which
happens to be next to it, in the previous array-position checked). If larger, it leaves the element in
place and moves to the next. If smaller, it finds the correct position within the sorted list, shifts
all the larger values up to make a space, and inserts into that correct position.
The most common variant of insertion sort, which operates on arrays, can be described as
follows:
1. Suppose there exists a function called Insert designed to insert a value into a sorted
sequence at the beginning of an array. It operates by beginning at the end of the sequence
and shifting each element one place to the right until a suitable position is found for the
new element. The function has the side effect of overwriting the value stored immediately
after the sorted sequence in the array.
2. To perform an insertion sort, begin at the left-most element of the array and invoke Insert
to insert each element encountered into its correct position. The ordered sequence into
which the element is inserted is stored at the beginning of the array in the set of indices
already examined. Each insertion overwrites a single value: the value being inserted.
Program :
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
23
Data Structure lab manual
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
return 0;
}
24