116
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<conio.h> 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.

Data Stuctures Lab Manual

Embed Size (px)

Citation preview

Page 1: Data Stuctures Lab Manual

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<conio.h>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.

Page 2: Data Stuctures Lab Manual

FLOWCHART:

OUTPUT:

Enter the name: R. Siva KumarEnter 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

START

Read name, father name, city, age

Print name, father name, city, age

STOP

Page 3: Data Stuctures Lab Manual

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<conio.h>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<n;i++){Scanf(“%d”,&a[i]);}for(i=0;i<n;i++){s=s+a[i];}avg=s/n;printf(“The average of %d numbers=%f”,n,avg);getch();}

RESULT:

Thus the C-program to find the average of n numbers had been written and it is executed.

Page 4: Data Stuctures Lab Manual

FLOWCHART:

OUTPUT:

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

START

Read n

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

Read a[i]

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

s=s+a[i]

Avg=s/n

Print avg

stop

Page 5: Data Stuctures Lab Manual

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<conio.h>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.

Page 6: Data Stuctures Lab Manual

FLOW CHART:

OUTPUT:

Enter the numbers to be sort:51 5 3 4 2The minimum value is 1The maximum value is 5

start

Read n

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

Ifa[i]>a[j]

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

A[j]=n

Print minimum value a[i]

stop

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

Print maximum value a[j]

Page 7: Data Stuctures Lab Manual

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 programStep 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<conio.h>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<n;j++){Scanf(“%d”,&b[i][j]);a=0;a=a+b[i][j];printf(“Total value of each item by %d girl=%d”,i++,c);}For(i=0;i<n;i++){For(j=0;j<m;j++){c=0;c=a+b[i][j];printf(“ Total value of sales by each girl=%d”,c);}s=s+c;}printf(“Grand total of each item sold by each girl=%d”,s);getch();}

RESULT:

Thus the ‘c’ program to print the sales report of girlsexecuted and output was verified.

Page 8: Data Stuctures Lab Manual

FLOW CHAR T:

START

Read m,n

a=a+b[i][j]

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

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

Print a

for(j=0;j<n;j++)

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

