Remember
me?
RegisterForgot Your Password?

Category Archives: DATA STRUCTURES

Program to create a binary tree and perform recursive and non-recursive traversals

C++ program to create binary tree and perform recursive and non-recursive traversals.

download :  SOURCE  CODE      OUTPUT

Description :

binary tree is a data structure in which each node has at most two child nodes called  as “left” and “right”. The tree has a header, which contains a link to a designated node (the root node). The root node has no parent.

Code :

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<alloc.h>

typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;

class tree
{
public:
node *root;

tree()
{
root=NULL;
}

C Program to check well parenthesis of an infix expression using stack

Program to read a parenthesized infix expression from user & check whether it is well parenthesized or not using static implementation of stack

download :SOURCE CODE

Description :

The following code uses stack operations like push,pop,init,isfull,isempty .User is

asked to enter an infix expression.Every character in the expression is compared

using a switch case to find whether every opening

bracket has a closed bracked associated woth it.This is called as parathesis check.

Code :

#include<stdio.h>
#include<conio.h>
#define MAX 100

‘C’ program to remove first node of the list and insert it at the end of the list.

download : Source Code

Description :The code for removing first node from the list and insert
it at the last

The code consists of three modules-

-create :It is used to create the linked list

-display :It displays the list

-remove:It takes the start node of the list a parameter.The start is stored in a temporary

Binary tree:Number of nodes,Degree of tree,Leaf nodes,Interior nodes

download : source code

Description :C program to display total number of nodes of binary tree,Degree,Number of leaf nodes,Display interior nodes .

The following code consist of five modules-

1.no of leaf nodes
2.degree of tree
3.leaf nodes
4.interior nodes
5.children of parent

C Program to read ‘n’ integers and store them in binary search tree structure. Display mirror image of tree.(using recursive function)

download :  Source Code

Description :C Program to display mirror image of binary tree using

inorder.

The code consists of three modules -

1)create-This function creates a binary tree

2)mirror-Its a recursive function

3)inorder-It is used to display the tree in a left->root->right fashion.

create mirror inorder

Code :

Static implementation of circular queue for integers

A menu driven program in ‘C’ for static implementation of Circular Queue for integers.  The menu includes

-          Insert

-          Delete

-          Display

-          Exit

DOWNLOAD  SOURCE CODE  OUTPUT

CODE:
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
} *front, *rear;
void enqueue(int elt);
int dequeue();
void display();
void main()
{
int ch;
int elt;
clrscr();
rear = NULL;
front = NULL;
do
{
printf(“\n************ Menu ***************”);
printf(“\nEnter:\n1->Insert\n2->Delete\n3->Display\n4->Exit\n”);
printf(“Enter your choice :: “);
scanf(“%d”, &ch);
switch (ch)
{
case 1:
printf(“Enter the integer no\n “);
scanf(“%d”,&elt);
enqueue(elt);
break;
case 2:
elt = dequeue();
printf(“The deleted element = %d\n”, elt);
break;
case 3:
display();
break;
default:
printf(“~~~Exit~~~”);
getch();
exit(0);
break;
}
}while(ch!=4);
}
void enqueue(int elt)
{
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
p->info = elt;
p->next = NULL;
if (rear == NULL || front == NULL)
front = p;
else
rear->next = p;
rear = p;
}
int dequeue()
{
struct node *p;
int elt;
if (front == NULL || rear == NULL)
{
printf(“\nUnder Flow”);
getch();
exit(0);
}
else
{
p = front;
elt = p->info;
front = front->next;
free(p);
}
return (elt);
}
void display()
{
struct node *t;
t = front;
while (front == NULL || rear == NULL)
{
printf(“\nQueue is empty”);
getch();
exit(0);
}
while (t != NULL)
{
printf(“->%d”,t->info);
t = t->next;
}
}

OUTPUT:
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter the integer no
2

************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter the integer no
5

************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter the integer no
7

************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 3
->2->5->7
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 2
The deleted element = 2

************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice ::3
->5->7
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice ::4

Palindrome of string using static implementation of stack

A ‘C’ program to read a string and check whether string is palindrome or not. (using static implementation of stack)

DOWNLOAD  SOURCE CODE  OUTPUT

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 10

typedef struct
{
int items[SIZE];
int top;

}STACK;
void push();
int pop();
void display();
int isoverflow();
int isempty();

int main()
{
STACK s;
char str[100];
int i, flag;

s.top = -1;

printf(“\nEnter a string: “);
gets(str);

for(i=0;str[i]!=’\0′; i++)
push(&s, str[i]);

flag = 1;
for(i=0;str[i]!=’\0′;i++)
{
if(str[i] != pop(&s))
{
flag = 0;
break;
}
}

if(flag == 1)
printf(“\nEntered string is palindrome\n”);
else
printf(“\nEntered string is not a palindrome\n”);
getch();

}

void push(STACK *p, int element)
{
if(isoverflow(p->top))
{
printf(“\nStack is overflow”);
}
else
{
(p->top)++;
p->items[p->top] = element;
}
}

int pop(STACK *p)
{
if(isempty(p->top))
{
printf(“\nStack is underflow”);
}
else
{
return (p->items[(p->top)--]);
}
}

void display(STACK s)
{
int i;
if(isempty(s.top))
{
printf(“\nStack is empty”);
}
else
{
for(i=s.top; i>=0; i–)
{
printf(“\n%d”, s.items[i]);
}
}
}

//Function to check stack overflow condition
int isoverflow(int top)
{
if(top == SIZE – 1)
return (1);
else
return (0);
}

//Function to check stack empty condition
int isempty(int top)
{
if(top == -1)
return (1);
else
return (0);
}

OUTPUT:
Enter a string: nitin

Entered string is palindrome

Enter a string: mad

Entered string is not a palindrome

 

 

 

Counting no of nodes in linked list

A ‘C’ program to create a singly linked list and count total number of nodes in it

DOWNLOAD  SOURCE CODE  OUTPUT

CODE:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
} ;

void add ( struct node **, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void main( )
{
struct node *first;
int n,i,num;
char ch=’y';
clrscr();
first = NULL ; /* empty linked lists */

do
{
printf(“\nenter the data :”);
scanf(“%d”,&num);
add ( &first, num) ;
printf(“\nDo you want to continue(y/n)”);
flushall();
scanf(“%c”,&ch);
}while(ch==’y');
printf ( “\n—–The linked list is —–\n : “) ;
display ( first ) ;
printf ( “\nNo. of elements in Linked List : %d”, count ( first ) ) ;
getch();
}

/* adds node to an ascending order linked list */
void add ( struct node **q, int num )
{
struct node *r, *temp = *q ;

r = malloc ( sizeof ( struct node ) ) ;
r-> data = num ;

/* if list is empty or if new node is to be inserted before the first node */
if ( *q == NULL || ( *q ) -> data > num )
{
*q = r ;
( *q ) -> link = temp ;
}
else
{
/* traverse the entire linked list to search the position to insert the new node */
while ( temp != NULL )
{
if ( temp -> data < num && ( temp -> link -> data > num ||
temp -> link == NULL ))
{
r -> link = temp -> link ;
temp -> link = r ;
return ;
}
temp = temp -> link ; /*go to next node */
}

r -> link = NULL ;
temp -> link = r ;
}
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
printf ( “\n” ) ;

/* traverse the entire linked list */
while ( q != NULL )
{
printf ( “%d “, q -> data ) ;
q = q -> link ;
}
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */
while ( q != NULL )
{
q = q -> link ;
c++ ;
}

return c ;
}

 OUTPUT:
enter the data :5

Do you want to continue(y/n)y

enter the data :9

Do you want to continue(y/n)y

enter the data :10

Do you want to continue(y/n)y

enter the data :13

Do you want to continue(y/n)n

—–The linked list is —–
:
5 9 10 13
No. of elements in Linked List : 4