DS Lab Manual-Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Data structures Lab Manual CS 2001-1

1. Design, Develop and Implement a menu driven Program in C for the following
a) To create an Array of N Integer Elements and store n values.
• Inserting an Element (ELEM) at a given valid Position (POS)
• Deleting an Element at a given valid Position POS)
• Display of Array Elements
• Exit.
Support the program with functions for each of the above operations.

#include<stdio.h>
#include<stdlib.h>
int a[10], pos, elem;
int n = 0;
void create();
void display();
void insert();
void del();
void main()
{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1. Create an array of N integers");
printf("\n=>2. Display of array elements");
printf("\n=>3. Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4:del();
break;
case 5:exit(1);
break;
default:printf("\nPlease enter a valid choice:");
}
}
}

Department of CSE NMAMIT, Nitte 1


Data structures Lab Manual CS 2001-1

void create()
{
int i;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void display()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", a[i]);
}
void insert()
{
int i;
if(n == 5)
{
printf("\nArray is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter a valid position where element to be inserted: ");
scanf("%d", &pos);
}while(pos > n);
printf("\nEnter the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos ; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
display();
}

Department of CSE NMAMIT, Nitte 2


Data structures Lab Manual CS 2001-1

void del()
{
int i;

if(n == 0)
{
printf("\nArray is empty and no elements to delete");
return;
}
do
{
printf("\nEnter a valid position from where element to be deleted: ");
scanf("%d", &pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
for( i = pos; i< n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
display();
}

b] To create student structure with fields Roll No, Name, Semester, marks in 3 subjects.
And Write functions to
• Enter 5 students’ details and display the same using pointer to structure
• Find Student wise and subject wise total marks and display the same.

// Online C compiler to run C program online


#include <stdio.h>
struct student{
int rollno;
char name[15];
int sem;
float mark1;
float mark2;
float mark3;
};
int main() {
struct student s[2];
struct student *ptr;
int i;
float total=0;
float s1total=0,s2total=0,s3total=0;

Department of CSE NMAMIT, Nitte 3


Data structures Lab Manual CS 2001-1

ptr=&s;
for(i=0;i<2;i++)
{

printf("Enter student%d details",i+1);


printf("\nEnter name:");
scanf("%s",ptr->name);
printf("\nEnter rollno:");
scanf("%d",&ptr->rollno);

printf("\nEnter semester:");
scanf("%d",&ptr->sem);
printf("\nEnter mark of subject1:");
scanf("%f",&ptr->mark1);
printf("Enter mark of subject2:");
scanf("%f",&ptr->mark2);
printf("Enter mark of subject3:");
scanf("%f",&ptr->mark3);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{
printf("\nEnter student%d details",i+1);

printf("\nName=%s\n,RollNo=%d\n,Semester=%d\n,Mark1=%f,\nmark2=%f,\nMark3=%f",ptr-
>name,ptr->rollno,ptr->sem,ptr->mark1,ptr->mark2,ptr->mark3);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{
printf("\nTotalmarksofstudent %d is:",i+1);
total=ptr->mark1+ptr->mark2+ptr->mark3;
printf("%f",total);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{

s1total=s1total+ptr->mark1;
s2total=s2total+ptr->mark2;
s3total=s3total+ptr->mark3;

ptr++;

Department of CSE NMAMIT, Nitte 4


Data structures Lab Manual CS 2001-1

}
printf("\nSubject 1 total marks %f:",s1total);
printf("\nSubject 2 total marks%f",s2total);
printf("\nSubject 3 total marks%f",s3total);

return 0;
}
2. Design, Develop and Implement a menu driven Program in C for the following
operations on
a) STACK of Integers (Array with structure Implementation of Stack with size
MAX)
• Push an Element on to Stack.
• Pop an Element from Stack.
• Demonstrate Overflow and Underflow situations on Stack.
• Display the status of Stack .
• Exit

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
int top;
int items[MAX];
};
void push(int,struct stack *);
void pop(struct stack *);
void display(struct stack *);
int main()
{
struct stack s;
s.top=-1;
int choice,item;
for(;;)
{
printf("Enter your choice\n");
printf("1 Push\n2 Pop\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");

Department of CSE NMAMIT, Nitte 5


Data structures Lab Manual CS 2001-1

scanf("%d",&item);
push(item,&s);
break;
case 2: pop(&s);
break;
case 3: display(&s);
break;
case 4: exit(0);
}
}
return 0;
}
void push(int item,struct stack *s)
{
if(s->top==MAX-1)
printf("The stack is full\n");
else
{
(s->top)++;
s->items[s->top]=item;
}
}
void pop(struct stack *s)
{
int item;
if(s->top==-1)
printf("The stack is empty\n");
else
{
item=s->items[s->top];
(s->top)--;
printf("%d deleted\n",item);
}
}
void display(struct stack *s)
{
int t=s->top;
if(s->top==-1)
printf("The stack is empty\n");
else

Department of CSE NMAMIT, Nitte 6


Data structures Lab Manual CS 2001-1

{
printf("Elements in the stack are\n");
while(t>-1)
{
printf("%d ",s->items[t--]);
}
printf("\n");
}
}

Stack using arrays


#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}

Department of CSE NMAMIT, Nitte 7


Data structures Lab Manual CS 2001-1

case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");

Department of CSE NMAMIT, Nitte 8


Data structures Lab Manual CS 2001-1

}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

3. Queue of Integers (Array with structure Implementation of Queue with size MAX)
• Insert an Element in to Queue.
• Delete an Element from Queue.
• Demonstrate Overflow and Underflow situations on Queue.
• Display the status of Queue .
• Exit
Support the program with appropriate functions for each of the above
operations

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct queue{
int rear,front;
int q[MAX];
};
void INSERT(int,struct queue *);

Department of CSE NMAMIT, Nitte 9


Data structures Lab Manual CS 2001-1

void DELETE(struct queue *);


void DISPLAY(struct queue *);
int main()
{
int choice, item;
struct queue s;
s.rear=-1;
s.front=0;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:if(s.rear==MAX-1)
printf("Queue is full\n");
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&item);
INSERT(item,&s);
}
break;
case 2:DELETE(&s);
break;
case 3:DISPLAY(&s);
break;
case 4:exit(0);
}
}
return 0;
}
void INSERT(int item,struct queue *s)
{
s->rear=s->rear+1;
s->q[s->rear]=item;
}
void DELETE(struct queue *s)
{
int item;

Department of CSE NMAMIT, Nitte 10


Data structures Lab Manual CS 2001-1

if(s->front>s->rear)
printf("Queue is empty\n");
else
{
item=s->q[s->front];
printf("%d deleted\n",item);
(s->front)++;
}
}
void DISPLAY(struct queue *s)
{
int i;
if(s->front>s->rear)
printf("Queue is empty\n");
else
{
printf("Elements in the queue are\n");
for(i=s->front;i<=s->rear;i++)
printf("%d ",s->q[i]);
printf("\n");
}
}

