CS6212 PDS Lab Manual CSE 2013 Regulations

January 13, 2017 | Author: heyramzz | Category: N/A
Share Embed Donate


Short Description

Download CS6212 PDS Lab Manual CSE 2013 Regulations...

Description

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Syllabus: 1. C Programs using Conditional and Control Statements 2. C Programs using Arrays, Strings and Pointers and Functions 3. Representation of records using Structures in C – Creation of Linked List – Manipulation of records in a Linked List 4. File Handling in C – Sequential access – Random Access 5. Operations on a Stack and Queue – infix to postfix – simple expression evaluation using stacks - Linked Stack Implementation – Linked Queue Implementation 6. Implementation of Sorting algorithms 7. Implementation of Linear search and Binary Search.

Page 1

CS6212-PROGRAMMING & DATA STRUCTURES LAB

1. C PROGRAMS USING CONDITIONAL AND CONTROL STATEMENTS Program to illutrate if-else - Program to find odd or even number PROGRAM: #include #include void main() { int n; clrscr(); printf("enter the num\n"); scanf("%d",&n); if(n%2==0) printf("the num is even\n"); else printf("the num is odd"); getch(); }

OUTPUT:

Page 2

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to Illustrate Conditional Operator- find greatest of two numbers PROGRAM #include #include void main() { int a,b; clrscr(); printf("enter the two numbers"); scanf("%d %d",&a,&b); if((a>b)?printf("%d is greater"):printf("%d is greater")) getch(); } OUTPUT

Page 3

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illustrate nested if-else-find greatest of threee numbers PROGRAM #include #include void main() { int a,b,c; clrscr(); printf("enter the three numbers"); scanf("%d %d %d",&a,&b,&c); if((a>b)&&(a>c)) printf("%d is greater",a); else if((b>a)&&(b>c)) printf("%d is greater",b); else printf("%d is greater",c); getch(); }

OUTPUT:

Page 4

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illustrate use of switch case PROGRAM #include #include void main() {int choice,r,a,b; clrscr(); printf("enter the two num"); scanf("%d %d",&a,&b); ch: printf("enter your choice\n"); printf("\n1)add\n2)sub\n3)mul\n4)div"); scanf("%d",&choice); switch(choice) { case 1:(r=a+b); printf("%d is sum",r); break; case 2:(r=a-b); printf("%d is diff",r); break; case 3:(r=a*b); printf("%d is product",r); break; case 4:r=a/b; printf("%d is ratio",r); break; default:printf("plz enter correct value"); goto ch; } getch();} OUTPUT

Page 5

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illutrate for loop- find the factorial of given number PROGRAM #include #include void main() { int n,fact=1,i; clrscr(); printf("enter the num"); scanf("%d",&n); for(i=1;ilink!=NULL)&&(head!=NULL)) { printf("->"); p=p->link; printf("%d",p->data); } } void main() { int ch=0; clrscr(); while(ch10->60->20->30 SINGLY LINKED LIST MENU 1.Create 2.Insert Page 27

CS6212-PROGRAMMING & DATA STRUCTURES LAB

3.Delete 4.Display 5.Exit Enter the choice:3 Enter the position:3 data has been deleted... SINGLY LINKED LIST MENU 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter the choice:4 SINGLY LINKED LIST->10->60->30 SINGLY LINKED LIST MENU 1.Create 2.Insert 3.Delete 4.Display 5.Exit Enter the choice:5

5. Operations on a Stack and Queue – infix to postfix – simple expression evaluation using stacks - Linked Stack Implementation – Linked Queue Implementation (i)Operations on a Stack STACK ARRAY IMPLEMNTATION PROGRAM: #include main() { int a[10]={0},i,top=-1,max=10,n,x; printf("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n"); Page 28

CS6212-PROGRAMMING & DATA STRUCTURES LAB