c=c+b[i][[j]

Print c

A

FLOWCHART:

Page 9: Data Stuctures Lab Manual

OUTPUT:

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

s=s+c

A

STOP

Print s

Page 10: Data Stuctures Lab Manual

STUDENT DETAILS

AIM:

To write a ‘C’ program to print the mark details of n students.

ALGORITHM:

Step 1. Start the programStep 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<conio.h>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

Page 11: Data Stuctures Lab Manual

FLOWCHART:

START

Read the marks

S=0;t=0

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

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

s=s+a[i][j]

Avg=s/5

A

B

Page 12: Data Stuctures Lab Manual

OUTPUT:

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

b=avg/5

t=t+avg

Print avg

STOP

A

Write b

B

Page 13: Data Stuctures Lab Manual

FACTORIAL OF N NUMBERS

AIM:

To find the factorial of given numbers using recursive function in ‘c’ program.

ALGORITHM:

Step 1: Start the programStep 2: read nStep2: Find the factorial of n numbers using recursive functionStep 4: Stop the program

PROGRAM:

#include<stdio.h>#include<conio.h>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.

Page 14: Data Stuctures Lab Manual

FLOW CHART:

start

fact

Read n

Print fact

stop

Int fact(int n)

Ifn<0

PrintCannot find factorial

Return f

Return 1

If(n>1)

F=n*fact(n-1)

Main program

Sub program

no

yes

Page 15: Data Stuctures Lab Manual

OUTPUT:

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

Page 16: Data Stuctures Lab Manual

FIBONACCI SERIES

AIM:

To write a C-program to print a Fibonacci series.

ALGORITHM:

Step:1 Start the programStep:2 Read the value of 'n'Step:3 Executing he function int fib(int n)Step:4 Find the Fibonacci seriesStep:5 End program.

PROGRAM:

#include<stdio.h>include<conio.h>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.

Page 17: Data Stuctures Lab Manual

OUTPUT:

ENTER THE VALUE OF N; 5 0 1 1 2 3

start

Read n

F=fib(int n)

Print n

Stop

Int fib (int n)

Ifn<=i1

Return n

stop

a=0

b=1

x=a

a=b

b=a+b

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

next i

stop

yes

Sub program

Main program

no

FLOWCHART:

Page 18: Data Stuctures Lab Manual

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 auxiliaryStep:4 Move remaining disc from A to CStep:5 Move the n-1 disc from B to C using A as auxiliaryStep:6 Stop the program.

PROGRAM.

#include<stdio.h>#include<conio.h>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.

Page 19: Data Stuctures Lab Manual

FLOWCHART:

Main program:

START

Read n

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

STOP

Tower(int,char,char,char)

if(n==1)

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

Print move disc from reg to reg

Print move disc n from reg to reg

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

return

Sub-program:

Page 20: Data Stuctures Lab Manual

OUTPUT:

enter the no of disc to move 3

move disc middle from A to Cmove disc 2 from Amove disc from middle C to Bmove disc 1 from C to Bmove disc 3 from Amove disc from middle C to Amove disc 2 from C

Page 21: Data Stuctures Lab Manual

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<conio.h>#include<string.h>static int top=0static 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<l;i++) push(str[i]); for(i=0;i<l;i++){ k=loop; if(str[i]!=k){ printf("The string is not palindrome"); end;}elseprintf(“The string is Palindrome”);} getch();}void push(char c){ stack[top]=c; top=top++;}

Page 22: Data Stuctures Lab Manual

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.

Page 23: Data Stuctures Lab Manual

OUTPUT:

Enter the string

Madam

The string is Palindrome

start

Read the string

L=strlen[i]

Print not palindrome

Print palindrome

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

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

IfI=k

stop

no

yes

FLOWCHART:

Page 24: Data Stuctures Lab Manual

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 expressionStep: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<conio.h>#include<alloc.h>#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;

Page 25: Data Stuctures Lab Manual

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= ‘+’;breakcase 2:e= ‘-’;

Page 26: Data Stuctures Lab Manual

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.

Page 27: Data Stuctures Lab Manual

FLOWCHART:

START

Read input

Pre to post

STOP

Pre to post

while input()!

=10

if operat

ed

op2=pop()

op1=pop()

print postfix exp()

i++

Print the expression

yes

no

Sub-Program

Main program

Page 28: Data Stuctures Lab Manual

OUTPUT:

Enter the expreesion: +abThe postfix expression is ab+

Page 29: Data Stuctures Lab Manual

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<conio.h>#include<stdlib.h>#define size 5struck stack{ int s[size] int top;} st;int st full(){If (st.top>=size-1)return 1;elsereturn 0;}void push(int item){st.top++st.s[st.top]=item;}int stempty(){if(st.top==-1)return 1;elsereturn 0;}int pop(){int item:item = st.s[st.top];

Page 30: Data Stuctures Lab Manual

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");elsepush(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();

Page 31: Data Stuctures Lab Manual

}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.

Page 32: Data Stuctures Lab Manual

FLOWCHART:

switch(choic

e)

start

print1.push2.pop

3.display

display()

top=null

read choice

pop()push()

ifchoice<=

y

stop

yes

no

Main program:

Page 33: Data Stuctures Lab Manual

OUTPUT:

Implementation of stackMain menu1.Push2.Pop3.Display4.Exit

Enter your choice 1Enter the item to be pushed 3Do you want to continue yEnter your choice 2The popped element is 3Do you want to continue yThe stack is emptyDo you want to continue yEnter your choice 4

push()

read item

temp=malloc(size of stack)

temp->data=itemtemp->next=top

return

Sub-program

Page 34: Data Stuctures Lab Manual

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<conio.h>#include<stdlib.h>#define size 5struck queue{ int Q[size]int front,rear;} Q;int Q full(){If (Q.rear>=size-1)return 1;elsereturn 0;}void insert(int item){st.rear++;Q.que[Q.rear]=item;}int Qetempty(){If(Q.rear==-1)return 1;elsereturn 0;}int delete(){