4. Design, Develop and Implement a Program in C for the following Stack Applications
• Evaluation of Suffix expression with single digit operands and operators: +, -
, *, /, %, ^ b
• Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#define size 10
struct stack
{
int top;
double item[size];
};
double op(char,double,double);
void push(char, struct stack *);

Department of CSE NMAMIT, Nitte 11


Data structures Lab Manual CS 2001-1

double pop(struct stack *);


int main()
{
int i;
double op1,op2,res;
struct stack s; s.top = -1;
char postfix[20],sym;
printf("Enter the postfix expression\n");
gets(postfix);
for(i=0;i<strlen(postfix);i++)
{
sym=postfix[i];
if(isdigit(sym))
push(sym,&s);
else
{
op2=pop(&s);
op1=pop(&s);
res=op(sym,op1,op2);
if(res==-9999)
{
printf("Invalid input");
return 0;
}
s.item[++s.top]=res;
}
}
res=s.item[s.top--];
printf("Result after evaluation is %.2f",res);
return 0;
}
void push(char sym,struct stack *s)
{
s->top++;
s->item[s->top]=sym-'0';
}
double pop(struct stack *s)
{
double ele;
ele=s->item[s->top];

Department of CSE NMAMIT, Nitte 12


Data structures Lab Manual CS 2001-1

s->top--;
return(ele);
}
double op(char sym, double op1,double op2)
{
switch(sym)
{
case '+':return(op1+op2);
case '-':return(op1-op2);
case '*':return(op1*op2);
case '/':return(op1/op2);
case '&':
case '^':return(pow(op1,op2));
default:return -9999;
}
exit(0);
}

