Data Staructure Assignment
Data Staructure Assignment
ASSIGNMENT
ACKNOWLEDGEMENT
Every project big or small is successful largely due to the effort
of a number of wonderful people who have always given their
valuable advice or lent a helping hand. I sincerely
appreciate the inspiration; support and guidance of all those
people who have been instrumental in making this project a
success.
I, Your Name, the student of GGSIPU (BTECH-CSE), am
extremely grateful to COMPANY for the confidence
bestowed in me and entrusting my project entitled
At this juncture I feel deeply honored in expressing my
sincere thanks to ANURAG
AGGRAWAL for
making
the resources available at right time and providing valuable
insights leading to the successful completion of my project.
I express my gratitude to College for arranging the summer
training in good schedule. I also extend my gratitude to my
Project Guide who assisted me in compiling the project.
I would also like to thank all the faculty members of GGSIPU for
their critical advice and guidance without which this project
would not have been possible.
Last but not the least I place a deep sense of gratitude to my
family members and my friends who have been constant
source of inspiration during the preparation of this project work.
INDEX
S.no
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
TITLE
Program to implement a stack using array
Program to implement a singly linked list
Program to create doubly linked list
Program to add two big numbers
Program to create binary search tree
Program to implement mini heap
Program to implement merge sort
Program to implement insertion sort
Program to implement quick sort
Program to implement Breadth first
search for graphs
Program to implement Depth first search
for graphs
case 1:display();
break;
case 2:push();
break;
case 3:pop();
break;
case 4:exit(0);
default:printf("wrong choice");
}
getch();
}while(choice!=4);
} void display()
{ if(top==-1)
printf("empty!!!");
else
{
for(i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
}
void push()
{ if(top==max-1)
printf("overflow!!!!!");
else
{
}
}
void pop()
{ if(top==-1)
printf("underflow!!!!!");
else
{
item=stack[top];
top--;
printf("the popped number is:%d",item);
}
}
int info;
struct node *ptr;
}*temp,*top1;
struct node *top=NULL;
void insert();
void del();
void display();
int i,choice,data;
void main()
{ clrscr();
do
{ printf("\n\t\tMENU");
printf("\n1.DISPLAY");
printf("\n2.insert");
printf("\n3.remove");
printf("\n4.exit");
printf("\nchoice is:");
scanf("%d",&choice);
switch(choice)
{
case 1:display();
break;
case 2:insert();
break;
case 3:del();
break;
case 4:exit(0);
default:printf("wrong choice");
}
getch();
}while(choice!=4);
}
void insert()
{
}
else
{
}
}
void display()
{
top1=top;
if(top1==NULL)
{
printf("empty!!!");
}
while(top1!=NULL)
{
printf("%d",top1->info);
top1=top1->ptr;
}}
void del()
{
if(top==NULL)
printf("underflow!!!!!");
}
else
{
top1=top;
top1=top1->ptr;
printf("popped value is:%d",top->info);
free(top);
top=top1;
}
}
}*head,*rear,*temp,*flag,*head1,*rear1,*temp1;
int value;
void insert_beg()
{
}
else
{
}
}
void insert_end()
{
head->data=value;
head->next=NULL;
head->previous=NULL;
}
else
{
}
}
void del_beg()
{
printf("underflow!!!!");
}
else
{
head1=head;
head1=head1->next;
printf("the value deleted is:%d",head->data);
free(head);
head=head1;
}
}
void del_end()
{
printf("underflow!!!!!");
}
else
{
rear1=rear;
rear1=rear1->previous;
temp1=head;
if(temp1==NULL)
{
printf("empty!!!!!");
}
while(temp1!=NULL)
{
printf("%d",temp1->data);
temp1=temp1->next;
}
printf("%d",rear->data);
}
void main()
{ int choice;
clrscr();
do
{ printf("\n\t\tMENU");
printf("\n1.DISPLAY");
printf("\n2.insert at beginning");
printf("\n3.insert at end");
printf("\n4.delete from beginning");
printf("\n5.delete from end");
printf("\n6.exit");
printf("\nchoice is:");
scanf("%d",&choice);
switch(choice)
{
case 1:display();
break;
case 2:insert_beg();
break;
case 3:insert_end();
break;
case 4:del_beg();
break;
case 5:del_end();
break;
case 6:exit(0);
default:printf("wrong choice");
}
getch();
}while(choice!=6);
}
clrscr();
printf("no. of digits in first number:");
scanf("%d",&n1);
printf("\nenter the number:\n");
for(i=0;i<n1;i++)
{
scanf("%d",&A1[i]);
}
printf("no. of digits in second number:");
scanf("%d",&n2);
printf("\nenter the number:\n");
for(i=0;i<n2;i++)
{
scanf("%d",&A2[i]);
}
if(n2==n1)
{
int carry=0;
for(i=n1-1;i>=0;i--)
{
j=i;
sum[j]=A1[i]+A2[i]+carry;
carry=0;
if(sum[j]>9)
{
if(j>0)
{
sum[j]=sum[j]%10;
carry=1;
}
}
}
printf("%d",sum[j]);
}
getch();
}
if(n2<n1)
{
int carry=0;
for(i=n1-1;i>=0;i--)
{ if(i>=n2-1)
{ j=i-(n1-n2);
A3[i]=A2[j];
}
else
A3[i]=0;
}
for(i=n1-1;i>=0;i--)
{
j=i;
sum[j]=A1[i]+A3[i]+carry;
carry=0;
if(sum[j]>9)
{
if(j>0)
{
sum[j]=sum[j]%10;
carry=1;
}
}
}
printf("\nthe sum is:");
for(j=0;j<n1;j++)
{
}
printf("%d",sum[j]);
getch();
}
if(n2>n1)
{
int carry=0;
for(i=n2-1;i>=0;i--)
{ if(i>=n1-1)
{ j=i-(n2-n1);
A3[i]=A2[j];
}
else
A3[i]=0;
}
for(i=n2-1;i>=0;i--)
{
j=i;
sum[j]=A1[i]+A3[i]+carry;
carry=0;
if(sum[j]>9)
{
if(j>0)
{
sum[j]=sum[j]%10;
carry=1;
}
}
}
printf("\nthe sum is:");
for(j=0;j<n2;j++)
{
}
}
}
printf("%d",sum[j]);
getch();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{ create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{ int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{ if (root == NULL)
{
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{ int data;
if (root == NULL)
{
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
void search1(struct btnode *t, int data)
{ if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->value))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{ int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{ t1->l = NULL;
}
else
{ t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{ root = t->l;
t1 = root;
}
else if (t1->l == t)
{ t1->l = t->l;
}
else
{ t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{ root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{ k = smallest(t->r);
flag = 1;
}
else
{ k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}
}
int smallest(struct btnode *t)
{ t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}
int largest(struct btnode *t)
{ if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
}
return(t->value);
}
a[j/2] = temp;
return;
}
void build_minheap(int *a, int n)
{ int i;
for(i = n/2; i >= 1; i--)
{
min_heapify(a,i,n);
}
}
int main()
{ int n, i, x;
cout<<"enter no of elements of array\n";
cin>>n;
int a[20];
for (i = 1; i <= n; i++)
{
cout<<"enter element"<<(i)<<endl;
cin>>a[i];
}
build_minheap(a, n);
cout<<"Min Heap\n";
for (i = 1; i <= n; i++)
{
cout<<a[i]<<endl;
}
getch();
}
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{ int mid;
}
else
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
}
}
list[k] = temp[k];
}
}
void insertion_sort(int * x)
{ int temp, i, j;
for (i = 1;i < MAX;i++)
{ temp = x[i];
j = i - 1;
while (temp < x[j] && j >= 0)
{ x[j + 1] = x[j];
j = j - 1;
}
x[j + 1] = temp;
}
}
}
}
swap(&arr[i+1], &arr[r]);
return i + 1;
}
void quick_sort(int arr[], int p, int q)
{ int j;
if (p < q)
{
j = partion(arr, p, q);
quick_sort(arr, p, j-1);
quick_sort(arr, j+1, q);
}
}
int main()
{ int i;
int arr[MAX];
for (i = 0; i < MAX; i++)
arr[i] = i;
random_shuffle(arr); //To randomize the array
quick_sort(arr, 0, MAX-1); //function to sort the elements of array
for (i = 0; i < MAX; i++)
printf("%d \n", arr[i]);
return 0;
}
{ printf("empty queue\n");
}
else
{ p = front;
x = p->pt;
front = front->next;
delete(p);
return(x);
}
}
void bfs(int *v,int am[][7],int i)
{ if (c == 0)
{ q = new node_info;
q->no = i;
q->st_time = t++;
printf("time of visitation for node "<<q->no<<":%d",q->st_time);
v[i] = 1;
push(q);
}
c++;
for (int j = 0; j < 7; j++)
{ if (am[i][j] == 0 || (am[i][j] == 1 && v[j] == 1))
continue;
else if (am[i][j] == 1 && v[j] == 0)
{ r = new node_info;
r->no = j;
r->st_time = t++;
printf("time of visitation for node %d",r->no,":%d",r->st_time");
v[j] = 1;
push(r);
}
}
remove();
if (c <= 6 && front != NULL)
bfs(v, am, remove()->no);
}
int main()
{ int v[7], am[7][7];
for (int i = 0; i < 7; i++)
v[i] = 0;
for (int i = 0; i < 7; i++)
{ printf("enter the values for adjacency matrix row:%d",i+1);
for (int j = 0; j < 7; j++)
{
scanf("%d",&am[i][j];
}
}
bfs(v, am, 0);
getch();
}
printf("underflow\n");
else
{ q = top;
top = top->next;
return(q->pt);
delete(q);
}
}
void create(int a[], int b[][7], int i, int j)
{ c++;
p = new node;
printf("enter data for new node\n");
scanf("%d",&p->data;
p->st_time = c;
printf("start time for %d",p->data," is %d",c);
a[i] = 1;
push(p);
while (j < 7)
{
j++;
}
else if (b[i][j] == 1 && a[j] == 0)
{ create(a,b,j,0);
}
}
r = pop();
printf("node popped\n");
c++;
printf("leave time for %d",r->data," is %d",c);
return;
}
int main()
{ int a[7];
for (int i = 0; i < 7; i++)
{
a[i] = 0;
}
int b[7][7];
printf("enter values for adjacency matrix");
for (int i = 0 ; i < 7 ; i++ )
{ printf("enter values for %d",(i+1)," row");
for (int j = 0; j < 7; j++)
{
scanf("%d",&b[i][j]);
}
}
create(a,b,0,0);
getch();
}