• No results found

II YEAR III SEMESTER COURSE CODE: 7BCE3P1. CORE COURSE-VI DATA STRUCTURES AND COMPUTER ALGORITHMS LAB (Using C and C++)

N/A
N/A
Protected

Academic year: 2021

Share "II YEAR III SEMESTER COURSE CODE: 7BCE3P1. CORE COURSE-VI DATA STRUCTURES AND COMPUTER ALGORITHMS LAB (Using C and C++)"

Copied!
38
0
0

Loading.... (view fulltext now)

Full text

(1)

II YEAR – III SEMESTER COURSE CODE: 7BCE3P1

CORE COURSE-VI–DATA STRUCTURES AND COMPUTER ALGORITHMS LAB (Using C and C++)

Group A

(Programs from Data Structures Using C) 1. Implementing Stack as an array.

2. Implementing Stack as a linked list.

3. Convert Infix expression to Postfix expression using stack. 4. Convert Infix expression to Prefix expression using Stack. 5. Implementing Queue as an Array.

6. Implement Queue as a linked list. 7. Binary tree traversals.

8. Implement Binary Search Tree.

Group B

(Programs from Computer Algorithms Using C++) 1. Linear Search 2. Binary Search 3. Bubble Sort 4. Insertion Sort 5. Merge Sort 6. Quick Sort 7. Selection Sort

Books for Reference:

1. C and C++ Programming concepts and Data Structures, P.S.Subramanyam, BS Publications,2013.

Note:

One Question from Group A and another one Question from Group B is compulsory for University Examination.

(2)

`

GOVERNMENT ARTS AND SCIENCE COLLEGE

(Affiliated to Alagappa University)

THIRUVADANAI-623407

DEPARTMENT OF COMPUTER SCIENCE

2020-2021

II YEAR – III SEMESTER COURSE CODE: 7BCE3P1

CORE COURSE-VI–DATA STRUCTURES AND COMPUTER ALGORITHMS LAB (Using C and C++)

Group A

(Programs from Data Structures Using C)

1. Implementing Stack as an array. 2. Implementing Stack as a linked list.

3. Convert Infix expression to Postfix expression using stack. 4. Convert Infix expression to Prefix expression using Stack. 5. Implementing Queue as an Array.

6. Implement Queue as a linked list. 7. Binary tree traversals.

8. Implement Binary Search Tree.

Group B

(Programs from Computer Algorithms Using C++)

1. Linear Search 2. Binary Search 3. Bubble Sort 4. Insertion Sort 5. Merge Sort 6. Quick Sort 7. Selection Sort

(3)

`

1.IMPLEMENTING STACK AS AN ARRAY SOURCE CODE:

#include<stdio.h> #include<conio.h> #define SIZE 10 void push(int); void pop(); void display();

int stack[SIZE], top = -1; void main()

{

int value, choice; clrscr();

while(1){

printf("\n\n***** MENU *****\n");

printf("1. Push\n2. Pop\n3. Display\n4. Exit"); printf("\nEnter your choice: ");

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

case 1: printf("Enter the value to be insert: "); scanf("%d",&value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0);

default: printf("\nWrong selection!!! Try again!!!"); }

} }

void push(int value){ if(top == SIZE-1)

printf("\nStack is Full!!! Insertion is not possible!!!"); else{ top++; stack[top] = value; printf("\nInsertion success!!!"); } } void pop(){ if(top == -1)

printf("\nStack is Empty!!! Deletion is not possible!!!"); else{ printf("\nDeleted : %d", stack[top]); top--; } } void display(){ if(top == -1) printf("\nStack is Empty!!!"); else{ int i;

printf("\nStack elements are:\n"); for(i=top; i>=0; i--)

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

(4)

`

(5)
(6)

`

2.IMPLEMENTING STACK AS A LINKED LIST SOURCE CODE: #include<stdio.h> #include<conio.h> struct Node { int data;

struct Node *next; }*top = NULL;

void push(int); void pop(); void display(); void main() {

int choice, value; clrscr();

printf("\n:: Stack using Linked List ::\n"); while(1){

printf("\n****** MENU ******\n");

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); printf("Enter your choice: ");

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

case 1: printf("Enter the value to be insert: "); scanf("%d", &value);

push(value); break;

case 2: pop(); break; case 3: display(); break; case 4: exit(0);

default: printf("\nWrong selection!!! Please try again!!!\n"); }

} }