5. Design, Develop and Implement a Program in C for converting an Infix Expression to


Postfix Expression. Program should support for both parenthesized and free
parenthesized expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and
alphanumeric operands.
int F(char symbol)
{
switch(symbol)
{
case'+':
case'-':return 2;
case'*':
case'/':return 4;
case'^':
case'$':return 5;
case'(':return 0;
case'#':return -1;
default:return 8;

}
int G(char symbol)
{

Department of CSE NMAMIT, Nitte 13


Data structures Lab Manual CS 2001-1

switch(symbol)
{
case'+':
case'-':return 1;
case'*':
case'/':return 3;
case'^':
case'$':return 6;
case'(':return 9;
case')':return 0;
default:return 7;

}
void infix_postfix(char infix[],char postfix[])
{
int top,j,i;
char s[30],symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{

symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top])!=G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}

Department of CSE NMAMIT, Nitte 14


Data structures Lab Manual CS 2001-1

postfix[j]='\0';
puts(postfix);
}
void main()
{
char infix[25],postfix[30];
printf("Enter the expression");
gets(infix);
infix_postfix(infix,postfix);
}

6. Design, Develop and Implement a menu driven Program in C for the following
operations on Circular QUEUE of Characters (Array Implementation of Queue with
maximum size MAX)
• Insert an Element on to Circular QUEUE.
• Delete an Element from Circular QUEUE.
• Demonstrate Overflow and Underflow situations on Circular QUEUE d.
Display the status of Circular QUEUE.
• Exit
Support the program with appropriate functions for each of the above
operations
#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
int items[SIZE];
int front=-1,rear=-1;
int isFull()
{
if((front==rear+1)||(front==0 && rear==SIZE-1))
return 1;
return 0;
}
int isEmpty()
{
if(front==-1)
return 1;
return 0;
}
void insert()
{
int element;

Department of CSE NMAMIT, Nitte 15


Data structures Lab Manual CS 2001-1

if(isFull())
printf("Queue is full\n");
else
{
printf("Enter the element\n");
scanf("%d",&element);
if(front==-1)
front=0;
rear=(rear+1)%SIZE;
items[rear]=element;
}
}
void delete()
{
int element;
if(isEmpty())
printf("Queue is empty\n");
else
{
element=items[front];
if(front==rear) {front=-1; rear=-1;}
else
front=(front+1)%SIZE;
printf("Deleted element %d \n",element);
}
}
void display()
{
int i;
if(isEmpty())
printf("Queue is empty\n");
else
{
printf("Elements in the queue\n");
for(i=front; i!=rear; i=(i+1)%SIZE)
printf("%d ",items[i]);
printf("%d \n",items[i]);

}
}

Department of CSE NMAMIT, Nitte 16


Data structures Lab Manual CS 2001-1

int main()
{
int choice;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}

7. Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) for data of integers.
• Create a SLL of Data using front insertion.
• Display the status of SLL and count the number of nodes in it.
• Demonstration of stack
• Demonstration of Queue.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE X;
X=(NODE)malloc(sizeof(struct node));
return(X);
}
void freenode(NODE X)

Department of CSE NMAMIT, Nitte 17


Data structures Lab Manual CS 2001-1

{
free(X);
}
NODE insert_front(NODE first,int item)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return(temp);
}
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
return (temp);
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return (first);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return first;
}
temp=first;
temp=temp->link;
printf("Deleted data is %d\n",first->info);
freenode(first);
return(temp);
}
void display(NODE first)
{
NODE temp;
Int count=0;
if(first==NULL)

Department of CSE NMAMIT, Nitte 18


Data structures Lab Manual CS 2001-1

{
printf("List is empty\n");
return;
}
printf("Contents of the Linked list are\n");
temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->link;
count++;
}
printf("\n");
printf(“Number of nodes in the list are:%d”,count);
}
int main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the item\n");
scanf("%d",&item);
first=insert_front(first,item);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:first=insert_rear(first,item);
break;
case 5:exit(0);
}
}
}

8. Design, Develop and Implement a menu driven Program in C for the following
operations on Circular Singly Linked List (CSLL) for data of integers.
• Create a CSLL of data using front insertion.
• Perform Insertion and Deletion to the right of the given node.
• Perform Insertion and Deletion at end of CSLL.

Department of CSE NMAMIT, Nitte 19


Data structures Lab Manual CS 2001-1

• Display the status of CSLL and count the number of nodes in it.
• Exit

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node * NODE;
NODE insert_front(int item, NODE head)
{
NODE temp;
temp = getnode(); /* Create a node, insert the item */
temp->info = item;
first = head->link; /* Obtain the address of the first node */
head->link = temp; /* Insert at the front end */
temp->link = first;
return head; /* Return the header node */
}
NODE insert_rear(NODE first,int item)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=item;
if(first==NULL)
{
first=newnode;
first->link=newnode;
return first;
}
else
{
NODE temp=first;
while(temp->link!=first)
temp=temp->link;
temp->link=newnode;
newnode->link=first;
return first;
}
}
NODE delete_rear(NODE first)
{
if(first==NULL)
{

Department of CSE NMAMIT, Nitte 20


Data structures Lab Manual CS 2001-1

printf("List is empty\n");
return first;
}
else
{
int itm;
NODE temp=first;
if(first->link==first)
{
itm=first->data;
free(temp);
first=NULL;
}
else
{
NODE prev=NULL;
while(temp->link!=first)
{
prev=temp;
temp=temp->link;
}
prev->link=first;
itm=temp->data;
free(temp);
}
printf("Deleted data is %d\n",itm);
return first;
}
}
void display(NODE first)
{
Int count=0;
if(first==NULL)
{
printf("List is empty");
return;
}
else
{
NODE temp;
temp=first;
printf("Contents of the Circular Linked list are\n");
while(temp->link!=first)
{
printf("%d ",temp->data);
temp=temp->link;

Department of CSE NMAMIT, Nitte 21


Data structures Lab Manual CS 2001-1

count++;
}
printf("%d",temp->data);
printf("Number of nodes in the list are %d:",count);
}
}
int main()
{
int choice,val;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_rear(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3:display(first);
printf("\n");
break;
case 4: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 5:exit(0);
}
}
return 0;
}

9. Design, Develop and Implement a menu driven Program in C for the following
operations on Doubly Linked List (DLL) for data of integers.
• Create a DLL of data .
• Perform Insertion and Deletion at End of DLL.
• Perform Insertion and Deletion at Front of DLL.
• Display the status of DLL and count the number of nodes in it.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node

Department of CSE NMAMIT, Nitte 22


Data structures Lab Manual CS 2001-1

{
int info;
struct node *llink, *rlink;
};
typedef struct node * NODE;
NODE insert_front(NODE first,int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=val;
newnode->llink=NULL;
if(first==NULL)
{
first=newnode;
newnode->rlink=NULL;
return first;
}
else
{
newnode->rlink=first;
first->llink=newnode;
return newnode;
}
}
NODE insert_rear(int item, NODE first)
{
NODE temp, cur;
temp = getnode(); /*obtain a node from OS */
temp->info = item; /* Insert an item into new node */
temp->link = temp->rlink = NULL;
if (first == NULL) return temp; /* Insert a node for the first time */
/* Get the address of the first node */
cur = first;
/* Find the address of the last node */
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
/* Insert the node at the end */
cur->rlink = temp;
temp->llink = cur;
/* return address of the first node */
return first;
}

