Professional Documents
Culture Documents
Data Structure Rograms
Data Structure Rograms
#define MAXSIZE 10
struct st
{
int front,rear;
int queue[MAXSIZE];
};
struct st s;
int empty(void);
int full(void);
void add(void);
void delete(void);
void display(void);
void main()
{
char ans;
int ch;
s.front = 0;
s.rear = 0;
do
{
clrscr();
printf("********Queue Program**********\n");
printf("1. ADD\n");
printf("2. DELETE\n");
printf("3. DISPLAY\n");
printf("4. QUIT\n");
Page | 1
printf("Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("INVALID CHOICE!!!!!!!!!!!!!!!!\n");
break;
}
printf("\nWant To Go To The Main Menu[y/n]");
flushall();
ans = getch();
}
while(ans == 'y' ans == 'Y');
printf("\nPress Any Key To Continue\n");
getch();
}
int full(void)
{
if (s.rear == MAXSIZE)
return(1);
else
return(0);
}
int empty(void)
{
if (s.front == s.rear + 1)
return(1);
else
return(0);
}
Page | 2
void add(void)
{
char ch;
int x;
do
{
if(full() == 1)
{
printf("\n\nQueue Full\n");
break;
}
else
{
s.rear = s.rear + 1;
printf("\nEnter An Element to Be Added ");
scanf("%d",&x);
s.queue[s.rear] = x;
if(s.rear == 1) s.front ++;
}
printf("\nDo You Want to Add More Elements[y/n]:");
flushall();
ch = getch();
}
while(ch=='y' ch == 'Y');
}
void delete(void)
{
char ch;
do
{
if(empty() == 1)
{
printf("\n\nQueue Empty\n");
break;
}
else
{
printf("% d Has Been Deleted!",s.queue[s.front]);
s.front = s.front +1;
}
printf("\nWant to Delete More [y\n]");
flushall();
Page | 3
ch = getch();
}
while(ch=='y' ch == 'Y');
}
void display(void)
{
int i;
clrscr();
if(empty () == 1)
printf("\nQueue Empty!!");
else
{
printf("\nDisplaying Queue\n");
for(i = s.front;i printf("%d\n",s.queue[i]);
}
}
Page | 4
Program for Queue implementation through Array
#include <stdio.h>
#include<ctype.h>
# define MAXSIZE 200
int q[MAXSIZE];
int front, rear;
void main()
{
void add(int);
int del();
int will=1,i,num;
front =0;
rear = 0;
clrscr();
void add(int a)
Page | 5
{
if(rear>MAXSIZE)
{
printf("QUEUE FULL!!!");
return;
}
else
{
q[rear]=a;
rear++;
printf("Value of rear = %d and the value of front is %d",rear,front);
}
}
int del()
{
int a;
if(front == rear)
{
printf("QUEUE EMPTY!!!");
return(0);
}
else
{
a=q[front];
front++;
}
return(a);
}
Page | 6
/*Program of queue using array*/
# include
# define MAX 5
int queue_arr[MAX];
int rear = -1;
int front = -1;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
Page | 7
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/
del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */
display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of display() */
--------------------------------------------------------------------------------
Page | 8
Description : Single linked list implementation using different functions 1.INSERT A
NUMBER AT THE BEGINNING; 2.INSERT A NUMBER AT LAST 3.INSERT A
NUMBER AT A PARTICULAR LOCATION IN LIST 4.PRINT THE ELEMENTS IN
THE LIST 5.PRINT THE TOTAL NUMBER OF ELEMENTS IN THE LIST 6.DELETE
A NODE IN THE LINKED LIST: 7.REVERSE A LINKED LIST : 8.GET OUT OF
LINKED LIST (BYEE BYEE):
Code :
#include<stdio.h>
//#define NULL 0
/* STRUCTURE CONTANING A DATA PART AND A LINK PART */
struct node
{
int data;
struct node *next;
}*p;
delnode(int num)
{
struct node *temp, *m;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
{
p=temp->next;
free(temp);
return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}else
{
Page | 9
m=temp;
temp= temp->next;
}
}
printf("ELEMENT %d NOT FOUND!!!", num);
}/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */
addbeg(int num)
{
/* CREATING A NODE AND INSERTING VALUE TO IT */
else
{
Page | 10
temp->next=p;
p=temp;
}
}
Page | 11
r=r->next;
}
printf("<BR>");
}
//THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST
count()
{
struct node *n;
int c=0;
n=p;
while(n!=NULL)
{
n=n->next;
c++;
}
return(c);
}
//THIS FUNCTION REVERSES A LINKED LIST
reverse(struct node *q)
{
struct node *m, *n,*l,*s;
m=q;
n=NULL;
while(m!=NULL)
{
s=n;
n=m;
m=m->next;
n->next=s;
}
p=n;
}
main()
{
int i;
clrscr();
p=NULL;
while(1) /* this is an indefinite loop */
{
printf("1.INSERT A NUMBER AT BEGINNING”);
printf("2.INSERT A NUMBER AT LAST”);
printf("3.INSERT A NUMBER AT A PARTICULAR LOCATION IN LIST”);
printf("4.PRINT THE ELEMENTS IN THE LIST”);
printf("5.PRINT THE NUMBER OF ELEMENTS IN THE LIST”);
printf("6.DELETE A NODE IN THE LINKED LIST”);
printf("7.REVERSE A LINKED LIST”);
Page | 12
printf("8.GET OUT OF LINKED LIST (BYEE BYEE)”);
printf("PLEASE, ENTER THE NUMBER:");
switch(i)
{
case 1:
{
int num;
printf("PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
addbeg(num);
break;
}
case 2:
{
int num;
printf("PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
append(num);
break;
}
case 3:
{
int num, loc,k;
printf("PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
printf("PLEASE ENTER THE LOCATION NUMBER :-");
scanf("%d",&loc);
addafter(num,loc);
break;
} case 4:
{
struct node *n;
printf("THE ELEMENTS IN THE LIST ARE : <BR>);
display(n);
break;
}
case 5:
{
struct node *n;
display(n);
printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
break;
} case 6:
{
int num;
Page | 13
printf("PLEASE ENTER A NUMBER FROM THE LIST :");
scanf("%d",&num);
delnode(num);
break;
}
case 7:
{
reverse(p);
display(p);
break;
}
case 8:
{
exit();
}
}/* end if switch */
}/* end of while */
}/* end of main */
Page | 14
PROGRAM TO ADD TWO POLYNOMIALS
--------------------------------------------------------------------------------
/******************************************************************************
PROGRAM TO ADD TWO POLYNOMIALS
******************************************************************************/
#include<stdio.h>
#include<conio.h>
struct barbie
{
int coff;
int pow;
struct barbie *link;
}*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2;
typedef struct barbie bar;
int temp1,temp2;
void main()
{
void create(void);
void prnt(void);
void suml(void);
void sort(void);
clrscr();
Page | 15
{
printf("Unable to create memory.");
getch();
exit();
}
create();
clrscr();
//printing the elements of the lists
printf("The elements of the poly first are :");
ptr=start1;
prnt();
getch();
/*-----------------------------------------------------------------------------*/
void create()
{
char ch;
while(1)
{
printf("Enter the coff and pow :");
Page | 16
scanf("%d%d",&node->coff,&node->pow);
if (node->pow==0 )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}
while(ptr!=NULL )
{
if(i!=1)
printf("+ ");
printf(" %dx^%d ",ptr->coff,ptr->pow);
ptr=ptr->link;
i++;
}
//printf(" %d^%d",ptr->coff,ptr->pow);
}
/*---------------------------------------------------------------------------*/
void sort()
{
for(;ptr->coff!=NULL;ptr=ptr->link)
Page | 17
for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)
{
if(ptr->pow>ptr2->pow)
{
temp1=ptr->coff;
temp2=ptr->pow;
ptr->coff=ptr2->coff;
ptr->pow=ptr2->pow;
ptr2->coff=temp1;
ptr2->pow=temp2;
}
}
}
/*---------------------------------------------------------------------------*/
void suml()
{
node=(bar *)malloc (sizeof(bar));
start3=node;
ptr1=start1;
ptr2=start2;
Page | 18
}
Page | 19
Binary Search C Program
#include<stdio.h> //header file
#include<process.h> //header file
#include<conio.h> //header file
int s;
void bin(int a[],int,int);
void main()
{
int l,u,i,a[10];
clrscr();
l=0;
u=9;
for(i=0;i<10;i++)
{
printf("enter the %dth number",i);
scanf("%d",&a[i]);
}
printf("enter a element that has to be searched");
scanf("%d",&s);
bin(a,l,u);
getch();
}
void bin(int a[],int l,int u) //function named bin( )
{
int m;
m=(l+u)/2;
if(s<a[m])
{
if(u>=l)
{
u=m-1;
bin(a,l,u);
}
else
{
printf("no. is not present in the list");
getch();
exit(0);
}
}
else if(s>a[m])
{
if(l<=u)
{
l=m+1;
bin(a,l,u);
}
else
{
printf("the no. is not present in the list");
Page | 20
getch();
exit(0);
}
}
else if(s==a[m])
{
printf("element exists at %d",m);
getch();
exit(0);
}
}
Page | 21
Infix To Prefix Conversion
--------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 15
#define true 1
#define false 0
/*Structure Declaration*/
typedef struct
{
char data[MAX];
char top;
}STK;
/*Function Declarations*/
void input(char str[]);
void intopre(char str1[],char pre[]);
void intopost(char str1[],char post[]);
int isoperand(char sym);
int prcd(char sym);
void push(STK *s1,char elem);
int pop(STK *s1);
int empty(STK *s2);
int full(STK *s2);
void dis(char str[]);
void main()
{
STK s;
int cs,ans;
char str[MAX],pre[MAX],post[MAX];
clrscr();
do /*Using Do-while Loop*/
Page | 22
{
clrscr();
printf("-----Program for Expressions-----");
printf("Input The String:");
printf("MENU”);
printf("1.Infix to Prefix”);
printf("2.Infix to Postfix");
printf("3.Exit");
cs=getche();
getch();
}
/**************************************************/
/*To Input String*/
/**************************************************/
void input(char str)
{
printf("Enter the Infix String:");
scanf("%s",str);
}
/**************************************************/
/*To Covert Infix To Prefix*/
/**************************************************/
void intopre(STK s1,char str1[],char pre[])
Page | 23
{
int len,flag;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
char elem;
}
}
/**************************************************/
/*To Convert Infix To Postfix*/
/**************************************************/
void intopost(STK s1,char str1[],char post[])
{
int len;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
Page | 24
}
/**************************************************/
/*To Check For Operand*/
/**************************************************/
int isoperand(char sym)
{
if('A'<sym<'Z'||'a'<sym<'z')
return(true);
return(false);
}
/**************************************************/
/*To Check The Precedence*/
/**************************************************/
int prcd(char sym)
{
/**************************************************/
/*To Display String*/
/**************************************************/
void dis(char str[])
{
/******************************************/
/*Push Function Definition*/
/******************************************/
void push(STK *s1,char elem)
{
if(!full(s1))
{
s1->top++; /*Incrementing top*/
s1->data[s1->top]=elem; /*Storing element*/
}
else
printf("
Stack is Full!");
}
Page | 25
/******************************************/
/*Full Function Definition*/
/******************************************/
int full(STK *s2)
{
if(s2->top==MAX) /*Condition for Full*/
return(true);
return(false);
}
/******************************************/
/*Pop Function Definition*/
/******************************************/
int pop(STK *s1)
{
char elem;
if(!empty(s1))
{
elem=s1->data[s1->top]; /*Storing top stack element in elem*/
s1->top--; /*Decrementing top*/
return(elem);
}
return(false);
}
/******************************************/
/*Empty Function Definition*/
/******************************************/
int empty(STK *s2)
{
if(s2->top==-1) /*Condition For Empty*/
return(true);
return(false);
}
Page | 26
Creation of Binary search tree
#include<stdio.h>
#include<conio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
struct tree *create();
void preorder(struct tree *);
void inorder(struct tree *);
void postorder(struct tree *);
struct tree *create()
{
struct tree *p,*root;
int m,x;
char s;
root=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the main root");
scanf("%d",&m);
root->data=m;
root->left=NULL;
root->right=NULL;
printf("\nenter n to stop creation of the binary search tree");
fflush(stdin);
scanf("%c",&s);
while(s!='n')
{
p=root;
printf("\nenter the value of the newnode");
fflush(stdin);
scanf("%d",&x);
while(1)
{
if(x<p->data)
{
if(p->left==NULL)
{
p->left=(struct tree *)malloc(sizeof(struct tree));
p=p->left;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->left;
}
else
Page | 27
{
if(p->right==NULL)
{
p->right=(struct tree *)malloc(sizeof(struct tree));
p=p->right;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->right;
}
}
printf("\nwant to continue");
fflush(stdin);
scanf("%c",&s);
}
return(root);
}
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\t%d",p->data);
inorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t%d",p->data);
}
}
void main()
{
int h;
Page | 28
struct tree *root;
clrscr();
while(1)
{
printf("\nenter 1. for creation of the binary search tree");
printf("\nenter 2. for preorder traversal");
printf("\nenter 3. for inorder traversal");
printf("\nenter 4. for postorder traversal");
printf("\nenter 5. for exit");
printf("\nenter your choice");
scanf("%d",&h);
switch(h)
{
case 1:
root=create();
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default:
printf("\nentered a wrong choice");
}
}
}
Page | 29
Program to demonstrate linked list operations.
--------------------------------------------------------------------------------
# include<stdio.h>
# include<conio.h>
# include<malloc.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int a=111,b=2,c=3,will,wish,num;
struct node *ptr,*ptr2,*result,*temp;
void add(struct node **,int );
struct node * search(struct node *);
void display(struct node *);
void invert(struct node *);
void del(struct node *,int);
struct node * concat(struct node *,struct node *);
ptr=NULL;
ptr2=NULL;
result=NULL; //result for storing the result of concatenation
clrscr();
will=1;
while(will==1)
{
printf("Main Menu
1. Add element
2.Delete element
3.Search element
4Linked List concatenation
5.Invert linked list
6. Display elements
Please enter the choice");
scanf("%d",&wish);
switch(wish)
{
Page | 30
case 1:
printf("Enter the element you want to add ");
scanf("%d",&num);
add(&ptr,num);
display(ptr);
break;
case 2:
printf("Enter the element to delete ");
scanf("%d",&num);
del(ptr,num);
break;
case 3:
printf("Now demonstrating search ");
temp = search(ptr);
printf("Address of first occurence is %u ",temp);
break;
case 4:
/* Inputs given internally for demo only */
printf(" Now demonstrating linked list concatenation
Press any key to continue...");
add(&ptr2,2);
add(&ptr2,4);
add(&ptr2,6);
getch();
printf("Displaying second Linked List”);
display(ptr2);
getch();
result = concat(ptr,ptr2);
clrscr();
printf("Now Displaying the result of concatenation");
display(result);
getch();
break;
case 5:
Page | 31
}
printf("DO you want to continue ( press 1 for yes ");
scanf("%d",&will);
} //end of while
}
while(pt!=NULL)
{
printf("Data : %d",pt->data);
printf("Link : %d",pt->link);
pt=pt->link;
}
}
Page | 32
struct node *p,*q,*r;
p=ptr;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
ptr = q;
display(ptr);
}
if (p==NULL)
r=q;
if (q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}
Page | 33
struct node * search(struct node *p)
{
struct node *temp;
int num;
temp = p;
printf("Enter the data that you want to search ");
scanf("%d",&num);
printf("Link of temp %u", temp->link);
while(temp->link!=NULL)
{
printf("In while ");
if(temp->data == num)
return(temp);
temp=temp->link;
}
return(NULL);
}
Page | 34
return;
}
} //end of outer if
x=temp;
temp=temp->link;
} //end of while
printf("No such entry to delete ");
} //end of fn.
Page | 35
Threaded Binary Tree C program
#include<stdio.h>
#include<conio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
int i=0,a[20],c=0;
struct tree *s,*t,*header;
struct tree *create();
void inorder(struct tree *);
void inorder1(struct tree *);
void header_node(struct tree *p);
void display(struct tree *p);
struct tree *create()
{
struct tree *p,*root;
int m,x;
char s;
root=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the main root");
scanf("%d",&m);
root->data=m;
root->left=NULL;
root->right=NULL;
printf("\nenter n to discontinue creation of the binary search tree");
fflush(stdin);
scanf("%c",&s);
while(s!='n')
{
p=root;
printf("\nenter the value of the newnode");
fflush(stdin);
scanf("%d",&x);
while(1)
{
if(x<p->data)
{
if(p->left==NULL)
{
p->left=(struct tree *)malloc(sizeof(struct tree));
p=p->left;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
Page | 36
p=p->left;
}
else
{
if(p->right==NULL)
{
p->right=(struct tree *)malloc(sizeof(struct tree));
p=p->right;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->right;
}
}
printf("\nwant to continue");
fflush(stdin);
scanf("%c",&s);
}
return(root);
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
a[i]=p->data;
i++;
inorder(p->right);
}
}
void inorder1(struct tree *p)
{
if(p!=NULL)
{
inorder1(p->left);
if(a[c]==p->data)
s=p;
else if(a[c+1]==p->data)
{
t=p;
if(s->right==NULL)
s->right=t;
else if(t->left==NULL)
t->left=s;
s=t;
c++;
}
Page | 37
inorder1(p->right);
}
}
void header_node(struct tree *p)
{
struct tree *top;
int m;
top=p;
header=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the header node");
scanf("%d",&m);
header->data=m;
while(p->right!=NULL)
p=p->right;
p->right=header;
p=top;
while(p->left!=NULL)
p=p->left;
p->left=header;
header->left=top;
header->right=top;
}
void display(struct tree *p)
{
char a;
while(p->right!=header)
p=p->right;
printf("\npress n to quit display");
scanf("%c",&a);
while(a!='n')
{
while(p->left!=header)
{
printf("\t%d",p->data);
p=p->left;
}
printf("\t%d\t",header->data);
while(p->right!=header)
{
printf("\t%d",p->data);
p=p->right;
}
printf("\npress n to discontinue");
scanf("%c",&a);
}
}
void main()
{
int h;
struct tree *root;
Page | 38
clrscr();
while(1)
{
printf("\nenter 1. for creation of the binary search tree");
printf("\nenter 2. for creation of a binary threaded tree");
printf("\nenter 3. for display");
printf("\nenter 4. for exit");
printf("\nenter your choice");
scanf("%d",&h);
switch(h)
{
case 1:
root=create();
break;
case 2:
inorder(root);
inorder1(root);
header_node(root);
break;
case 3:
display(root);
break;
case 4:
exit(0);
default:
printf("\nentered a wrong choice");
}
}
}
Page | 39
Simple Linked List
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct linklist
{
int info;
struct linklist *next;
};
struct linklist *start=NULL;
int x=0,c=0;
void create_node();
void display();
void insertion_info();
void split();
void deletion_before();
void deletion_after();
void deletion_info();
void insertion_before();
void insertion_after();
void insertion_after()
{
struct linklist *newnode,*p,*t;
int data,b,i;
if(start!=NULL)
{
printf("\nthe total no. of nodes are= %d",c);
printf("\nenter the no. of node after which the new node required to be inserted");
scanf("%d",&b);
if(b>c)
{
printf("wrong choice entered");
goto z;
}
}
printf("\nenter the information of the newnode");
scanf("%d",&data);
newnode=(struct linklist *)malloc(sizeof(struct linklist));
c++;
newnode->info=data;
p=start;
if(start==NULL)
{
newnode->next=NULL;
start=newnode;
}
else
{
for(i=1;i<=b;i++)
{
Page | 40
t=p;
p=p->next;
}
t->next=newnode;
newnode->next=p;
}
z:}
void create_node()
{
int m,i;
struct linklist *newnode;
printf("\nenter the no. of nodes to be inserted");
scanf("%d",&x);
if(x<1)
printf("\nentered a wrong no.");
else
{
for(i=1;i<=x;i++)
{
printf("\nenter the information of the %d node",i);
fflush(stdin);
scanf("%d",&m);
newnode=(struct linklist *)malloc(sizeof(struct linklist));
newnode->info=m;
newnode->next=start;
start=newnode;
}
c=c+x;
}
}
void display()
{
struct linklist *p;
p=start;
if(p==NULL)
printf("link list is empty");
else
{
while(p!=NULL)
{
printf("\t%d",p->info);
p=p->next;
}
}
}
void insertion_before()
{
struct linklist *p,*t,*newnode;
int n,s,i;
if(start!=NULL )
Page | 41
{
printf("\nthe total no. of nodes are= %d",c);
printf("\nenter the no. before which the node has to be inserted");
scanf("%d",&n);
if(n>c)
{
printf("wrong choice");
goto x;
}
}
printf("\nenter the information of the inserted node");
scanf("%d",&s);
p=start;
newnode=(struct linklist*)malloc(sizeof(struct linklist));
c++;
newnode->info=s;
if(start==NULL || n==1)
{
newnode->next=start;
start=newnode;
}
else
{
for(i=1;i<n;i++)
{
t=p;
p=p->next;
}
t->next=newnode;
newnode->next=p;
}
x:}
void insertion_info()
{
struct linklist *newnode,*p,*t;
int i,j,a,s,q,m;
printf("\nenter the information to be inserted");
scanf("%d",&s);
newnode=(struct linklist*)malloc(sizeof(struct linklist));
c++;
newnode->info=s;
t=start;
p=t->next;
if(start==NULL || t->info>s)
{
newnode->next=start;
start=newnode;
}
else
{
Page | 42
for(i=1;i<c;i++)
{
if(i!=1)
{
t=t->next;
p=t->next;
}
for(j=i+1;j<=c;j++)
{
q=t->info;
m=p->info;
if(q>m)
{
a=q;
q=m;
m=a;
}
p=p->next;
}
}
p=start;
while(p->info<s)
{
t=p;
p=p->next;
if(p==NULL)
{
t->next=newnode;
newnode->next=p;
break;
}
}
if(p!=NULL)
{
t->next=newnode;
newnode->next=p;
}
}
}
void split()
{
struct linklist *v,*t;
int n,i;
v=start;
printf("\nenter the node no. after which the linked list is to be splitted");
scanf("%d",&n);
if(start==NULL)
printf("\nthe linked list is empty");
else
{
Page | 43
printf("\nthe splitted linked lists are->");
for(i=1;i<=n;i++)
{
t=v;
v=v->next;
}
t->next=NULL;
display();
printf("\n\t");
while(v!=NULL)
{
printf("\t%d",v->info);
v=v->next;
}
}
}
void deletion_before()
{
struct linklist *p,*t;
int j,i;
p=start;
if(start!=NULL)
{
printf("\nenter the no. of node before which the node has to be deleted");
scanf("%d",&j);
}
if(start==NULL)
printf("\nlinked list is empty");
else if(j==1)
printf("\nwrong choice");
else if(j==2)
{
start=p->next;
free(p);
}
else
{
for(i=1;i<j-1;i++)
{
t=p;
p=p->next;
}
t->next=p->next;
free(p);
}
}
void deletion_after()
{
struct linklist *p,*t;
int y,i;
Page | 44
p=start;
if(start!=NULL)
{
printf("\nenter the no. of node before which the node has to be deleted");
scanf("%d",&y);
}
if(start==NULL)
printf("\nlinked list is empty");
else
{
for(i=1;i<=y;i++)
{
t=p;
p=p->next;
}
t->next=p->next;
free(p);
}
}
void deletion_info()
{
int s;
struct linklist *p,*t;
p=start;
if(p==NULL)
printf("\nlinked list is empty");
else
{
printf("\nenter the information to be deleted");
scanf("%d",&s);
while(p->info !=s)
{
if(p==NULL)
{
printf("\ninformation is not present in the linked list");
break;
}
t=p;
p=p->next;
}
if(p==start)
{
start=p->next;
free(p);
}
t->next=p->next;
free(p);
}
}
void main()
Page | 45
{
int ch;
clrscr();
while(1)
{
printf("\nenter 1. for creation of node");
printf("\nenter 2. for display of linked list");
printf("\nenter 3. for insertion before a node");
printf("\nenter 4. for insertion after a node");
printf("\nenter 5. for insertion according to information of the node");
printf("\nenter 6. for splitting the linked list");
printf("\nenter 7. for deletion before a node");
printf("\nenter 8. for deletion after a node");
printf("\nenter 9. for deletion according to information");
printf("\nenter 10. for exit");
printf("\nenter your choice");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
create_node();
break;
case 2:
display();
break;
case 3:
insertion_before();
break;
case 4:
insertion_after();
break;
case 5:
insertion_info();
break;
case 6:
split();
break;
case 7:
deletion_before();
break;
case 8:
deletion_after();
break;
case 9:
deletion_info();
break;
case 10:
exit(0);
default:
Page | 46
printf("\nentered a wrong choice");
}
}
getch();
}
Page | 47
Program for demonstration of Tree Operations - INSERTION . INORDER .
PREORDER . POSTORDER TRAVERSAL
# include<stdio.h>
# include <conio.h>
# include <malloc.h>
struct node
{
struct node *left;
int data;
struct node *right;
} ;
void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();
/* Getting Input */
for(i=0;i<will;i++)
{
printf("Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}
getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
Page | 48
printf("POSTORDER TRAVERSAL");
postorder(ptr);
getch();
}
Page | 49
return;
}
Page | 50