void push(int value) {

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; if(top == NULL) newNode->next = NULL; else newNode->next = top; top = newNode;

printf("\nInsertion is Success!!!\n"); }

void pop() {

if(top == NULL)

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

struct Node *temp = top;

printf("\nDeleted element: %d", temp->data); top = temp->next; free(temp); } } void display() { if(top == NULL)

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

struct Node *temp = top; while(temp->next != NULL){

printf("%d--->",temp->data); temp = temp -> next;

}

printf("%d--->NULL",temp->data); }

} OUTPUT:

(7)
(8)
(9)

`

3. CONVERT INFIX EXPRESSION TO POST FIX EXPRESSION USING STACK SOURCE CODE:

#include<stdio.h> #include<conio.h> char stack[20]; int top = -1; void push(char x) { stack[++top] = x; } char pop() { if(top == -1) return -1; else return stack[top--]; }

int priority(char x) { if(x == '(' ) return 0; if(x == '+' || x == '-') return 1; if(x == '*' || x == '/') return 2; else return 0; } main() { char exp[20]; char *e, x;

printf("Enter the expression :: "); scanf("%s",exp); e = exp; 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) {

(10)

`

printf("%c",pop()); }

}

(11)

`

4. CONVERT INFIX EXPRESSION TO PREFIX EXPRESSION USING STACK.

SOURCE CODE: #include<stdio.h> #include<conio.h> #define SIZE 50 #include<string.h> #include <ctype.h> char s[SIZE]; int top=-1; push(char elem) { s[++top]=elem; } char pop() { return(s[top--]); }

int pr(char elem) { switch(elem) { case '#': return 0; case ')': return 1; case '+': case '-': return 2; case '*': case '/': return 3; default: return 0; } } main() {

(12)

`

char infx[50],prfx[50],ch,elem; int i=0,k=0;

clrscr();

printf("\n\nRead the Infix Expression ? "); scanf("%s",infx); push('#'); strrev(infx); while( (ch=infx[i++]) != '\0') { if( ch == ')') push(ch); else if(isalnum(ch)) prfx[k++]=ch; else if( ch == '(') { while( s[top] != ')') prfx[k++]=pop(); } else { while( pr(s[top]) >= pr(ch) ) prfx[k++]=pop(); push(ch); } } while( s[top] != '#') prfx[k++]=pop(); prfx[k]='\0'; strrev(prfx); strrev(infx);

printf("\n\nGiven Infix Expn: %s Prefix Expn: %s\n",infx,prfx); getch();

(13)

`

OUTPUT:

5. IMPLEMENTING QUEUE AS AN ARRAY.

SOURCE CODE: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct Node { int Data;

struct Node* next; }*rear, *front;

void delQueue() {

struct Node *temp, *var=rear; if(var==rear) { rear = rear->next; free(var); } else printf("\nQueue Empty"); }

(14)

`

void push(int value) {

struct Node *temp;

temp=(struct Node *)malloc(sizeof(struct Node)); temp->Data=value; if (front == NULL) { front=temp; front->next=NULL; rear=front; } else { front->next=temp; front=temp; front->next=NULL; } } void display() {

struct Node *var=rear; if(var!=NULL)

{

printf("\nElements are as: "); while(var!=NULL) { printf("\t%d",var->Data); var=var->next; } printf("\n"); } else printf("\nQueue is Empty"); } int main()

(15)