do { printf("\nEnter your choice\n"); scanf("%d",&n); switch(n) { case 1: if(top==max-1) printf("Overflow\n"); else { printf("Enter the element\n"); scanf("%d",&x); a[++top]=x; } break; case 2: if(topnext=NULL; } else { temp->next=top; top=temp; } } void display() { struct Node *var=top; if(var!=NULL) { printf("\nElements are as:\n"); while(var!=NULL) { printf("\t%d\n",var->Data); var=var->next; } printf("\n"); } else printf("\nStack is Empty"); } int main(int argc, char *argv[]) { int i=0; clrscr(); top=NULL; Page 31

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf(" \n1. Push to stack"); printf(" \n2. Pop from Stack"); printf(" \n3. Display data of Stack"); 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 Stack: "); scanf("%d",&value); push(value); display(); break; } case 2: { popStack(); display(); break; } case 3: { display(); break; } case 4: { struct Node *temp; while(top!=NULL) { temp = top->next; free(top); top=temp; } exit(0); Page 32

CS6212-PROGRAMMING & DATA STRUCTURES LAB

} default: { printf("\nwrong choice for operation"); } } } } OUTPUT:

(ii)Operations on a Queue QUEUE -ARRAY IMPLEMENTATION PROGRAM: #include #include #define MAX 10 void insert(int); int del(); int queue[MAX], rear=0, front=0; void display(); int main() { char ch , a='y'; int choice, token; Page 33

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf("1.Insert"); printf("\n2.Delete"); printf("\n3.show or display"); do { printf("\nEnter your choice for the operation: "); scanf("%d",&choice); switch(choice) { case 1: insert(token); display(); break; case 2: token=del(); printf("\nThe token deleted is %d",token); display(); break; case 3: display(); break; default: printf("Wrong choice"); break; } printf("\nDo you want to continue(y/n):"); ch=getch(); } while(ch=='y'||ch=='Y'); getch(); } void display() { int i; printf("\nThe queue elements are:"); for(i=rear;inext; free(var); } else printf("\nQueue Empty"); Page 36

CS6212-PROGRAMMING & DATA STRUCTURES LAB

} 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() { int i=0; clrscr(); Page 37

CS6212-PROGRAMMING & DATA STRUCTURES LAB

front=NULL; printf(" \n1. Enqueue to Queue"); printf(" \n2. Dequeue 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: { exit(0); } default: { printf("\nwrong choice for operation"); } }}} Page 38

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

(iii)infix to postfix – simple expression evaluation using stacks INFIX TO POST FIX USING STACK: PROGRAM: #include #include #include #define MAX 50 char stack[MAX],ch; char in[50],post[50]; int top=-1; void push(char ch) { if(top==MAX-1) return; stack[++top]=ch; } char pop() { if(top==-1) return'\0'; else Page 39

CS6212-PROGRAMMING & DATA STRUCTURES LAB

return stack[top--]; } int prece(char ch) { if(ch=='*'||ch=='/') return 2; else if(ch=='+'||ch=='-') return 1; else return 0; } void postfix() { int i=0,j=0; push('('); while(in[i]!='\0') { if(isdigit(in[i])) post[j++]=in[i]; else if (in[i]=='(') push('('); else if(in[i]==')') { while((ch=pop())!='(') post[j++]=ch; } else if(in[i]!=0) { while(prece(ch=pop())>=prece(in[i])) post[j++]=ch; push(ch); push(in[i]); } i++; } post[j]='\0'; printf("postfix notation is %s\n",post); } void evalpost() { Page 40

CS6212-PROGRAMMING & DATA STRUCTURES LAB

int i=0,op1,op2; char c; while( (ch=post[i++]) != '\0') { if(isdigit(ch)) { push(ch-'0'); /* Push the operand */ } else { /* Operator,pop two operands */ op2=pop(); op1=pop(); switch(ch) { case '+':push(op1+op2);break; case '-':push(op1-op2);break; case '*':push(op1*op2);break; case '/':push(op1/op2);break; } } } //printf("\nGiven Postfix Expn: %s\n",post); printf("\nResult after Evaluation: %d\n",stack[top]); } void main() { int len; clrscr(); printf("\nEnter expression in INFIX notation\n"); gets(in); len=strlen(in); in[len]=')'; in[len+1]='\0'; postfix(); evalpost(); getch(); }

Page 41

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

6. Implementation of Sorting algorithms INSERTION SORTING PROGRAM : #include #include void main(){ int i,j,s,temp,a[20]; clrscr(); printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF