Data Stuctures Lab Manual
Short Description
Download Data Stuctures Lab Manual...
Description
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 #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 #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;idata==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 rprev=r qnext=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 #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)); 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. FLOW CHART:
Circularly linked list start
Start=null
Read choice
Switch (choic e)
Create()
yes
Delete()
display()
While Chright=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 #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 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 #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;inext=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 rprev=r qnext=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 #include #include #include #include #include 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 #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)); 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. FLOW CHART:
Circularly linked list start
Start=null
Read choice
Switch (choic e)
Create()
yes
Delete()
display()
While Chdata=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”); 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 { for(i=1;i
View more...
Comments