` {

int i=0; front=NULL;

printf(" \n1. Push to Queue"); printf(" \n2. Pop from Queue"); printf(" \n3. Display Data of Queue"); printf(" \n4. Exit\n");

while(1) {

printf(" \nChoose Option: "); scanf("%d",&i); switch(i) { case 1: { int value;

printf("\nEnter a value to push into Queue: "); scanf("%d",&value); push(value); display(); break; } case 2: { delQueue(); display(); break; } case 3: { display(); break; } case 4: {

(16)

`

exit(0); }

default: {

printf("\nwrong choice for operation"); }

} }}

(17)

`

6.IMPLEMENT QUEUE AS A LINKED LIST.

SOURCE CODE: #include <stdio.h> #include <stdlib.h> #include<conio.h> struct node { int info; struct node *ptr; }*front,*rear,*temp,*front1; int frontelement(); void enq(int data); void deq(); void empty(); void display(); void create(); void queuesize(); int count = 0; void main() {

(18)

` int no, ch, e; clrscr(); printf("\n 1 - Enque"); printf("\n 2 - Deque"); printf("\n 3- display"); printf("\n 4 - exit"); create(); while (1) {

printf("\n Enter choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter data : "); scanf("%d", &no); enq(no); break; case 2: deq(); break; case 4: exit(0); case 3: display(); break; default:

printf("Wrong choice, Please enter correct choice "); break; } } } void create() {

(19)

`

front = rear = NULL; }

void enq(int data) {

if (rear == NULL) {

rear = (struct node *)malloc(1*sizeof(struct node)); rear->ptr = NULL; rear->info = data; front = rear; } else {

temp=(struct node *)malloc(1*sizeof(struct node)); rear->ptr = temp; temp->info = data; temp->ptr = NULL; rear = temp; } count++; } void display() { front1 = front;

if ((front1 == NULL) && (rear == NULL)) {

printf("Queue is empty"); return;

}

while (front1 != rear) {

printf("%d ", front1->info); front1 = front1->ptr;

(20)

` } if (front1 == rear) printf("%d", front1->info); } void deq() { front1 = front; if (front1 == NULL) {

printf("\n Error: Trying to display elements from empty queue"); return; } else if (front1->ptr != NULL) { front1 = front1->ptr;

printf("\n Dequed value : %d", front->info); free(front);

front = front1; }

else {

printf("\n Dequed value : %d", front->info); free(front); front = NULL; rear = NULL; } count--; } OUTPUT:

(21)

`

7.BINARY TREE TRAVERSALS.

SOURCE CODE: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data;

struct node* left; struct node* right; };

void inorder(struct node* root){ if(root == NULL) return; inorder(root->left);

printf("%d ->", root->data); inorder(root->right);

}

void preorder(struct node* root){ if(root == NULL) return; printf("%d ->", root->data); preorder(root->left);

(22)

`

preorder(root->right); }

void postorder(struct node* root) { if(root == NULL) return;

postorder(root->left); postorder(root->right); printf("%d ->", root->data); }

struct node* createNode(value) {

struct node* newNode = malloc(sizeof(struct node)); newNode->data = value;

newNode->left = NULL; newNode->right = NULL; return newNode;

}

struct node* insertLeft(struct node *root, int value) { root->left = createNode(value);

return root->left; }

struct node* insertRight(struct node *root, int value){ root->right = createNode(value);

return root->right; }

int main() {

struct node* root = createNode(1); clrscr(); insertLeft(root, 12); insertRight(root, 9); insertLeft(root->left, 5); insertRight(root->left, 6); printf("Inorder traversal \n"); inorder(root);

(23)

` printf("\nPreorder traversal \n"); preorder(root); printf("\nPostorder traversal \n"); postorder(root); getch(); } OUTPUT:

(24)

`

8. IMPLEMENT BINARY SEARCH TREE.

SOURCE CODE: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data;

struct node* left; struct node* right; };