Page 35: Data Stuctures Lab Manual

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");elseinsert(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:

Page 36: Data Stuctures Lab Manual

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.

Page 37: Data Stuctures Lab Manual

FLOWCHART:

Main Program:

Page 38: Data Stuctures Lab Manual

Sub Program:

Page 39: Data Stuctures Lab Manual

OUTPUT:

Implementation of queueMain menu1.Insert2.Delete3.Display4.Exit

Enter your choice 1Enter the item to be inserted 4Do you want to continue yEnter your choice 2The deleted element is 4Do you want to continue yThe queue is emptyDo you want to continue yEnter your choice 4

Page 40: Data Stuctures Lab Manual

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<conio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0typedef 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”);

Page 41: Data Stuctures Lab Manual

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;

Page 42: Data Stuctures Lab Manual

}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){

Page 43: Data Stuctures Lab Manual

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”);Newdata=val;Newnext=NULL;Newnext=tempnext;tempnext=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=listlink;else

Page 44: Data Stuctures Lab Manual

{ for(i=1;i<pos-1;i++) list=listlink;if(pos==n) listlink=NULL;else{ prev=list;list=listlink;next=listlink;prevlink=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.

Page 45: Data Stuctures Lab Manual

FLOWCHART:

start

Switch(choic

e)

Create()

WhileCh<=

0

stop

Read choice

Delete() display() view()

Start=null

Main Program

yes

no

Page 46: Data Stuctures Lab Manual

view()

return

list=head

if list=NUL

L

print roll no name

print roll no name

print list is empty

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

next list

Sub-Program:

Page 47: Data Stuctures Lab Manual

OUTPUT:

Program for various operations of linked list1. create2. display2. search3. insert an item in the list4. delete an item in the list5. exit

Enter your choice:1Enter the element 5Do you want to enter more element=YEnter the element 6Do you want to enter more element=N

1. create2. display3. search4. insert an item in the list5. delete an item in the list6. exit

Enter your choice : 3Enter the element you want to search: 5The element is present

Page 48: Data Stuctures Lab Manual

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<conio.h>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()

Page 49: Data Stuctures Lab Manual

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){

Page 50: Data Stuctures Lab Manual

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;

Page 51: Data Stuctures Lab Manual

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.

Page 52: Data Stuctures Lab Manual

START

Print program for various operation on doubly linked list 1.create2.display3.search4.insert5.delete6.exit

Read choice

Switchchoice

while(choice!=6)

STOP

create() display() search() insert() delete() exit()

FLOWCHART:

Main Program:

Page 53: Data Stuctures Lab Manual

OUTPUT:

Program for double link listInsertDeleteDisplaySearchingExitEnter your choice:1

Page 54: Data Stuctures Lab Manual

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<conio.h>#include<alloc.h>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();}

Page 55: Data Stuctures Lab Manual

Void addcirq (struct node **f,struct node **r,int item){ struct node *q; q=malloc (sizeof(struct node)); qdata+=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=qdata;*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”,qdata);qqlink;p=f;}}

RESULT:

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

Page 56: Data Stuctures Lab Manual

FLOW CHART:

start

Switch(choic

e)

Create()

WhileCh<=

0

stop

Read choice

Delete() display() view()

Start=null

Circularly linked list

yes

no

Page 57: Data Stuctures Lab Manual

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<conio.h>

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( );

Page 58: Data Stuctures Lab Manual

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!”);elseinorder (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;

Page 59: Data Stuctures Lab Manual

}}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);

Page 60: Data Stuctures Lab Manual

}}

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

Page 61: Data Stuctures Lab Manual

A

switch (ch)

while(ch <=8)

Create node(btree, temp)

preorder(btree)

insert(btree)

post order(btree)

Stop

Page 62: Data Stuctures Lab Manual

if(btree= =NULL)

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

if(temp->data> btree->data)

if(temp-> data=btree->data)

return

Print “Data already exists”

if(temp-> data< btree->data)

Page 63: Data Stuctures Lab Manual

OUTPUT:

