Vous êtes sur la page 1sur 46

DATA

STRUCTURES LAB



Submitted BY-
SHIVAM SIDDHARTH
2K13/CO/123


INDEX

Sr No. Program Name Date Signature



























Program #1: Array implementation of Stack
CODE:
#include<stdio.h>
struct q
{
int data[100];
int top;
}a;

void display()
{
int i;
if (a.top==-1)
printf("\nStack Is Empty!");
else
for (i = a.top; i >= 0; i--)
printf("\n%d", a.data[i]);

}
void pop()
{
int x;
if(a.top==-1)
printf("Stack Underflow");

else
x=a.data[a.top];
printf("Element popped is:%d", x);
(a.top)--;
}

void push()
{
int x;
if(a.top>=99)
printf("stack overflow");
else
printf("Enter element to Push\n");
scanf("%d", &x);
a.data[++(a.top)]=x;
}

int main()
{

char yes;
int ch;
a.top=-1;
do
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push(a);
break;
case 2: pop(a);
break;
case 3: display();
break;
case 4: exit(0);
default: printf("ERROR: invalid choice");
break;
}
printf("\nDo you wanna continue y/n \n");
yes=getche();
}while(yes=='y' || yes=='Y');


return 0;
}









Program #2: Stack using Linked List
CODE:
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create empty stack */
void create()
{
top = NULL;
}

/* Count stack elements */
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}

/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

int topelement()
{
return(top->info);
}

void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy()
{
top1 = top;

while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");
count = 0;
}












Program #3: Array implementation of Queue
CODE:
#include<stdio.h>
struct queue
{
int data[100];
int front, rear;
};

void rem(struct queue *pq)
{
int x, i;
if((pq->rear)<(pq->front))
{
printf("Queue Underflow\n");
}
else
{
x=pq->data[pq->front];
(pq->front)++;
printf("Element removed: %d", x);
for(i=0; i<(pq->rear); i--)
pq->data[i]=pq->data[i+1];
}
}

void add(struct queue *pq)
{
int x;
if(pq->rear>=99)
printf("Queue Overflow\n");
printf("Enter element to be added\n");
scanf("%d", &x);
pq->data[++(pq->rear)]=x;
}

void display(struct queue *pq)
{
int i;
if((pq->rear)<(pq->front))
printf("Queue Empty!!\n");
for(i=pq->front; i<=(pq->rear); i++)
printf("%d\t", pq->data[i]);

}

int main()
{
int ch;
char yes;
struct queue q;
q.front=0;
q.rear=-1;
do
{
printf("\n1.ADD\n2.REMOVE\n3.DISPLAY\n4.EXIT\n");
scanf("%d", &ch);
switch(ch)
{
case 1: add(&q);
break;
case 2: rem(&q);
break;
case 3: display(&q);
break;
case 4: exit(0);
default:printf("ERROR");
break;
}
printf("\nDo you want to continue Y/N \n");
yes=getche();
}while(yes=='y' || yes=='Y');
return 0;
}






PROGRAM #4: LINKED LIST IMPLEMENTATION OF QUEUE
CODE:
#include <stdlib.h>
#include <stdio.h>
struct QNode
{
int key;
struct QNode *next;
};
struct Queue
{
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

// The function to add a key k to q
void enQueue(struct Queue *q, int k)
{
struct QNode *temp = newNode(k);
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
struct QNode *deQueue(struct Queue *q)
{.
if (q->front == NULL)
return NULL;
struct QNode *temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
return temp;
}
int main()
{
struct Queue *q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
struct QNode *n = deQueue(q);
if (n != NULL)
printf("Dequeued item is %d", n->key);
return 0;
}















Program #5: Implementation of Heap
CODE:
#include <stdio.h>
int array[100], n;
main()
{
int choice, num;
n = 0;
while(1)
{
printf("1.Insert the element \n");
printf("2.Delete the element \n");
printf("3.Display all elements \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}
}
}
void display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}

void insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return;
}
array[location] = array[parentnode];
location = parentnode;
}
array[0] = num;
}
void delete(int num)
{
int left, right, i, temp, parentnode;

for (i = 0; i < num; i++) {
if (num == array[i])
break;
}
if (num != array[i])
{
printf("%d not found in heap list\n", num);
return;
}
array[i] = array[n - 1];
n = n - 1;
parentnode =(i - 1) / 2; /*find parentnode of node i */
if (array[i] > array[parentnode])
{
insert(array[i], i);
return;
}
left = 2 * i + 1; /*left child of i*/
right = 2 * i + 2; /* right child of i*/
while (right < n)
{
if (array[i] >= array[left] && array[i] >= array[right])
return;
if (array[right] <= array[left])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
i = left;
}
else
{
temp = array[i];
array[i] = array[right];
array[right] = temp;
i = right;
}
left = 2 * i + 1;
right = 2 * i + 2;
}
if (left == n - 1 && array[i]) {
temp = array[i];
array[i] = array[left];
array[left] = temp;
}
}















Program #6: Implementation of a program that demonstrates Recursion
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};

void printReverse(struct node* head)
{
if(head == NULL)
return;
printReverse(head->next);
printf("%d ", head->data);
}

void push(struct node** head_ref, char new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{

struct node* head = NULL;

push(&head, 1);
push(&head, 2);
push(&head, 3);
push(&head, 4);

printReverse(head);
getchar();
}













Program #7: Implementation of Heap Sort
CODE:
#include <stdio.h>
void main()
{
int heap[10], no, i, j, c, root, temp;

printf("\n Enter no of elements :");
scanf("%d", &no);
printf("\n Enter the nos : ");
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
for (i = 1; i < no; i++)
{
c = i;
do
{
root = (c - 1) / 2;
if (heap[root] < heap[c])
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}
printf("Heap array : ");
for (i = 0; i < no; i++)
printf("%d\t ", heap[i]);
for (j = no - 1; j >= 0; j--)
{
temp = heap[0];
heap[0] = heap[j];
heap[j] = temp;
root = 0;
do
{
c = 2 * root + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j)
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
printf ("\n The sorted array is : ");
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
}
Program #8: Implementation of Binary Search
CODE:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");
scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);

printf("Enter value to find\n");
scanf("%d",&search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);

return 0;
}











Program #9: Implentation of Linear Search
CODE:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

Program #10: Implementation of Recursive Methods for Inorder Traversal of a
Tree
CODE:
# include <iostream>
# include <cstdlib>
using namespace std;

struct node
{
int info;
struct node *left;
struct node *right;
}*root;

class BST
{
public:
void insert(node *, node *);
void inorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};

int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Inorder Traversal"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 3:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 4:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}


void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}

void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}


void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}









Program #11: Implementation of Recursive Methods for Preorder Traversal of
a Tree
CODE:
# include <iostream>
# include <cstdlib>
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*root;

class BST
{
public:
void insert(node *, node *);
void preorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};

int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Preorder Traversal"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 3:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 4:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}


void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}

void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}


void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level; i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}











Program #12: Implementation of Insertion Sort
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{ int array[100],n,i,j,temp,k;
printf("\nHow many elements you want to enter => ");
scanf("%d",&n);
printf("\nEnter elements one by one => ");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=1;i<n;i++)
{ for(j=0;j<i;j++)
{if(array[j]>array[i])
{ temp=array[i];
for(k=i;k>j;k--)
{ array[k]=array[k-1];
}
array[j]=temp;
}
}
}
printf("\nThe insertion sorted array => ");
for(i=0;i<n;i++)
printf("%d - ",array[i]);
getch();
}
Program #13: Implementation of Quick Sort
CODE:
#include<stdio.h>
int a[10],n;
void quick_sort(int[10],int,int);
int main()
{
int k;
printf("Enter the size of array\n"); // input the elements
scanf("%d",&n);
printf("\nEnter the elements:");
for(k=0; k<n; k++)
scanf("%d",&a[k]);
quick_sort(a,0,n-1); // sort the array
printf("\nSorted array:"); // print sorted array
for(k=0; k<n; k++)
printf("\n%d",a[k]);
return 0;
}

void quick_sort(int a[10],int first,int last)
{
int pivot,t,temp,i,k,j;
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i<=last)
{
i=i+1;
}
while(a[j]>pivot)
{
j=j-1;
}

if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
t=a[j];
a[j]=pivot;
pivot=t;
a[first]=pivot;
quick_sort(a,first,j-1);
quick_sort(a,j+1,last);
}
}

Vous aimerez peut-être aussi