struct node* createNode(value){

struct node* newNode = malloc(sizeof(struct node)); newNode->data = value;

newNode->left = NULL; newNode->right = NULL;

return newNode; }

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

if (root == NULL) return createNode(data);

if (data < root->data)

root->left = insert(root->left, data); else if (data > root->data)

root->right = insert(root->right, data); return root;

}

void inorder(struct node* root){ if(root == NULL) return; inorder(root->left);

(25)

`

printf("%d ->", root->data); inorder(root->right); }

int main(){

struct node *root = NULL; clrscr(); root = insert(root, 8); insert(root, 3); insert(root, 1); insert(root, 6); insert(root, 7); insert(root, 10); insert(root, 14); insert(root, 4); inorder(root); getch(); } OUTPUT:

(26)

` 1. LINEAR SEARCH SOURCE CODE: #include<iostream.h> #include<conio.h> void main() { clrscr();

int arr[10], i, num, n, c=0, pos; cout<<"Enter the array size : "; cin>>n;

cout<<"Enter Array Elements : "; for(i=0; i<n; i++)

{

cin>>arr[i];

}cout<<"Enter the number to be search : "; cin>>num;

for(i=0; i<n; i++) { if(arr[i]==num) { c=1; pos=i+1; break; } } if(c==0) {

cout<<"Number not found..!!"; }

else {

cout<<num<<" found at position "<<pos; }

getch(); }

(27)

`

(28)

` 2.BINARY SEARCH SOURCE CODE: #include <iostream.h> #include<conio.h> int main() {

int count, i, arr[30], num, first, last, middle; clrscr();

cout<<"how many elements would you like to enter?:"; cin>>count;

for (i=0; i<count; i++) {

cout<<"Enter number "<<(i+1)<<": "; cin>>arr[i];

}

cout<<"Enter the number that you want to search:"; cin>>num;

first = 0; last = count-1;

middle = (first+last)/2; while (first <= last) {

if(arr[middle] < num) {

first = middle + 1;

}

else if(arr[middle] == num) {

cout<<num<<" found in the array at the location "<<middle+1<<"\n";

break;

} else {

(29)

`

last = middle - 1; }

middle = (first + last)/2; }

if(first > last) {

cout<<num<<" not found in the array"; }

getch(); return 0; }

(30)

` 3.BUBBLE SORT SOURCE CODE: #include<iostream.h> #include<conio.h> int main() { clrscr(); int array[20],size,temp;

cout<<"Enter the size of array: "; cin>>size; cout<<"Enter the elements: ";

for(int i=0;i<size;i++) cin>>array[i]; for(i=1;i<size;i++)

{

for(int j=0;j<(size-i);j++) if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } }

cout<<"\nArray after sorting"; for(i=0;i<size;i++)

cout<<" "<<array[i]; getch();

return 0; }

(31)

` OUTPUT: 4.INSERTION SORT SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdlib.h>

void insertion(int [],int); void main()

{ int *arr; int i,n; clrscr();

cout<<"enter the no of elemnt in the array:\n"; cin>>n;

arr=(int *) malloc (sizeof(int));

cout<<"enter the element to be sort:\n"; for(i=0;i<n;i++)

cin>>arr[i]; insertion(arr,n);

cout<<"the sorted element are:\n"; for(i=0;i<n;i++)

cout<<arr[i]; getch(); }

void insertion(intarray[], int size) {

int i,j,temp; for(i=1;i<size;i++) {

(32)

`

temp=array[i]; for(j=i-1;j>=0;j--) if(array[j]>temp) array[j+1]=array[j]; else break; array[j+1]=temp; } } OUTPUT: 5.MERGE SORT SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdlib.h> void mergesort(int*,int); void merge (int*,int,int*,int);

(33)

` void main() { int *arr; int i,n; clrscr();

cout<<"enter the no of elemnt in the array:\n"; cin>>n;

arr=(int *) malloc (sizeof(int)*n); cout<<"enter the element to be sort:\n"; for(i=0;i<n;i++)

cin>>arr[i]; mergesort(arr,n);

cout<<"the sorted element are:\n"; for(i=0;i<n;i++)

cout<<”\n”<<arr[i]; getch();

}

void mergesort(int *array, int size) { int mid; if(size==1) return; else { mid=size/2; mergesort(array,mid); mergesort(array+mid,size-mid); merge(array,mid,array+mid,size-mid); } }

void merge(int *a,int s1,int *b, int s2) {

int i,j,k,*t;

t=(int*)malloc((s2+s1)*sizeof(int)); i=j=k=0;

(34)

` t[k++]=(a[i]<b[j])?a[i++]:b[j++]; while(i<s1) t[k++]=a[i++]; while(j<s2) t[k++]=b[j++]; for(i=0;i<k;i++) a[i]=t[i]; free(t); } OUTPUT:

(35)

` 6.QUICK SORT SOURCE CODE: #include<iostream.h> #include<conio.h> #include<stdio.h>

void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; }

int partition (int array[], int start, int end) {

int pivot = array[end]; // pivot element int i = (start - 1);

for (int j = start; j <= end- 1; j++) {

if (array[j] <= pivot) {

i++; // increment index of smaller element swap(&array[i], &array[j]); } } swap(&array[i + 1], &array[end]); return (i + 1); }

void quickSort(int array[], int start, int end) {

if (start < end) {

int p = partition(array, start, end);

quickSort(array, start, p - 1); quickSort(array, p + 1, end);

(36)

` } } int main() { clrscr(); int n,array[50];

cout<<"Enter number of Elements: "; cin>>n; cout<<"Enter Elements: ";

for(int i=0;i<n;i++) cin>>array[i]; quickSort(array, 0, n-1); cout<<"\nAfter Sorting: \n"; for(i=0;i<n;i++) cout<<array[i]<<" "; getch(); return 0; } OUTPUT:

(37)

` 7.SELECTION SORT SOURCE CODE: #include<iostream.h> #include<conio.h> void main() { clrscr();

int size, arr[50], i, j, temp; cout<<"Enter Array Size : "; cin>>size;

cout<<"Enter Array Elements : "; for(i=0; i<size; i++)

{

cin>>arr[i]; }

cout<<"Sorting array using selection sort...\n"; for(i=0; i<size; i++)

{ for(j=i+1; j<size; j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } }

cout<<"Now the Array after sorting is :\n"; for(i=0; i<size; i++)

{

cout<<arr[i]<<" "; }

getch(); }

(38)

`

References

Related documents

Data association Track handling Measurement update Measurement prediction FILTER EXTERNAL SENSORS Measurement update FILTER INTERNAL SENSORS Measurement update FILTER LANE

Journal of Applied and Natural Science 11(1): 23- 34 (2019) ISSN : 0974-9411 (Print), 2231-5209 (Online) journals.ansfoundation.org Composition, richness and floristic

Master Repository Controller 11 network 11 ports 11 T task templates adding to favorites 24 creating/editing 24 definition 5 importing/exporting 24 list of 81 management 23 tasks

If the political attack against the MBH98 study was rooted in synecdochally situat­ ing the hockey-stick article as representative of all global climate change studies, the

The histological evaluation of the brainstem nuclei, according to the guidelines established by the Italian law, are focused on identifying the hypoglossus, the

included their study in our review because of the large population followed up and the finding of 3 risk factors for general mortality in epilepsy coinciding with risk factors

Graph analysis based on a measure that favored the interaction between regions with a similar degree of atrophy was used to characterize the impact of the mesencephalic volume loss

In mice subjected to PSNL, repeated once daily intrathecal injection of TAT-GESV (10 nmol i.t.) and MK-801 (5 nmol i.t.) across eight consecutive days elevated (A) mechanical