NODE delete_front(NODE first) {

Department of CSE NMAMIT, Nitte 23


Data structures Lab Manual CS 2001-1

NODE second;
if ( first == NULL ) /* Check for empty list */
{
printf("List is empty cannot delete\n");
return NULL; // We can replace NULL with first also
}
if (first ->rlink == NULL) /* Delete if there is only one node */
{
printf(“Item deleted = %d\n”, first->info);
free(first);
return NULL;
}
second = first->rlink; /* Get the address of second node */
second->llink = NULL; /* Make second node as the first node */
printf(“Item deleted = %d\n”, first->info);
free(first); /* Delete the first node */
return second;
}

NODE delete_rear(NODE first)


{
if(first==NULL)
{
printf("List is Empty\n");
return first;
}
int del;
NODE cur=first;
if(first->rlink==NULL)
{
del=first->info;
free(first);
first=NULL;
}
else
{
NODE prev=NULL;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
prev=cur->llink;
prev->rlink=NULL;
del=cur->info;
free(cur);

Department of CSE NMAMIT, Nitte 24


Data structures Lab Manual CS 2001-1

}
printf("Deleted data is %d\n",del);
return first;
}
void display(NODE first)
{
Int count=0;
if(first==NULL)
{
printf("List is Empty\n");
return;
}
else
{
NODE temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->rlink;
count++;
}
printf(“No of nodes in the list%d”,count);
printf("\n");
}
}

int main()
{
NODE first=NULL;
int choice,val;
while(1)
{
printf("Enter your choice\n1 Insert front\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3: display(first);
break;
case 4: printf("Enter the data\n");
scanf("%d",&val);

Department of CSE NMAMIT, Nitte 25


Data structures Lab Manual CS 2001-1

first=insert_rear(first,val);
break;
case 5: first=delete_front(first);
break;
case 6: exit(0);
}
}
return 0;
}

10. Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree (BST) of integers .
• Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2.
• Traverse the BST in Inorder, Preorder and Post Order.
• Search the BST for a given element (KEY) and report the appropriate
message.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef struct node * NODE;
NODE create_B_Tree()
{
NODE newnode;
int data=0;
newnode=NULL;
printf("Enter data ('0' if no data)");
scanf("%d",&data);
if(data)
{
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=data;
printf("\nLeft child of %d\n",newnode->info);
newnode->lchild=create_B_Tree();
printf("\nRight child of %d\n",newnode->info);
newnode->rchild=create_B_Tree();

Department of CSE NMAMIT, Nitte 26


Data structures Lab Manual CS 2001-1

}
return newnode;
}
NODE search(int item, NODE root)
{
NODE cur;
if (root == NULL) return NULL; /* empty tree */
cur = root;
while ( cur != NULL ) /* search for the item */
{
if (item == cur->info) return cur; /* If found return the node */
if ( item < cur->info )
cur = cur->llink; /* Search towards left */
else
cur = cur->rlink; /* Search towards right */
}
return NULL; /* Key not found */
}
void pre_order(NODE root)
{
if(root!=NULL)
{
printf("%d\n",root->info);
pre_order(root->lchild);
pre_order(root->rchild);
}
}
void post_order(NODE root)
{
if(root!=NULL)
{
post_order(root->lchild);
post_order(root->rchild);
printf("%d\n",root->info);
}
}
void in_order(NODE root)
{
if(root!=NULL)
{

Department of CSE NMAMIT, Nitte 27


Data structures Lab Manual CS 2001-1

in_order(root->lchild);
printf("%d\n",root->info);
in_order(root->rchild);
}
}
int main()
{
printf("Create binary tree, start from root\n");
NODE root;
root=create_B_Tree();
while(1)
{
printf("Select mode of traversal for displaying the binary tree\n");
printf("1 Pre-order\n2 Post-order\n3 In-order\n4 Exit\nChoice \n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1:pre_order(root);
printf("\n");
break;
case 2:post_order(root);
printf("\n");
break;
case 3:in_order(root);
printf("\n");
break;
case 4: printf("Enter the item to be searched\n");
scanf("%d",&item);
cur = search(item, root);
if (cur == NULL)
printf(“Item not found\n”);
else
printf(“Item found\n”);
break;
case 5:exit(0);
}
}
return 0;
}

Department of CSE NMAMIT, Nitte 28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy