Data Stuctures Lab Manual

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Data Stuctures Lab Manual as PDF for free.

More details

  • Words: 7,065
  • Pages: 95
BIO-DATA AIM: To write a C-program to print the bio-data. ALGORITHM: Step:1 Start the program. Step:2 Read name, father’s name, city, age. Step:3 Enter the details. Step:4 Print the details. Step:5 Stop the program. PROGRAM: #include<stdio.h> #include void main() { char name, fathername, city; int age; clrscr(); printf(“Enter the name:”); scanf(“%s”,name); printf(“\n Enter the father’s name:”); scanf(“%s”,fathername); printf(\n Enter the city”); scanf(“%s”,city); printf(“\n Enter the age:”); scanf(“%d”,&age); printf(“\n Name:%s”,Name); printf(“\n Father’s Name:%s”,fathername); printf(“\n City:%s”,city); printf(“\n Age:%d”,age); getch(); } RESULT: Thus the C-program to print the bio-data had been executed and the output was verified.

FLOWCHART:

START

Read name, father name, city, age

Print name, father name, city, age

STOP

OUTPUT: Enter the name: R. Siva Kumar Enter the Father’s name: M. Rajavel. Enter the city: Tiruchengode. Enter the age:21 Name: R. Siva Kumar. Father’s Name: M. Rajavel. City: Tiruchengode. Age: 21

AVERAGE OF ‘N’ NUMBERS AIM: To write a C-program to find the average of ‘n’ numbers. ALGORITHM: Step:1 Start the program. Step:2 Read n. Step:3 Enter the numbers. Step:4 Use for loop with condition i < n find its sum. Step:5 Calculate the average. Step:6 Stop the program. PROGRAM: #include<stdio.h> #include void main() { int a[20],i,n; float avg,s=0; printf(“Enter the number of element to be added”); scanf(“%d”,&n); printf(“\n Enter the numbers”); for(i=0;i
FLOWCHART:

START

Read n For(i=0;i
Read a[i]

For(i=0;i
s=s+a[i]

Avg=s/n Print avg

stop

OUTPUT: Enter the number of element: 5 Enter the numbers: 7 8 9 5 4 The average of 5 no is : 6.600000

LARGEST AND SMALLEST NUMBER IN AN ARRAY AIM: To write a C-program to find the largest and smallest number in an array. ALGORITHM: Step:1 Start. Step:2 Read ‘n’ number element of array. Step:3 Save the numbers in ascending order using temp’t’. Step:4 Print the first element (i.e) smallest end largest elements. Step:5 Stop. PROGRAM: #include<stdio.h> #include void main() { int n,i,j,a[15],t; clrscr(); printf(“Enter the numbers to be sort:”); scanf(“%d”,&n); for(i=1;i<=n;i++) { scanf(“%d”,&a[i]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf(“The minimum value is %d”,a[1]); printf(“The maximum value is %d”,a[n]); getch(); } RESULT: Thus the C-program to print the largest and smallest of ‘N’ numbers is executed and the output is verified.

FLOW CHART: start

Read n

For(i=1;i<=n;i++)

For(j=i+1;j<=n;j++)

Ifa[i]>a[j ]

I=a[i] A[i]=a[j] A[j]=n

Print minimum value a[i]

Print maximum value a[j]

stop OUTPUT: Enter the numbers to be sort:5 15342 The minimum value is 1 The maximum value is 5

SALES REPORT OF GIRLS AIM: To write a ‘c’ program to find the total value of each item by each girl. ALGORITHM: Step 1. Start the program Step 2. Read the no of items and girls. Step 3.Use for loop to get the value of sales and value of each item and grand total. Step 4. Print the value of sales, value of each item and grand total. Step 5. Stop PROGRAM: #include<stdio.h> #include void main() { int b[10][10],m, a, s=0,c=0; clrscr(); printf(“ Enter m, n”); scanf(“%d%d”,&m,&n); for(i=0;i<m;i++) { For(j=0;j
FLOW CHAR

T: FLOWCHART:

START

Read m,n

for(i=0;i<m;i++)

for(i=0;i
a=a+b[i][j]

Print a

for(j=0;j
for(i=0;i
c=c+b[i][[j]

Print c

A

A

s=s+c

Print s

STOP

OUTPUT: Enter m,n: 2 2 Enter the value of each item by 1 girl=8 Total value of each item by 2 girl=8 Total value of sales by 1 girl=5 Total value of sales by 2 girl=11 Grand total of each item sold by all girl=16

STUDENT DETAILS AIM: To write a ‘C’ program to print the mark details of n students. ALGORITHM: Step 1. Start the program Step 2. Read the marks of srudents using arrays and loops. Step 3. Find the average of 3 students by using arrays and loops. Step 4. Find the average of student . Step 5. Print average. Step 6. Find the overall average. Step 7. Print the overall average. Step 8. Stop. PROGRAM: #include<stdio.h> #include void main() { int a[5][5], i, j; float avg, b, s, t=0; clrscr(); for(i=1;i<4;i++) { printf(“\n Enter the mark for %d student”,i); for(j=1;j<6;j++) { scanf(“%d”,a[i][j]); s=s+a[i][j]; } avg=s/5; Ss0; printf(“ The no %d student avg is %f”,I,avg); t=t+avg; } b=t/3; printf(“The overall avg is %f”,b); getch(); } RESULT: Thus the ‘C’ program to print the student details was written, executed and output was verified

FLOWCHART:

START

Read the marks

S=0;t=0

For(j=0;j<4;j++)

B For(j=1;j<6;j++)

s=s+a[i][j]

Avg=s/5

A

A Print avg

t=t+avg

b=avg/5

B Write b

STOP

OUTPUT: Enter the mark for 1 student:50 50 50 50 The avg of 1 student is 50 Enter the mark for 2 student:70 70 70 70 The avg of 2 student is 70 Enter the mark of 3 student: 80 80 80 80 The avg of 3 student is 80 Overall avg is 66.66

FACTORIAL OF N NUMBERS AIM: To find the factorial of given numbers using recursive function in ‘c’ program. ALGORITHM: Step 1: Start the program Step 2: read n Step2: Find the factorial of n numbers using recursive function Step 4: Stop the program PROGRAM: #include<stdio.h> #include int fact (int n) void main() { int n; clrscr(); printf(“ enter the no:”); scanf(“%d”,&n); printf(“the factorial of %d is %d” ,n ,fact(n)); getch(); } int fact (int n) { int f=1; if(n<0); { printf(“cannot find factorial”); } else if(n>1) { f=n*fact n-1; return(f); } else return (1); } RESULT: Thus the ‘c’ program to find the factorial of given number using recursion function is executed and output was verified.

FLOW CHART: Main program start Read n fact

Print fact

stop Sub program Int fact(int n)

no

If n < 0

Print Cannot find factorial

Return f

Return 1

yes

If (n> 1)

F=n*fact(n-1)

OUTPUT: Enter the value of n: 5 The factorial of 5 is 120

FIBONACCI SERIES AIM: To write a C-program to print a Fibonacci series. ALGORITHM: Step:1 Start the program Step:2 Read the value of 'n' Step:3 Executing he function int fib(int n) Step:4 Find the Fibonacci series Step:5 End program. PROGRAM: #include<stdio.h> include void main() { int n; int fib(int n); printf("enter n/n"); scanf("%d",&n); fib(n); } int fib(int n); { int a=0;b=1;f,i,x; printf("\n%d\t%d",a,b); for(i=0;i<=n:i++) { x=a; a=b; b=x+a; f=printf("\t%d",b); } return f; } RESULT: Thus a C program to print the Fibonacci series is written.

FLOWCHART: Main program Sub program

start Read n F=fib(int n)

Int fib (int n) If n< =i1

Print n

yes

no

a=0

Return n Stop

b=1 stop For(i=2;i
x=a

a=b b=a+b next i stop

OUTPUT: ENTER THE VALUE OF N; 5 01123

TOWER OF HANOI AIM: To write a C-program for solving tower of Hanoi. ALGORTHIM: Step:1 Start the program. Step:2 If n==1,move single disc from A to C and stop. Step:3 Move the top = n-1 disc from A to B using C as auxiliary Step:4 Move remaining disc from A to C Step:5 Move the n-1 disc from B to C using A as auxiliary Step:6 Stop the program. PROGRAM. #include<stdio.h> #include void hanoi(int n,char initial,char final,char temp) { if(n==1) { printf("move disc one from middle %c\n",initial,final); return; hanoi(n-1;initial, final,temp); printf("move disc %d from %c \n"n,initial,final); hanoi(n-1,temp,final,initial); printf("move disc %d from %c \n",n,temp, final); } void main() { int n; clrscr(); printf("enter the no of disc to move"); scanf("%d",&n); hanoi(n,'A','B','C'); getch(); } RESULT: Thus a C program for solving tower of Hanoi is written.

FLOWCHART: Main program: START

Read n

Tower(n,’A’,’B’,’C,)

STOP Sub-program:

Tower(int,char,char,char)

if(n== 1) Print move disc from reg to reg Tower(n-1,from reg auxillary to reg)

Print move disc n from reg to reg

Tower(n-1,aux reg to reg from reg)

return

OUTPUT: enter the no of disc to move 3 move disc middle from A to C move disc 2 from A move disc from middle C to B move disc 1 from C to B move disc 3 from A move disc from middle C to A move disc 2 from C

PALINDROME USING STACK AIM: To write a C-program to check whether the given string is palindrome or not. ALGORITHM: Step:1 Get the string. Step:2 Find the string length. Step:3 Push each character to stack Step:4 Retrieve each character from stack in reverse order and store in new variable Step:5 Compare two characters and if two strings are equal say the string is palindrome otherwise the string is not palindrome. PROGRAM: #include<stdio.h> #include #include<string.h> static int top=0 static char stack[100] void main() { void push(char); char(pop); char stack[100],char str[100],l; int top i,j,l,c; clrscr(); printf("\n Enter the string"); scanf("%s",str); l=strlenght(str); for(i=0;i
char pop() { top=top-1; return(stack top) } RESULT: Thus a C-program is written to check whether the given string is palindrome or not.

FLOWCHART:

start Read the string L=strlen[i] For(i=1;i<=l;i+ +) For(i=1;i<=l;i+ +)

If I=k

Print not palindrome

yes Print palindrome

stop

OUTPUT: Enter the string Madam The string is Palindrome

no

CONVERSION OF PREFIX TO POSTFIX AIM: To write a C program to convert prefix to postfix expression.. ALGORITHM: Step:1 Start the program. Step:2 Read the given string. Step:3If the input symbol read is ‘c’ push it into the stack. Step:4 If the input symbol read is an operand then place it in the expression Step:5 If the input symbol read is an operator,then a)check the precedence of the operator read. If it has a higher precedence then remove it from the stack and place it in the postfix expression. Repeat 5a) till operator in the stack has a higher precedence than that being read. Step:6 If the input symbol read is closing parenthesis then pop all the operators from the stack and place them in the postfix expression till the opening parenthesis is encountered. Step:7 Stop the program. PROGRAM: #include<stdio.h> #include #include #include<stdlib.h> #include<process.h> #include<string.h> char pre[40],post[40]; int top=0,st[20]; void postfix(); void push(int); char pop(); void main() { clrscr(); printf(“Enter the expression”); scanf(“%s”,&pre); postfix(); getch(); } void postfix() { int i,j=0; fro(i=0;pre[i]!=’\0’;i++) { switch(inf[i]) { case ‘+’: while(st[top]>=1) post[j++]=pop(); push(1); break;

case ‘-’: while(st[top]>=1) post[j++]=pop(); push(2); break; case ‘*’: while(st[top]>=3) post[j++]=pop(); push(3); break; case ‘/’: while(st[top]>=3) post[j++]=pop(); push(4); break; case ‘^’: while(st[top]>=1) post[j++]=pop(); push(5); break; case ‘(’: push(0); break; case ‘)’: while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=pre[i]; } } while(top>0) post[j++]=pop(); printf(“\nThe postfix expression is”,post); } void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top] ; top--; switch(el) { case 1: e= ‘+’; break case 2: e= ‘-’;

break; case 3: e= ‘*’; break; case 4: e= ‘/’; break; case 5: e= ‘^’; break; } return(e); } RESULT: Thus a C program to convert the prefix expression to postfix expression was written, executed and the output was verfied.

FLOWCHART: START

Main program

Read input

Pre to post

STOP

Sub-Program Pre to post

while input()! =10

yes

no if operat ed Print the expression op2=pop()

op1=pop()

print postfix exp() i++

OUTPUT: Enter the expreesion: +ab The postfix expression is ab+

IMPLEMENTATION OF STACK AIM: To write a C- program to implement the element of stack. ALGORITHM: Step 1: Start. Step 2: Define structure & size of stack. Step 3: Initialize st.top = -1. Step 4: Read choice for 1.push 2.pop 3.display 4.Exit. Step 5: If choice is 1,then perform push operation. If choice is 2,then perform pop operation. If choice is 3,then perform display operation. If choice is 4,then perform Exit. Step 6: Read the choice Y_continue or N_not continue. Step 7: If Y_continue else exit. Step 8: STOP. PROGRAM: #include<stdio.h> #include #include<stdlib.h> #define size 5 struck stack { int s[size] int top; } st; int st full() { If (st.top>=size-1) return 1; else return 0; } void push(int item) { st.top++ st.s[st.top]=item; } int stempty() { if(st.top==-1) return 1; else return 0; } int pop() { int item: item = st.s[st.top];

st.top--; return(item); } void display() { int i; If (stempty()) printf(" \n stack is empty"); else { for(i=st.top;i>=0;i++) printf(" \n %d",st.s[i]); } } void main(void) { int item,choice; char ans; st.top =-1; clrscr(); printf(" \n\t\t Implementation of stack"); do { printf("\n Main menu"); printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit"); printf("Enter your choice"); scanf("%d",& choice); switch (choice) { case 1: { printf("\n Enter the item to be pushed"); scanf("%d",&item): if(st.full()) printf("\n stack is full"); else push(item); } break; case 2: { If (stempty()) printf("\n Empty stack!underflow!!"); else { item = pop(); printf("\n the popped element is %d",item); } break; case 3: display(); break; case 4: exit();

} printf("\n Do you want to continue"); ans=getchar(); }while (ans=='y' || ans=='Y'); getch(); } RESULT: Thus the C-program to implement the element of stack using arrays was done and the output was verified.

FLOWCHART:

Main program: start

top=null

print 1.push 2.pop 3.display

read choice

switch (choic e)

push()

pop()

if choice<= y no stop

display()

yes

Sub-program push()

read item

temp=malloc(size of stack)

temp->data=item temp->next=top

return

OUTPUT: Implementation of stack Main menu 1.Push 2.Pop 3.Display 4.Exit Enter your choice 1 Enter the item to be pushed 3 Do you want to continue y Enter your choice 2 The popped element is 3 Do you want to continue y The stack is empty Do you want to continue y Enter your choice 4

IMPLEMENTATION OF QUEUE AIM: To write a C-program to implement the element of queue. ALGORITHM: Step 1: Start. Step 2: Define structure & size of queue. Step 3: Initialize Q.rear=0,Q.front=-1. Step 4: Read choice for 1.insert 2.delete 3.display 4.Exit. Step 5: If choice is 1,then perform insert operation. If choice is 2,then perform delete operation. If choice is 3,then perform display operation. If choice is 4,then perform Exit. Step 6: Read the choice Y_continue or N_not continue. Step 7: If Y_continue else exit. Step 8: Stop. PROGRAM: #include<stdio.h> #include #include<stdlib.h> #define size 5 struck queue { int Q[size] int front,rear; } Q; int Q full() { If (Q.rear>=size-1) return 1; else return 0; } void insert(int item) { st.rear++; Q.que[Q.rear]=item; } int Qetempty() { If(Q.rear==-1) return 1; else return 0; } int delete() {

int item: item = Q.que[Q.rear]; st.front++; return(item); } void display() { int i; If (stempty()) printf(" \n queue is empty"); else { for(i=Q.front;i<=Q.rear;i++) printf(" \n %d",Q.que[i]); } } void main(void) { int item,choice; char ans; Q.front==0; Q.rear==0; clrscr(); printf(" \n\t\t Implementation of queue"); do { printf("\n Main menu"); printf("\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit"); printf("Enter your choice"); scanf("%d",& choice); switch (choice) { case 1: { printf("\n Enter the item to be inserted"); scanf("%d",&item): if(Q.full()) printf("\n queue is full"); else insert(item); } break; case 2: { If (Qetempty()) printf("\n Empty queue!underflow!!"); else { item = delete(); printf("\n the deleted element is %d",item); } } break; case 3:

display(); break; case 4: exit(); } printf("\n Do you want to continue"); ans=getchar(); }while (ans=='y' || ans=='Y'); getch(); } RESULT: Thus the C-program to implement the element of stack using arrays was done and the output was verified.

FLOWCHART: Main Program:

Sub Program:

OUTPUT: Implementation of queue Main menu 1.Insert 2.Delete 3.Display 4.Exit Enter your choice 1 Enter the item to be inserted 4 Do you want to continue y Enter your choice 2 The deleted element is 4 Do you want to continue y The queue is empty Do you want to continue y Enter your choice 4

SINGLY LINKED LIST AIM: To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a singly linked list. ALGORITHM: Step 1. Start the program. Step 2. To perform various operations read the value for choice. Step 3. If the choice is 1. create function is called to create to the list. a) Assign temp=NULL and Flag=TRUE and also ans=’Y’ b) Read the value for val and allocate memory for the new node. A new node is created and the above procedure is followed until ans=’Y’. step 4. If the choice is 2, display() is executed to display the list. a) Assign temp=bead. b) If temp =NULL, print “ the list is empty”. c) print the list under it until temp is NULL. Step 5. If the choice is 3 search() is executed to search the element in the list. Step 6. If the choice is 4 insert() is executed to insert an element. Step 7.If the choice is 5 delete () is executed Step 8. If we want to exit ,the choice is 6 and the default statement is” Invalid choice,try again”. Step 9. Stop the program. PROGRAM: #include<stdio.h> #include #include<stdlib.h> #define TRUE 1 #define FALSE 0 typedef struct SLL { int data; struct SLL*next; }node; Node*create(); void main() { int choice, val; char ans; node*head; void display(node*); node*search(node*,int); void insert(node*) void delete(node**); head=NULL; do { clrscr(); printf(“ program to perform various operations on linked list”);

printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”); printf(“ \n Enter your choice”); scanf(“%d”,& choice); switch(choice) { case1: head=create(); break; case 2: display (head); break; case 3: printf(“ enter the element you want to search”); scanf(“%d”,val); scanf(“head,val); break; case 4: insert(head); break; case 5: dele (head); break; case 6: exit(0); default; clrscr(); printf(“ Invalid choice,try again”); getch(); } } while(choice!=t); } node*create() { node*temp,*new,*head; int val, flag; char ans=’Y’; node*get_node(); temp=NULL; flag=TRUE; do { { printf(“\n Enter the element:”); scanf(“%d”,&val); new=get_node(); If(new ==NULL) printf(“ \n memory is not allocated”); new->data=val; if(flag) { head= new; temp=head; flag=FALSE;

} else { temp->next=new; temp=new; } printf(“\n do you want to enter more element?(y/n)”); ans=getche(); } while (ans==’Y’); printf(“The singly linked list is created”); getch(); clrscr(); return head; } node*get_node() { node*temp; temp=(node*)malloc(size of node)); temp->next=NULL; return temp; } void display(node*head) { node*temp; temp=head; if(temp==NULL) { printf(“\n The list is empty”); getch(); clrscr(); return; } while(temp!=NULL) } printf(“%d->”,temp->data); temp=temp->next; } printf(“NULL”); } node*searchnode(node*head,int key) { node*temp; int found; temp=head; if(temp==NULL) { printf(“The linked list is empty”); getch(); clrscr(); return NULL; } found=FALSE; while(temp!=NULL&&!found){

if(found) { printf(“The element is present in the list\n”); getch(); return temp; } else { printf(“The element is not present in the list\n”); getch(); return NULL; } } void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) { printf(“\nInsertion is not possible”); getch(); return; } clrscr(); printf(“\nEnter the elements”); scanf(“%d”,&val); New=(node*)malloc(sizeof(node)); if(New==NULL) printf(“\nMemory is not allocated”); Newdata=val; Newnext=NULL; Newnext=tempnext; tempnext=New; printf(“\nElement is inserted”); getch(); } void del() { int t,pos,j; printf(“\n want to delete any data?(y/n)”); scanf(“%c”,j); while(j==’y’) { list=head; printf(“enter the position to be deleted”); fflush (stdin); scanf(“%d”,&pos); if(pos==1) { head=listlink; else

{ for(i=1;i<pos-1;i++) list=listlink; if(pos==n) listlink=NULL; else { prev=list; list=listlink; next=listlink; prevlink=next; } } n--; printf(“want delete any data?(y/n)”); fflush (stdin); scanf(“%c”,&j); } }

RESULT: Thus the c-program for singly linked list had been written and the operations such as creation ,deletion, insertion, display was performed.

FLOWCHART: Main Program

start

Start=null

Read choice

Switch (choic e)

Create()

yes

Delete()

display()

While Ch<= 0 no stop

view()

Sub-Program: view()

list=head

print list is empty if list=NUL L

print roll no name

for(list!=NULL;list=list->link)

print roll no name

next list

return

OUTPUT: Program for various operations of linked list 1. create 2. display 2. search 3. insert an item in the list 4. delete an item in the list 5. exit Enter your choice:1 Enter the element 5 Do you want to enter more element=Y Enter the element 6 Do you want to enter more element=N 1. create 2. display 3. search 4. insert an item in the list 5. delete an item in the list 6. exit Enter your choice : 3 Enter the element you want to search: 5 The element is present

IMPLEMENTATION OF DOUBLY LINKED LIST AIM: To write and execute a program to implement the operation in double linked list. ALGORITHM: Step:1 Start. Step:2 Read. Step:3 If c==1,create new node. Step:4 If c==2,insert new node at specific location. Step:5 If c==3,delete a particular node. Step:6 If c==4,display the list. Step:7 If c==5,exit. Step:8 Stop. PROGRAM: #include<stdio.h> #include<stdlib.h> #include struct node { int data; struct node*next,*prev; } *New,*new1,*temp,*start,*dummy; void add(void) struct node*get_node(); void display(void); void delete(void); int find(int); int first=1; void main() { char ans; int choice,num,found=0; struct=NULL; do{ clrscr(); printf("Program for double link list"); printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit"); printf("\n enter your choice:"); scanf("%d",&choice); switch(choice); { case 1:add() break; case 2:delete() break; case 3:display()

break; case 4:printf("enter no to be search:"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(found==0)) found=find(num); if(found) printf("\n no is present"); break; case 5:exit(0) } printf("\n do u want to continue?"); ans=getch(); } while(ans=='y'||ans=='Y') getch(); } void add(void) { clrscr(); New=get_node(); printf("\n\n\tEnter the element:"); scanf("%d",&New->data); if(first==1) { start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->pre=dummy; } } struct node*get_node() { new1=(node*)malloc(sizeof(struct node)); new1->next=NULL; new1->prev=NULL; return(new1); } void display(void) { clrscr(); temp=stat; if(temp==NULL); printf("\n double linked list is empty"); else { while(temp!=NULL) {

printf("%d",temp->data); temp=temp->next; } printf("NULL"); } getch(); } int find(int num) { if(temp->data==num) return(1) else temp=temp>next; return 0; } void delete(void) { int num,flag=0; int found; int least=0; clrscr(); temp=start; if(temp==NULL) printf("\n sorry dll not created"); else { printf("\n Enter the no of deleated"); scanf("%d",&num); while((flag==0)&&temp!=NULL)) { found=find(num); flag=found; } if(found==0) printf("\n no not found"); else { if(temp=start) { start=start->next; temp->next=NULL; start->next=NULL; free(temp); getch(); printf("\n the starting node is deleted"); } else { if(temp->next==NULL) last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next;

temp->prev=NULL; free(temp); if(last) printf("\n the last node is deleted"); else printf("\n the intermediate node is deleted"); } } } } RESULT: Thus,a program to implement double linked list was written,executed and output was verified.

FLOWCHART: Main Program: START

Print program for various operation on doubly linked list 1.create 2.display 3.search 4.insert 5.delete 6.exit

Read choice

Switch choice

create()

display()

search()

while (choice!=6)

STOP

insert()

delete()

exit()

f data=num i next=null rprev=r qnext=r

Return

OUTPUT: Program for double link list Insert Delete Display Searching Exit Enter your choice:1

CIRCULARLY LINKED LIST AIM: To write a program to implement circularly linked list and perform the operations such as creation, insertion, deletion and view ALGORITHM: Step 1.Start the program. Step 2.Adding the element in the circularly linked list is achieved by addcirq(); Addcirq:Creating a new node using malloc. Getting the item in the data field of the node. Linking the next field to the list. Step 3.Removing the node from the list using delcirq() operation. Delcirq:If the queue is empty print that the list is empty. Link the data of the node to the temp node and free the temp node.. Step 4.View the list using display operation.. Step 5.Using print function print all the data. Step 6.Stop the program. PROGRAM: #include<stdio.h> #include #include struct node { int data; struct node *link; }; void addcirq(struct node**,struct node**,int); int delcirq(struct node**,struct node**); void cirq_display(struct node*); void main() struct node *front,*rear; front=rear=NULL; addcirq(&front,&rear,10); addcirq(&front,&rear,17); addcirq(&front,&rear,18); addcirq(&front,&rear,23); addcirq(&front,&rear,12); clrscr(); printf(“\n Before deletion:\n”); cirq_display(front); delcirq(&front,&rear); delcirq(&front,&rear); printf(“\n After deletion:\n”); cirq_display(front); getch(); }

Void addcirq (struct node **f,struct node **r,int item) { struct node *q; q=malloc (sizeof(struct node)); qdata+=item; if(*f==null) *f=q; else (*r)link=q; (*r)q; (*r)link=(*f); } int delcirq (struct node **f,struct node **r) { struct node *q; int item; if(*f==null) printf(“queue is empty”); else { if(*f==*r) { item=(*f)data; free(*f); *f=null; *r=null; } else { q=*f; item=qdata; *f=(*f)link; (*r)link=*f; free(q); } return(item); } return Null; } void cirq_display(struct node *f) { struct node *q=f,*p=Null; while(q1=p) { printf(“%d”,qdata); qqlink; p=f; } } RESULT:

Thus a C program to implement circularly linked list was written, executed and the output was verified. FLOW CHART:

Circularly linked list start

Start=null

Read choice

Switch (choic e)

Create()

yes

Delete()

display()

While Ch<= 0 no stop

view()

BINARY TREE AIM: To write a C-program to create the simple binary tree and recursive traversal. ALGORITHM: Step:1 Start the program. Step:2 Read the choice Step:3 If the choice is one go to create ( ) function. Step:4 If the choice is two go to inorder ( ) function. Step:5 If the choice is three go to preorder ( ) function. Step:6 If the choice is four go to postorder ( ) function. Step:7 If the choice is five go to exit the program. Sub – function: Create ( ) i) Node = (node *) malloc (size of (node)); ii) Assign the right and left value as the NULL Insert ( ) i) Read where to insert the data (right/left). ii) If the value is ‘r’||’R’ assign root->right=new. Inorder ( ) i) Check the condition if(temp !=NULL) ii) First print the left root and data and right root. Preorder ( ) i) Check the condition if (temp! = NULL) ii) First print the data and then left root and right root. Postorder ( ) i) Check the condition if (temp! = NULL) ii) First print the right root and then left root at last print the data. PROGRAM: #include<stdio.h> #include typedef struct bin { int data; struct bin *left; struct bin *right; }node; void insert (node *,node*); void inorder (node *); void preorder (node *); void postorder (node *); nod *get_node( );

void main( ) { int choice; char ans = ‘n’; node *new,*root; root = null; clrscr( ); do { printf (“\n program for implementing simple binary tree”); printf (“\n 1.create”); printf (“\n 2.inorder”); printf (“\n 3.preorder”); printf (“\n 4.postorder”); printf (“\n 5.exit”); printf (“\n \t Enter Your Choice”); scanf (“%d”,&choice); switch (choice) { case 1: root = null; do { new=get_node( ); printf(“\n enter the element”); scanf(“%d”,& new->data); if(root = = null) root= new; else insert (root, new); printf (“\m do you want to enter more elements?(y/n)”); ans = getch(); } while (ans==’y’|| ans==’y’) clrscr ( ); break; case 2: if (root ==null) printf (“tree is not created!”); else inorder (root); break; case 3: if (root ==null) printf (“there is not created!”); preorder (root); break; case 4: if (root = = null) printf (“ tree is not created!”); postorder (root); break;

} }while(choice!=5); } node *get_node ( ) { node *temp; temp = (node *) malloc (size of(node)); temp ->left = null; temp->right = null; return temp; } void insert (node *root, node *new) { char ch; printf(“\n where to insert left/right of %d”, root->data); ch=getch( ); if((ch==’r’) || (ch==’r’)) { if(root->right == null) { root->right=new; } else insert (root->right,new); } else { if (root->left ==null) { root->left=new; } else insert (root->left,new); } } void inorder (node *temp) { if (temp!=null) { inorder (temp->left); printf (“ %d”,temp->data); inorder (temp->right); } } void preorder (node *temp) { if(temp != null) { printf(“%d”,temp ->data); preorder (temp ->left); preorder (temp->right);

} } void postorder (node *temp) { if (temp!=null) { postorder (temp-> left); postorder (temp->right); printf (“ %d “,temp->data); } }

RESULT: Thus a C-program to create the simple binary tree and recursive traversal had been executed and verified.

FLOW CHART:

Start

Print 1.create 2.Preorder 3.Inorder 4.Postorder

Read ch

A

A

switc h (ch)

Create node(b tree, temp)

preorder (btree)

while(c h <=8)

Stop

insert( btree)

post order( btree)

tree *insert node (tree *btree,tree* temp)

if(btree= =NULL)

if(temp-> data> btree>data)

if(temp-> data< btree>data)

if(temp> data=btr ee>data)

Print “Data already exists”

return

OUTPUT: Program for implementing simple binary tree 1. Create 2. Inorder 3. Preorder 4. Postorder Enter your choice 1 Enter the element 5 Do you want to enter more elements? (y/n)y Enter the element 3 Do you want to enter more elements? (y/n)y Enter the element 4 Do you want to enter more elements? (y/n)y Enter the element 6 Do you want to enter more elements? (y/n)n Program for implementing simple binary tree 1. Create 2. Inorder 3. Preorder 4. Postorder Enter your choice 2 5

SEQUENTIAL SEARCH AIM: To write a C-program to perform the linear search operation on some number of elements.

ALGORITHM: Step:1 Start the program. Step:2 Read the key value Step:3 Go to the functions create and display. Step:4 If (status = = 1) print the value is present. Step:5 Else print the value is not present. Step:6 Stop the program. Sub – function. Create ( ) i) Read the n value. ii) Read the a[i] elements using for loop. Display ( ) i)

Print the elements.

Search ( ) i) If (a[i] = = k) return 1. ii) Else return 0.

PROGRAM: #include<stdio.h> #include #define max 10 int a[max] , n , i; void create ( ) { printf (“ \n how many elements”); scanf (“%d”,&n); printf (“\n enter the elements”); for (i=0;i
printf (“\n the elements are”); for (i=0;i
RESULT:

Thus the c – program to perform the linear search operation on some number of elements had been executed and verified.

FLOW CHART: Main program: Enter the limit

for(i=0;i
Enter the element

linearsearch()

if search = =-1

Print the output

Stop

Sub-program: linearsearch()

for(i=0;i
if a[i]= =k

return 1

OUTPUT: How many elements 5 Enter the elements 23 36 56 48 79 Enter the element you wish to search 48 The element is present

QUICKSORT AIM: To write a C-program to sort the elements by quick sort. ALGORITHM: Step:1 Read the total number of elements in the list, say n. Step:2 Store the elements in the array. Step:3 Take the first element from the array and call it as the pivoted element. Step:4 Now find all elements which are less than the pivoted element and place them before the pivoted element. Thus the array will be divided in the lesser elements ,pivoted elements and elements greater than the pivoted element. Step:5 Repeat step 4 each time placing the pivoted element at the proper position. Thus the complete list will get sorted. Step:6 Stop. PROGRAM: #include<stdio.h> #include #include<stdlib.h> #define MAX 10 void main() { int n,i,x[MAX]; void quick(int x[],int,int); clrscr(); printf(“How many elements do you wish to sort?\n”); scanf(“%d”,&n); for(i=0;i
int partition(int x[MAX],int lb,int ub); if(lb
FLOW CHART: START Print “How many elements”

Read n for(i=0;i
Print “Enter the elements”

Read x[i]

Next i

Quick(x,0,n-1)

Print “The sorted elements are “

for(i=0;i
Print x[i]

STOP OUTPUT: Enter how many elements do you wish to sort:5

Enter the elements:55 Enter the elements:44 Enter the elements:33 Enter the elements:22 Enter the elements:11 The sorted elements are 11 22 33 44 55

BUBBLE SORT AIM: To write a C-program to perform sorting by bubble sort. ALGORITHM: Step:1 Read the total number of element, n. Step:2 .Store the elements in the array. Step:3 Set i=0. Step:4 Compare the adjacent elements. Step:5 Repeat step 4 for all n elements. Step:6 Increment the value of i by 1 and repeat step 4,5 for i #include #include<process.h> void bubblesort(int a[20],int n); void bubblesort(int a[20],int n); { int i,j,temp; for(i=0;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for(i=0;i
scanf(“%d”,&n); printf(“Enter the number of elements in the array”); for(i=0;i
FLOW CHART: Main program:

Sub-program:

Start Bubble(a,n)

Enter the elements

int swapped=TRUE

i=0

For(i=o;i
Elements

whil e j
stop j=0

Whil e J
Ajj=a[j+1] ; A[] [j]
J+1 Print the elements

OUTPUT: Enter the number of elements in the sorting array:3 Enter the elements in the array: 30 50 10

After sorting 10 30 50

IMPLEMENTATION OF DOUBLY LINKED LIST AIM: To write and execute a program to implement the operation in double linked list. ALGORITHM: Step:1 Start. Step:2 Read. Step:3 If c==1,create new node. Step:4 If c==2,insert new node at specific location. Step:5 If c==3,delete a particular node. Step:6 If c==4,display the list. Step:7 If c==5,exit. Step:8 Stop. PROGRAM: #include<stdio.h rel="nofollow"> #include<stdlib.h> #include struct node { int data; struct node*next,*prev; } *New,*new1,*temp,*start,*dummy; void add(void) struct node*get_node(); void display(void); void delete(void); int find(int); int first=1; void main() { char ans; int choice,num,found=0; struct=NULL; do{ clrscr(); printf("Program for double link list"); printf("\n1.insert\n2.delete\n3.display\n4.searching\n5.exit"); printf("\n enter your choice:"); scanf("%d",&choice); switch(choice); {

case 1:add() break; case 2:delete() break; case 3:display() break; case 4:printf("enter no to be search:"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(found==0)) found=find(num); if(found) printf("\n no is present"); break; case 5:exit(0) } printf("\n do u want to continue?"); ans=getch(); } while(ans=='y'||ans=='Y') getch(); } void add(void) { clrscr(); New=get_node(); printf("\n\n\tEnter the element:"); scanf("%d",&New->data); if(first==1) { start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->pre=dummy; } } struct node*get_node() { new1=(node*)malloc(sizeof(struct node)); new1->next=NULL; new1->prev=NULL; return(new1); } void display(void) { clrscr(); temp=stat; if(temp==NULL);

printf("\n double linked list is empty"); else { while(temp!=NULL) { printf("%d",temp->data); temp=temp->next; } printf("NULL"); } getch(); } int find(int num) { if(temp->data==num) return(1) else temp=temp>next; return 0; } void delete(void) { int num,flag=0; int found; int least=0; clrscr(); temp=start; if(temp==NULL) printf("\n sorry dll not created"); else { printf("\n Enter the no of deleated"); scanf("%d",&num); while((flag==0)&&temp!=NULL)) { found=find(num); flag=found; } if(found==0) printf("\n no not found"); else { if(temp=start) { start=start->next; temp->next=NULL; start->next=NULL; free(temp); getch(); printf("\n the starting node is deleted"); } else { if(temp->next==NULL)

last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next; temp->prev=NULL; free(temp); if(last) printf("\n the last node is deleted"); else printf("\n the intermediate node is deleted"); } } } } RESULT: Thus,a program to implement double linked list was written,executed and output was verified.

START

Print program for various operation on doubly linked list 1.create 2.display 3.search 4.insert 5.delete 6.exit

Read choice

Switch choice

create()

display()

search()

while (choice!=6)

STOP

insert()

delete()

exit()

f data=num i next=null rprev=r qnext=r

Return

OUTPUT: Program for double link list Insert Delete Display Searching Exit Enter your choice:1

CONVERSION OF PREFIX TO POSTFIX AIM: To write a C program to convert prefix to postfix expression.. ALGORITHM: Step:1 Start the program. Step:2 Read the given string. Step:3If the input symbol read is ‘c’ push it into the stack. Step:4 If the input symbol read is an operand then place it in the expression Step:5 If the input symbol read is an operator,then a)check the precedence of the operator read. If it has a higher precedence then remove it from the stack and place it in the postfix expression. Repeat 5a) till operator in the stack has a higher precedence than that being read. Step:6 If the input symbol read is closing parenthesis then pop all the operators from the stack and place them in the postfix expression till the opening parenthesis is encountered. Step:7 Stop the program. PROGRAM: #include<stdio.h> #include #include #include<stdlib.h> #include<process.h> #include<string.h> char pre[40],post[40]; int top=0,st[20]; void postfix(); void push(int); char pop(); void main() { clrscr(); printf(“Enter the expression”); scanf(“%s”,&pre); postfix(); getch(); } void postfix() { int i,j=0; fro(i=0;pre[i]!=’\0’;i++) { switch(inf[i]) { case ‘+’: while(st[top]>=1) post[j++]=pop(); push(1); break; case ‘-’: while(st[top]>=1) post[j++]=pop(); push(2); break;

case ‘*’: while(st[top]>=3) post[j++]=pop(); push(3); break; case ‘/’: while(st[top]>=3) post[j++]=pop(); push(4); break; case ‘^’: while(st[top]>=1) post[j++]=pop(); push(5); break; case ‘(’: push(0); break; case ‘)’: while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=pre[i]; } } while(top>0) post[j++]=pop(); printf(“\nThe postfix expression is”,post); } void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top] ; top--; switch(el) { case 1: e= ‘+’; break case 2: e= ‘-’; break; case 3: e= ‘*’; break; case 4:

e= ‘/’; break; case 5: e= ‘^’; break; } return(e); } RESULT: Thus a C program to convert the prefix expression to postfix expression was written, executed and the output was verfied.

FLOWCHART: START

Main program

Read input

Pre 2 post

STOP

Sub-Program Pre 2 post

while input()! =10

yes

no if operat ed Print the expression op2=pop()

op1=pop()

print postfix exp() i++

OUTPUT: Enter the expreesion: +ab The postfix expression is ab+

CIRCULARLY LINKED LIST AIM: To write a program to implement circularly linked list and perform the operations such as creation, insertion, deletion and view ALGORITHM: Step 1.Start the program. Step 2.Adding the element in the circularly linked list is achieved by addcirq(); Addcirq:Creating a new node using malloc. Getting the item in the data field of the node. Linking the next field to the list. Step 3.Removing the node from the list using delcirq() operation. Delcirq:If the queue is empty print that the list is empty. Link the data of the node to the temp node and free the temp node.. Step 4.View the list using display operation.. Step 5.Using print function print all the data. Step 6.Stop the program. PROGRAM: #include<stdio.h> #include #include struct node { int data; struct node *link; }; void addcirq(struct node**,struct node**,int);

int delcirq(struct node**,struct node**); void cirq_display(struct node*); void main() struct node *front,*rear; front=rear=NULL; addcirq(&front,&rear,10); addcirq(&front,&rear,17); addcirq(&front,&rear,18); addcirq(&front,&rear,23); addcirq(&front,&rear,12); clrscr(); printf(“\n Before deletion:\n”); cirq_display(front); delcirq(&front,&rear); delcirq(&front,&rear); printf(“\n After deletion:\n”); cirq_display(front); getch(); } Void addcirq (struct node **f,struct node **r,int item) { struct node *q; q=malloc (sizeof(struct node)); qdata+=item; if(*f==null) *f=q; else (*r)link=q; (*r)q; (*r)link=(*f); } int delcirq (struct node **f,struct node **r) { struct node *q; int item; if(*f==null) printf(“queue is empty”); else { if(*f==*r) { item=(*f)data; free(*f); *f=null; *r=null; } else { q=*f; item=qdata; *f=(*f)link; (*r)link=*f; free(q); }

return(item); } return Null; } void cirq_display(struct node *f) { struct node *q=f,*p=Null; while(q1=p) { printf(“%d”,qdata); qqlink; p=f; } } RESULT: Thus a C program to implement circularly linked list was written, executed and the output was verified. FLOW CHART:

Circularly linked list start

Start=null

Read choice

Switch (choic e)

Create()

yes

Delete()

display()

While Ch<= 0 no stop

view()

SINGLY LINKED LIST AIM: To write a ‘C’ program to perform operations like create, insert,delete, seach,and display in a singly linked list. ALGORITHM: Step 1. Start the program. Step 2. To perform various operations read the value for choice. Step 3. If the choice is 1. create function is called to create to the list. a) Assign temp=NULL and Flag=TRUE and also ans=’Y’ b) Read the value for val and allocate memory for the new node. A new node is created and the above procedure is followed until ans=’Y’. step 4. If the choice is 2, display() is executed to display the list. a) Assign temp=bead. b) If temp =NULL, print “ the list is empty”. c) print the list under it until temp is NULL. Step 5. If the choice is 3 search() is executed to search the element in the list. Step 6. If the choice is 4 insert() is executed to insert an element. Step 7.If the choice is 5 delete () is executed Step 8. If we want to exit ,the choice is 6 and the default statement is” Invalid choice,try again”. Step 9. Stop the program. PROGRAM: #include<stdio.h> #include #include<stdlib.h> #define TRUE 1 #define FALSE 0

typedef struct SLL { int data; struct SLL*next; }node; Node*create(); void main() { int choice, val; char ans; node*head; void display(node*); node*search(node*,int); void insert(node*) void delete(node**); head=NULL; do { clrscr(); printf(“ program to perform various operations on linked list”); printf(“\n 1. create \n 2. display\n 3. search\n 4. insert\n 5. delete \n 6.quit”); printf(“ \n Enter your choice”); scanf(“%d”,& choice); switch(choice) { case1: head=create(); break; case 2: display (head); break; case 3: printf(“ enter the element you want to search”); scanf(“%d”,val); scanf(“head,val); break; case 4: insert(head); break; case 5: dele (head); break; case 6: exit(0); default; clrscr(); printf(“ Invalid choice,try again”); getch(); } } while(choice!=t); } node*create() {

node*temp,*new,*head; int val, flag; char ans=’Y’; node*get_node(); temp=NULL; flag=TRUE; do { { printf(“\n Enter the element:”); scanf(“%d”,&val); new=get_node(); If(new ==NULL) printf(“ \n memory is not allocated”); new->data=val; if(flag) { head= new; temp=head; flag=FALSE; } else { temp->next=new; temp=new; } printf(“\n do you want to enter more element?(y/n)”); ans=getche(); } while (ans==’Y’); printf(“The singly linked list is created”); getch(); clrscr(); return head; } node*get_node() { node*temp; temp=(node*)malloc(size of node)); temp->next=NULL; return temp; } void display(node*head) { node*temp; temp=head; if(temp==NULL) { printf(“\n The list is empty”); getch(); clrscr(); return; } while(temp!=NULL)

} printf(“%d->”,temp->data); temp=temp->next; } printf(“NULL”); } node*searchnode(node*head,int key) { node*temp; int found; temp=head; if(temp==NULL) { printf(“The linked list is empty”); getch(); clrscr(); return NULL; } found=FALSE; while(temp!=NULL&&!found){ if(found) { printf(“The element is present in the list\n”); getch(); return temp; } else { printf(“The element is not present in the list\n”); getch(); return NULL; } } void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) { printf(“\nInsertion is not possible”); getch(); return; } clrscr(); printf(“\nEnter the elements”); scanf(“%d”,&val); New=(node*)malloc(sizeof(node)); if(New==NULL) printf(“\nMemory is not allocated”); Newdata=val; Newnext=NULL;

Newnext=tempnext; tempnext=New; printf(“\nElement is inserted”); getch(); } void del() { int t,pos,j; printf(“\n want to delete any data?(y/n)”); scanf(“%c”,j); while(j==’y’) { list=head; printf(“enter the position to be deleted”); fflush (stdin); scanf(“%d”,&pos); if(pos==1) { head=listlink; else { for(i=1;i<pos-1;i++) list=listlink; if(pos==n) listlink=NULL; else { prev=list; list=listlink; next=listlink; prevlink=next; } } n--; printf(“want delete any data?(y/n)”); fflush (stdin); scanf(“%c”,&j); } }

RESULT: Thus the c-program for singly linked list had been written and the operations such as creation ,deletion, insertion, display was performed.

FLOWCHART: Main Program

start

Start=null

Read choice

Switch (choic e)

Create()

yes

Delete()

display() While Ch<= 0 no stop

view()

Sub-Program: view()

list=head

print list is empty if list=NUL L

print roll no name

for(list!=NULL;list=list->link)

print roll no name

next list

return

OUTPUT: Program for various operations of linked list 6. create 2. display 7. search 8. insert an item in the list 9. delete an item in the list 10. exit Enter your choice:1 Enter the element 5 Do you want to enter more element=Y Enter the element 6 Do you want to enter more element=N 2. create 2. display 7. search 8. insert an item in the list 9. delete an item in the list 10. exit Enter your choice : 3 Enter the element you want to search: 5 The element is present

Related Documents