Program for implementing simple binary tree1. Create2. Inorder3. Preorder4. PostorderEnter your choice 1Enter the element 5Do you want to enter more elements? (y/n)yEnter the element 3Do you want to enter more elements? (y/n)yEnter the element 4Do you want to enter more elements? (y/n)yEnter the element 6Do you want to enter more elements? (y/n)nProgram for implementing simple binary tree1. Create2. Inorder3. Preorder4. PostorderEnter your choice 2

5

Page 64: Data Stuctures Lab Manual

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 valueStep: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<conio.h>#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<n;i++) scanf (“%d”.& a[i]);

}

void display ( ){

Page 65: Data Stuctures Lab Manual

printf (“\n the elements are”);for (i=0;i<n;i++) printf (“\n %d ”,a[i]);

}

search (int k){ for(i=0;i<n;i++) { if(a[i]= =k)

return 1; } return 0;}

void main( ){

int status, key; clrscr ( );create ( );display ( );printf (“\m enter the element which you wish to search”);scanf (“%d”,& key);status = search (key);if (status = = 1) printf(“\n the element is present”);else printf(“\n the element is not found”);getch ( );

}

RESULT:

Page 66: Data Stuctures Lab Manual

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

FLOW CHART:Main program:

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

Enter the element

linearsearch()

Enter the limit

Print the output

Stop

if search = =-1

Page 67: Data Stuctures Lab Manual

Sub-program:

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

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

linearsearch()

if a[i]= =k

return 1

Page 68: Data Stuctures Lab Manual

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<conio.h>#include<stdlib.h>#define MAX 10void 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<n;i++){printf(“\n Enter the elements:”);scanf(“%d”,&x[i]);}quick(x,0,n-1);printf(“\n\t\t The sorted elements are:” );for(i=0;i<n;i++)printf(“\n\t\t%d”,x[i]);getch();}void quick(int x[MAX],int lb,int ub){

int new pivote:

Page 69: Data Stuctures Lab Manual

int partition(int x[MAX],int lb,int ub);if(lb<ub){new pivote=partition(x,lb,ub);quick(x,lb,new pivote-1);quick(x,new pivote+1,ub);}}int partition(int x[MAX],int lb,int ub){

int q,pivot,i,lower;void interchange(int x[MAX],int a,int b;interchange(x,lb,(lb+ub)/2);pivote=x[lb];lower=lb;for(i=lb+1;i<=ub;i++){if(x[i]<pivot){lower=lower+1;interchange(x,lower,i);}}interchange(x,lb,lower);q=lower;return q;void interchange(int x[MAX],int a,int b){int temp;temp=x[a];x[a]=x[b];x[b]=temp;} RESULT:

Thus a C-program is written to sort the elements by quick sort.

Page 70: Data Stuctures Lab Manual

FLOW CHART:

OUTPUT:

Enter how many elements do you wish to sort:5

START

Print “How many

elements”

Read n

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

Read x[i]

Print “Enter the

elements”

Next i

Quick(x,0,n-1)

Print “The sorted elements

are “

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

STOP

Print x[i]

Page 71: Data Stuctures Lab Manual

Enter the elements:55Enter the elements:44

Enter the elements:33Enter the elements:22Enter 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<n.Step:7 Print the list of sorted elements.Step:8 Stop.

PROGRAM:

#include<stdio.h>#include<conio.h>#include<process.h>void bubblesort(int a[20],int n);void bubblesort(int a[20],int n);{int i,j,temp;for(i=0;i<n-1;i++) { for(j=0;j<n;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }for(i=0;i<n;i++)printf(“\n\t%d”,a[i]);} void main(){int a[20],i,n;int ch;clrscr();printf(“Enter the number of elements in the sorting array”);

Page 72: Data Stuctures Lab Manual

scanf(“%d”,&n);printf(“Enter the number of elements in the array”);for(i=0;i<n;i++)scanf(“%d”,&a[i]);bubblesort(a,n);}

RESULT:

Thus a C-program is written to perform sorting by bubble sort.

Page 73: Data Stuctures Lab Manual

FLOW CHART:

Main program: Sub-program:

OUTPUT:

Enter the number of elements in the sorting array:3Enter the elements in the array:305010

Read

Elements

Enter the elements

Start

stop

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

Bubble(a,n)

int swapped=TRUE

i=0

j=0

WhileJ<n-1

Ajj=a[j+1];

A[][j]<a[j+1]Swapped=TRUE;

Temp=a[j];A[j]=a[j+1];

J+1

Print the elements

whilej<n

Page 74: Data Stuctures Lab Manual

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>#include<stdlib.h>#include<conio.h>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);{

Page 75: Data Stuctures Lab Manual

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);

Page 76: Data Stuctures Lab Manual

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)

Page 77: Data Stuctures Lab Manual

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.

Page 78: Data Stuctures Lab Manual

START

Print program for various operation on doubly linked list 1.create2.display3.search4.insert5.delete6.exit

Read choice

Switchchoice

while(choice!=6)

STOP

create() display() search() insert() delete() exit()

Page 79: Data Stuctures Lab Manual

OUTPUT:

Program for double link listInsertDeleteDisplaySearchingExitEnter your choice:1

Page 80: Data Stuctures Lab Manual

CONVERSION OF PREFIX TO POSTFIXAIM:

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 expressionStep: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<conio.h>#include<alloc.h>#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;

Page 81: Data Stuctures Lab Manual

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= ‘+’;breakcase 2:e= ‘-’;break;case 3:e= ‘*’;break;case 4:

Page 82: Data Stuctures Lab Manual

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.

Page 83: Data Stuctures Lab Manual

FLOWCHART:

START

Read input

Pre 2 post

STOP

Pre 2 post

while input()!

=10

if operat

ed

op2=pop()

op1=pop()

print postfix exp()

i++

Print the expression

yes

no

Sub-Program

Main program

Page 84: Data Stuctures Lab Manual

OUTPUT:

Enter the expreesion: +abThe postfix expression is ab+

Page 85: Data Stuctures Lab Manual

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<conio.h>#include<alloc.h>struct node{ int data;struct node *link;};void addcirq(struct node**,struct node**,int);

Page 86: Data Stuctures Lab Manual

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)); qdata+=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=qdata;*f=(*f)link;(*r)link=*f;free(q);}

Page 87: Data Stuctures Lab Manual

return(item);}return Null;}void cirq_display(struct node *f){struct node *q=f,*p=Null;while(q1=p){ printf(“%d”,qdata);qqlink;p=f;}}

RESULT:

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

FLOW CHART:

start

Switch(choic

e)

Create()

WhileCh<=

0stop

Read choice

Delete() display() view()

Start=null

Circularly linked list

yes

no

Page 88: Data Stuctures Lab Manual

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<conio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0

Page 89: Data Stuctures Lab Manual

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(){

Page 90: Data Stuctures Lab Manual

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)

Page 91: Data Stuctures Lab Manual

}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”);Newdata=val;Newnext=NULL;

Page 92: Data Stuctures Lab Manual

Newnext=tempnext;tempnext=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=listlink;else{ for(i=1;i<pos-1;i++) list=listlink;if(pos==n) listlink=NULL;else{ prev=list;list=listlink;next=listlink;prevlink=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.

Page 93: Data Stuctures Lab Manual

FLOWCHART:

start

Switch(choic

e)

Create()

WhileCh<=

0stop

Read choice

Delete() display() view()

Start=null

Main Program

yes no

Page 94: Data Stuctures Lab Manual

view()

return

list=head

if list=NUL

L

print roll no name

print roll no name

print list is empty

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

next list

Sub-Program:

Page 95: Data Stuctures Lab Manual

OUTPUT:

Program for various operations of linked list6. create2. display7. search8. insert an item in the list9. delete an item in the list10. exit

Enter your choice:1Enter the element 5Do you want to enter more element=YEnter the element 6Do you want to enter more element=N

2. create2. display7. search8. insert an item in the list9. delete an item in the list10. exit

Enter your choice : 3Enter the element you want to search: 5The element is present