The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by abhimoga971999, 2018-12-12 10:07:22

DS Practical (1)-converted

DS Practical (1)-converted

Stack Using Array

Input:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 10

int st[MAX], top = -1;

void push(int st[], int value);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);

void main()
{

int value, option;
do
{

printf("**MAIN MENU**");
printf("\n1.Push \n2.Pop \n3.Peek \n4.Display \n5.Exit \nEnter your Choice=");
scanf("%d", &option);
switch(option)
{

case 1: printf("\nEnter the Value to be added in th stack=");
scanf("%d",&value);
push(st, value);

break;
case 2: value=pop(st);

printf("\nValue deleted=%d\n",value);
break;
case 3: value=peek(st);

printf("Value at the top of th stack is %d\n", value);
break;
case 4: display(st);
break;
}
}while(option!=5);
getch();
}

void push(int st[], int value)
{

if(top == MAX-1)
printf("\nStack is Full.");

else
{

top+=1;
st[top] = value;
}
}

int pop(int st[])
{

int value = 0;
if(top == -1)
{

printf("\nStack is Empty.");
return value;
}
else
{
value = st[top];
top-=1;
return value;
}
}

int peek(int st[])
{

int value = 0;
if(top == -1)
{

printf("\nStack is Empty!");
return value;
}
else
{
value = st[top];
return value;
}

}

void display(int st[])
{

int i;
if(top == -1)
printf("\nStack is Empty.");
else
{

printf("\nElements are:");
for(i = top; i >= 0; i--)
{

printf("%d\t", st[i]);
}
printf("\n");
}
}

Queue Using Array

Input:
#include <stdio.h>
#include <conio.h>
#define MAX 10

int queue[MAX], front = -1, rear = -1;

void insertq(void);
int deleteq(void);
int peekq(void);
void displayq(void);

void main()
{

int ch, value;
do
{

printf("**MAIN MENU**");
printf("\n1.Insert \n2.Delete \n3.Peek \n4.Display \n5.Exit \nEnter your Choice=");
scanf("%d", &ch);
switch(ch)
{

case 1: insertq();
break;
case 2: value = deleteq();

printf("\nDeleted Value is =%d\n", value);

break;
case 3: value=peekq();

printf("\nUppermost value in the Queue is =%d \n", value);
break;
case 4: displayq();
break;
}
}while(ch!=5);
getch();
}

void insertq()
{

int x;
if(rear == MAX-1)

printf("\nQueue is Full.");
else if(front == -1 && rear == -1)
{

front = 0;
rear = 0;
printf("\nEnter a Value =");
scanf("%d", &x);
queue[rear] = x;
}
else
{
rear+=1;

printf("\nEnter a Value =");
scanf("%d", &x);
queue[rear] = x;
}
}

int deleteq()
{

int value = 0;
if(front == -1 || front > rear)
{

printf("\nQueue is Empty.");
return value;
}
else
{
value = queue[front];
front+=1;
if(front > rear)

front = rear = -1;
return value;
}
}

int peekq()
{

int value = 0;

if(front == -1 || front > rear)
{

printf("\nQueue is Empty.");
return value;
}
else
{
value = queue[front];
return value;
}
}

void displayq()
{

int i;
if(front == -1 || front > rear)

printf("\nQueue is Empty!");
else
{

for(i = front; i <= rear; i++)
{

printf("\t%d", queue[i]);
}
printf("\n");
}
}

Circular Queue

Input:
#include <stdio.h>
#include <conio.h>
#define MAX 10

int queue[MAX], front = -1, rear = -1;

void insert();
int deleteq();
void display();

void main()
{

int ch, value = 0;
do
{

printf("\n**MAIN MENU**");
printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit \nEnter your Choice="); //No Peek Option
as it a Circular Queue.
scanf("%d", &ch);
switch(ch)
{

case 1: insert();
break;
case 2: value = deleteq();

if(value == -1)

printf("Nothing Deleted\n");
else

printf("\nDeleted No.=%d\n", value);
break;
case 3: display();
break;
}
}while(ch!=4);
getch();
}

void insert()
{

int num;
if(front == 0 && rear == MAX-1)

printf("\nQueue is Overflow.");
else if(front == -1 && rear == -1)
{

front = 0;
rear = 0;
printf("\nEnter a Number=");
scanf("%d", &num);
queue[rear] = num;
}
else if(rear == MAX-1 && front != 0)
{
rear = 0;

printf("\nEnter a Number=");
scanf("%d", &num);
queue[rear] = num;
}
else if(front > rear)
printf("\nQueue is Overflow.");
else
{
rear+=1;
printf("\nEnter a Number=");
scanf("%d", &num);
queue[rear] = num;
}
}

int deleteq()
{

int value = 0;
if(front == -1 && rear == -1)
{

printf("\nQueue is Underflow");
return value;
}
else
{
value = queue[front];
if(front == rear)

rear = front = -1;
else if(front == MAX-1)

front = 0;
else

front+=1;
return value;
}
}

void display()
{

int i;
if(front == -1 && rear == -1)

printf("\nQueue is Empty");
else
{

printf("Queue = ");
if(front < rear)
{

for(i = front; i <= rear; i++)
printf("\n%d", queue[i]);

}
else
{

for(i = front; i < MAX; i++)
printf("\n%d", queue[i]);

for(i = 0; I <= rear; i++)

printf("\n%d", queue[i]);
}
printf("\n");
}
}

Infix to Postfix

Input:
#include<stdio.h>
#include<ctype.h>
#define MAX 10

char stack[MAX];
int top = -1;

void push(char x)
{

if(top == MAX-1)
printf("\nStack is Full.");

else
stack[++top] = x;

}

char pop()
{

if(top == -1)
{

printf("\nStack is Empty.");
return -1;
}
else
return stack[top--];
}

int priority(char x)
{

if(x == '(')
return 0;

if(x == '+' || x == '-')
return 1;

if(x == '*' || x == '/')
return 2;

}

void main()
{

char exp[MAX];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
printf("\nPostfix Expression=");
while(*e != '\0')
{

if(isalnum(*e))
printf("%c",*e);

else if(*e == '(')
push(*e);

else if(*e == ')')
{

while((x = pop()) != '(')
printf("%c", x);

}
else
{

while(priority(stack[top]) >= priority(*e))
printf("%c", pop());

push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}

Priority Queue

Input:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{

char job[10];
int priority;
struct node *next;
};

struct node *start = NULL;
struct node *insertq(struct node *);
struct node *deleteq(struct node *);
struct node *displayq(struct node *);

void main()
{

int option;
do
{

printf("\n**MAIN MENU**");
printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit \nEnter your choice: ");
scanf("%d", &option);
switch(option)
{

case 1: start = insertq(start);
break;
case 2: start = deleteq(start);
break;
case 3: start = displayq(start);
break;
}
}while(option!=4);
getch();
}

struct node *insertq(struct node *start)
{

struct node *new_node, *ptr;
int priority;
new_node = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the Job=");
scanf("%s", new_node->job);
printf("\nEnter the Priority=");
scanf("%d", &priority);
new_node->priority = priority;
if(start == NULL || priority < start->priority)
{

new_node->next = start;
start = new_node;
}
else
{

ptr = start;
while(ptr->next != NULL && ptr->next->priority <= priority)

ptr = ptr->next;
new_node->next = ptr->next;
ptr->next = new_node;
}
return start;
}

struct node *deleteq(struct node *start)
{

struct node *ptr;
if(start == NULL)
{

printf("\nQueue is Empty!");
return NULL;
}
else
{
ptr = start;
printf("\nDeleted Item = %s", ptr->job);
start = ptr->next;
free(ptr);
}
return start;
}

struct node *displayq(struct node *start)

{
struct node *ptr;
ptr = start;
if(start == NULL)
printf("\nQueue is Empty!");
else
{
printf("\nPriority Queue : ");
while(ptr != NULL)
{
printf("\n%s[Priority = %d]", ptr->job, ptr->priority);
ptr = ptr->next;
}
}
return start;

}

Implementation Of Single Linked List

Input:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{

int data;
struct node *next;
};

struct node *start=NULL;
struct node *create(struct node *);
struct node *insertBeg(struct node *);
struct node *insertEnd(struct node *);
struct node *insertBefore(struct node *);
struct node *insertAfter(struct node *);
struct node *deleteBeg(struct node *);
struct node *deleteEnd(struct node *);
struct node *deleteNode(struct node *);
struct node *deleteAfter(struct node *);
struct node *deleteList(struct node *);
struct node *display(struct node *);
struct node *sortedList(struct node *);

void main()

{
int option;
do
{
printf("\n***MAIN MENU***");
printf("\n1.Create \n2.Display \n3.Insert from the Beginning \n4.Insert from the End

\n5.Insert Before a Given Node \n6.Insert After a given Node \n7.Delete from the Beginning
\n8.Delete from the End \n9.Delete a Node \n10.Delete After a given Node \n11.Delete the List
\n12.Sort List \n13.Exit \nEnter your Choice=");

scanf("%d", &option);
switch(option)
{

case 1: start = create(start);
break;
case 2: start = display(start);
break;
case 3: start = insertBeg(start);
break;
case 4: start = insertEnd(start);
break;
case 5: start = insertBefore(start);
break;
case 6: start = insertAfter(start);
break;
case 7: start = deleteBeg(start);
break;
case 8: start = deleteEnd(start);
break;

case 9: start = deleteNode(start);
break;
case 10: start = deleteAfter(start);
break;
case 11: start = deleteList(start);
break;
case 12: start = sortedList(start);
break;
}
}while(option != 13);
getch();
}

struct node *create(struct node *start)
{

struct node *new_node, *ptr;
int value;
printf("\nEnter -1 to End");
printf("\nEnter the data=");
scanf("%d", &value);
while(value != -1)
{

new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = value;
if(start == NULL)
{

new_node->next = NULL;

start = new_node;
}
else
{

ptr = start;
while(ptr->next != NULL)

ptr = ptr->next;
new_node->next = NULL;
ptr->next = new_node;
}
printf("\nEnter -1 to End");
printf("\nEnter the data=");
scanf("%d", &value);
}
return start;
}

struct node *insertBeg(struct node *start)
{

struct node *new_node;
int value;
printf("\nEnter the data=");
scanf("%d", &value);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = value;
new_node->next = start;
start = new_node;

return start;
}

struct node *insertEnd(struct node *start)
{

struct node *new_node, *ptr;
int value;
printf("\nEnter the data=");
scanf("%d", &value);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = value;
ptr = start;
while(ptr->next != NULL)

ptr = ptr->next;
ptr->next = new_node;
new_node->next = NULL;
return start;
}

struct node *insertBefore(struct node *start)
{

struct node *new_node, *ptr, *preptr;
int value, num;
printf("\nEnter the data=");
scanf("%d", &value);
printf("\nEnter the no before which data is to be added=");
scanf("%d", &num);

new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = value;
ptr = start;
while(ptr->data != num)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = new_node;
new_node->next = ptr;
return start;
}

struct node *insertAfter(struct node *start)
{

struct node *new_node, *ptr, *preptr;
int value, num;
printf("\nEnter the data=");
scanf("%d", &value);
printf("\nEnter the no before which data is to be added=");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = value;
ptr = start;
preptr = ptr;
while(preptr->data != num)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = new_node;
new_node->next = ptr;
return start;
}

struct node *deleteBeg(struct node *start)
{

struct node *ptr;
ptr = start;
start = start->next;
printf("\nValue Deleted=%d", ptr->data);
free(ptr);
return start;
}

struct node *deleteEnd(struct node *start)
{

struct node *ptr, *preptr;
ptr = start;
while(ptr->next != NULL)
{

preptr = ptr;
ptr = ptr->next;
}

preptr->next = NULL;
free(ptr);
return start;
}

struct node *deleteNode(struct node *start)
{

struct node *ptr, *preptr;
ptr = start;
int num;
printf("\nEnter the no. which is to be Deleted=");
scanf("%d", &num);
if(ptr->data == num)
{

start = deleteBeg(start);
return start;
}
else
{
while(ptr->data !=num)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
free(ptr);
return start;

}
}

struct node *deleteAfter(struct node *start)
{

struct node *ptr, *preptr;
int num;
printf("\nEnter the no after which the node is to be deleted=");
scanf("%d", &num);
ptr = start;
preptr = ptr;
while(preptr->data != num)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
printf("\nDeleted Value=%d", ptr->data);
free(ptr);
return start;
}

struct node *deleteList(struct node *start)
{

struct node *ptr;
if(start != NULL)
{

ptr = start;
while(ptr != NULL)
{

printf("\n%d to be Deleted next!", ptr->data);
start = deleteBeg(ptr);
ptr = start;
}
}
return start;
}

struct node *sortedList(struct node *start)
{

struct node *ptr1, *ptr2;
int temp;
ptr1 = start;
while(ptr1->next != NULL)
{

ptr2 = ptr1->next;
while(ptr2 != NULL)
{

if(ptr1->data > ptr2->data)
{

temp = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp;
}

ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
return start;
}

struct node *display(struct node *start)
{

struct node *ptr;
ptr = start;
while(ptr != NULL)
{

printf("%d\t", ptr->data);
ptr = ptr->next;
}
return start;
}

Linked Implementation of Stack

Input:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>

struct stack
{

int data;
struct stack *next;
};

struct stack *top = NULL;
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int peek(struct stack *);

int main()
{

int value, option;
do
{

printf("\n***MAIN MENU***");
printf("\n1.Push \n2.Pop \n3.Peek \n4.Display \n5.Exit \nEnter your Option=");
scanf("%d", &option);

switch(option)
{

case 1: printf("\nEnter the No. to be pushed on the stack=");
scanf("%d", &value);
top=push(top, value);

break;

case 2: top=pop(top);
break;

case 3: value=peek(top);
if(value != -1)
printf("\nThe value at the top of the stack is= %d", value);
else
printf("\nStack is Empty");

break;

case 4: top=display(top);
break;
}
}while(option != 5);
return 0;
}

struct stack *push(struct stack *top, int value)
{

struct stack *ptr;

ptr=(struct stack *)malloc(sizeof(struct stack));
ptr->data=value;
if(top == NULL)
{

ptr->next=NULL;
top = ptr;
}
else
{
ptr->next = top;
top = ptr;
}
return top;
}

struct stack *display(struct stack *top)
{

struct stack *ptr;
ptr = top;
if(top == NULL)

printf("\nStack is Empty");
else
{

while(ptr != NULL)
{

printf("\n %d", ptr->data);
ptr = ptr->next;

}
}
return top;
}

struct stack *pop(struct stack *top)
{

struct stack *ptr;
ptr = top;
if(top == NULL)

printf("\nStack Underflow");
else
{

top = top->next;
printf("\nThe Value being deleted is= %d", ptr->data);
free(ptr);
}
return top;
}

int peek(struct stack *top)
{

if(top == NULL)
return -1;

else
return top->data;

}

Circular Linked List

Input:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node
{

int data;
struct node *next;
};

struct node *start = NULL;
struct node *create(struct node *);
struct node *insertBeg(struct node *);
struct node *deleteBeg(struct node *);
struct node *display(struct node *);
struct node *insertEnd(struct node *);
struct node *deleteEnd(struct node *);
struct node *deleteAfter(struct node *);
struct node *deleteList(struct node *);

void main()
{

int option;
do
{

printf("\n***MAIN MENU***");
printf("\n1.Create a node \n2.Insert at the beginning \n3.Insert at the End
\n4.Delete at Beginning \n5.Delete at End \n6.Delete after \n7.Display \n8.Delete List \n9.Exit
\nEnter your Choice=");
scanf("%d", &option);
switch(option)
{

case 1: start = create(start);
break;
case 2: start = insertBeg(start);
break;
case 3: start = insertEnd(start);
break;
case 4: start = deleteBeg(start);
break;
case 5: start = deleteEnd(start);
break;
case 6: start = deleteAfter(start);
break;
case 7: start = display(start);
break;
case 8: start = deleteList(start);
}
}while(option != 8);
getch();
}

struct node *create(struct node *start)

{
struct node *new_node, *ptr;
int data;
printf("\nEnter -1 to end!");
printf("\nEnter the data=");
scanf("%d", &data);
while(data != -1)
{
if(start == NULL)
{
new_node =(struct node *)malloc(sizeof(struct node));
start = new_node;
new_node->data = data;
new_node->next = start;
}
else
{
new_node = (struct node *)malloc(sizeof(struct node));
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
new_node->data = data;
new_node->next = start;
ptr->next = new_node;
}
printf("\nEnter -1 to end!");
printf("\nEnter the data=");

scanf("%d", &data);
}
return start;
}

struct node *insertBeg(struct node *start)
{

struct node *new_node, *ptr;
int data;
printf("\nEnter the data=");
scanf("%d", &data);
ptr = start;
while(ptr->next != start)

ptr = ptr->next;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = start;
ptr->next = new_node;
start = new_node;
return start;
}

struct node *deleteBeg(struct node *start)
{

struct node *ptr;
ptr = start;
while(ptr->next != start)

ptr = ptr->next;
ptr->next = start->next;
printf("\nValue Deleted at the starting = %d", ptr->data);
free(ptr);
return start;

}

struct node *display(struct node *start)
{

struct node *ptr;
ptr = start;
while(ptr->next != start)
{

printf("\t%d", ptr->data);
ptr = ptr->next;
}
printf("\t%d", ptr->data);
return start;
}

struct node *insertEnd(struct node *start)
{

struct node *new_node, *ptr;
int data;
printf("\nEnter the data=");
scanf("%d", &data);
ptr = start;

new_node = (struct node *)malloc(sizeof(struct node));
while(ptr->next != start)

ptr = ptr->next;
new_node->data = data;
ptr->next = new_node;
new_node->next = start;
return start;
}

struct node *deleteEnd(struct node *start)
{

struct node *ptr,*preptr;
ptr = start;
while(ptr->next != start)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
printf("\nDeleted Value at the end=%d", ptr->data);
free(ptr);
return start;
}

struct node *deleteAfter(struct node *start)
{

struct node *ptr, *preptr;

int value;
printf("\nEnter the value after which the node has to be deleted=");
scanf("%d", &value);
ptr = start;
preptr = ptr;
while(preptr->data != value)
{

preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
if(ptr == start)
start = preptr->next;
free(ptr);
return start;
}

struct node *deleteList(struct node *start)
{

struct node *ptr;
ptr = start;
while(ptr->next != start)

start = deleteEnd(start);
free(start);
return start;
}

Implementation of Binary Tree

Input:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{

int data;
struct node *left;
struct node *right;
};

struct node *tree = NULL;
struct node *insert(struct node *, int);
struct node *displayInorder(struct node *);
struct node *delete(struct node *, int);
struct node *findSmallestElement(struct node *);

void main()
{

int option, data;
do
{

printf("\n***MAIN MENU***");
printf("\n1.Insert a node \n2.Display In-order Form Traversal \n3.Delete a Node \n4.Exit
\nEnter your option=");

scanf("%d", &option);
switch(option)
{

case 1: printf("\nEnter the data=");
scanf("%d", &data);
tree = insert(tree, data);

break;
case 2: tree = displayInorder(tree);
break;
case 3: printf("\nEnter the value to be deleted=");

scanf("%d", &data);
tree = delete(tree, data);
break;
}
}while(option != 4);
getch();
}

struct node *insert(struct node *tree, int data)
{

struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));

new_node->data = data;

if(tree == NULL)
{

tree = new_node;

new_node->left = NULL;
new_node->right = NULL;
}
else
{
if(data < tree->data)

tree->left = insert(tree->left, data);
else

tree->right = insert(tree->right, data);
}
return tree;
}

struct node *displayInorder(struct node *tree)
{

if(tree != NULL)
{

printf("\n");
displayInorder(tree->left);
printf("%d\t", tree->data);
displayInorder(tree->right);
}
return tree;
}

struct node *delete(struct node *tree, int data)
{

struct node *temp;
if(tree == NULL)
{

printf("\nTree is Empty!");
return tree;
}
else
{
if(data < tree->data)

tree->left = delete(tree->left, data);
else if(data > tree->data)

tree->right = delete(tree->right, data);
else
{

if(tree->left == NULL)
{

temp = tree->right;
free(tree);
return temp;
}
else if(tree->right == NULL)
{
temp = tree->left;
free(tree);
return temp;
}
else

{
temp = findSmallestElement(tree->right);
tree->data = temp->data;
tree->right = delete(tree->right, temp->data);

}
}
return tree;

}
}

struct node *findSmallestElement(struct node *tree)
{

if((tree == NULL) || (tree->left == NULL))
return tree;

else
return findSmallestElement(tree->left);

return tree;
}

Quick Sort

Input:
#include <stdio.h>
#include <conio.h>
#define SIZE 100

int partition(int a[], int beg, int end);
void quickSort(int a[], int beg, int end);

void main()
{

int a[SIZE], i, n;
printf("\nEnter the no. of elements in the array=");
scanf("%d", &n);
printf("\nEnter the elements in the array->\n");
for(i = 0; i < n; i++)

scanf("%d", &a[i]);
quickSort(a, 0, n-1);
printf("\nSorted Array is: \n");
for(i = 0; i < n; i++)

printf("%d\t", a[i]);
getch();
}

int partition(int a[], int beg, int end)
{

int left, right, temp, loc, flag;

loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{

while((a[loc] <= a[right]) && (loc != right))
right--;

if(loc == right)
flag = 1;

else if(a[loc] > a[right])
{

temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag != 1)
{
while((a[loc] >= a[left]) && (loc != left))

left++;
if(loc == left)

flag = 1;
else if(a[loc] < a[left])
{

temp = a[loc];
a[loc] = a[left];
a[left] = temp;

loc = left;
}
}
}
return loc;
}

void quickSort(int a[], int beg, int end)
{

int loc;
if(beg < end)
{

loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}


Click to View FlipBook Version
Previous Book
The Holocaust
Next Book
amber room- Unique Healthiness Sistem