Você está na página 1de 48

DATA STRUCTURES

INDEX
SR.NO

CONTENT

Write a program create a Stack& addelements into the


stack,delete elementsfrom the same stack.
Write a program to create a Stack.
Write a program to create a Queue, addelements into
the queue & delete elementsfrom the same queue.
Write a program to create a Queue.
Write a program to traverse binary tree in :
1 ) In-order form
2 )Pre-order form
3 )Post-order form
Write a program to count the leaf nodes in binary tree.
Write a program to create a SingleLinked
List, Insert & Delete elements from it.
Write a program to create a singlelinked list
Write a program to implement Bubble Sort
Write a program to implement Heap Sort
Write a program to implement Insertion
Sort
Write a program to sort the given numbers by using
Bubble sort technique.
Write a program to join circular linked lists.
Write a program to join single linked lists.

2
3
4
5

6
7
8
9
10
11
12
13
14

SIGNATURE

Practical No. 1
#include<stdo.h>
#include<conio.h>
int stack[50], item;
int ch, n=0, top, i;
void main()
{
void create();
void add();
void deletelast();
void display();
do
{
clrscr();
printf("\n\t Stack Operation \n\t");
printf("\n-------------------------\n\n");
printf("\n1. Create \n\n 2. Add \n\n 3. Delete \n\n 5. Exit\n\n");
printf("\nEnter Choice");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter Maximum size stack:");
scanf("%d",&n);
}
switch(ch)
{
case 1: create();getch();break;
case 2: add();getch();break;
case 3: deletelast();getch();break;
case 4: display();getch();break;
case 5: exit();getch();break;
default: printf("Invalid Choice"); break;
}
getch();
}
while(ch!=5);
}
add()
{
struct stack *new,*temp;
int i=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
i++;
}

if(i==5)
{
printf("Stack Overflow");
getch();
}
else
{
new=(struct stack *)malloc(sizeof(struct stack));
printf("Enter value for stack: ");
scanf("%d",&new->info);
new->link=start;
start=new;
}
getch();
}
display()
{
struct stack *temp;
printf("\n****Stack Values****\n");
for(temp=start;temp!=NULL;temp=temp->link)
{
printf("%d\n",temp->info);
}
getch();
}
deletelast()
{
struct stack *temp,*temp2;
int i=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
i++;
}
if(i==0)
{
printf("Underflow");
getch();
}
else
{
temp2=start->link;
start=temp2;
printf("\n***The value has been deleted***\n");
}
}

Output:

Practical No. 2
#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct stack
{
int data;
struct stack *next;
};

struct stack *pop(struct stack *top);


struct stack *push(struct stack *top);

void main()
{
struct stack *top;
int reply,option,data;
clrscr();
top=NULL;
do
{
printf("\nStack Operations: \n");
printf(" --------------------- ");
printf("\n 1.push \n 2.pop \n 3.exit\n\n");
printf("Select Your option:");
scanf(" %d" ,&option);
switch(option)
{

case 1:top=push(top);
break;
case 2:top=pop(top);
break;
case 3: exit(0);
break;
}
}
while(1);
}
struct stack *push(struct stack *top)
{
struct stack *c;
c=(struct stack*)malloc(sizeof(struct stack));
if(c==NULL)
{
printf("\nlnsufficient Memory! ");
return(top);
}
printf("\nEnter data: ");
scanf("%d",&c->data);
c->next=NULL;
printf("\n\tTha Data item %d has been pushed into the stack \n\n",c->data);
if(top==NULL)
top=c;
else
{
c->next=top;
top=c;
}

return(top);
}
struct stack *pop(struct stack *top)
{
struct stack *c;
if(top== NULL)
{
printf("stack is empty");
return(top);
}
printf("\n\t Popped data is: %d\n\n",top->data);
c=top;
top=top->next;
free(c);
return(top);
}

Output:

Practical No. 3
#include<stdio.h>
#include<conio.h>
#include<alloc.h>

int queue[50],item;
int ch,n=0,front,rear,i;
void main()
{
void create();
void add();
void deletelast();
void display();
do
{
clrscr();
printf("Queue operations");
printf("\n ------------------- \n");
printf("\n1:Create\n2.Add\n3.Delete last\n4.Display\n5.Exit");
printf("\n Enter Your Choice:");
scanf("%d",&ch);
if(ch==1)
{
printf("\nEnter maximum size of queue you wish to have:");
scanf("%d",&n);
}
switch(ch)
{
case 1:create();getch();break;
case 2:add();getch();break;

case 3:deletelast();getch();break;
case 4:display();getch();break;
case 5:exit(0);getch();break;
default:
printf("Invalid choice! Try again ");
break;
}
}
while (ch!=5);
getch();
}
void create()
{
printf("\nCreating a Queue ");
printf("\n---------------------- \n ");
front=0,rear=0;
printf("\nQueue created");
return;
}
void add()
{
if(rear>=n){
printf("\nQueue is full !");
getch();
return;
} else {
printf("\nAdding eleme_ntinto a queue");
printf("\n------------------------- \n");
printf("Enter item to be inserted: ");
scanf("%d",&item);

rear++;
queue[rear]=item;
if(front==0)
front=1 ;
printf("\nItem %d has been inserted into queue" ,item);
printf("\nItems of queue are: ");
for(i=front;i<=rear;i++ )
{
printf(" %d",queue[i]);
}
return;
}
}
void deletelast()
{
printf("\nDeleting element from queue");
printf("\n----------------------------- \n");
if(front==rear){
printf("\nQueue is empty! ");
getch();
return;
}
front++;
item=queue[front];
printf("\nItem %d has been deleted from queue",item);
printf("\nItems of queue are:");
for(i=front;i<=rear;i++)
{
printf(" %d",queue[i]);
}

return;
}
void display(){
printf("\nDisplay all elements of Queue");
printf("\nItems of queue are:");
for(i=front;i<=rear;i++)
{
printf(" %d",queue[i]);
}
return;
}

Output:

Practical No. 4
#include<stdio.h>
#include<conio.h>
#define max 3

int queue[max],data;
int front,rear,reply,option,x;
void main()
{
clrscr();
front=rear=-1;
do {
printf("\nQueue Operations : \n1.Insert into Queue \n2.Delete from Queue\n3.Exit");
printf("\nEnter Your choice: ");
scanf("%d",&option);
switch(option)
{
case 1: printf("\nEnter data to insert:");
scanf("%d", &data);
reply=insertq(queue,&rear,&data);
if(reply==-1)
printf("\nQueue is full! ");
else
printf("\nData %d inserted into Queue successfully !",data);
break;
case 2: reply=deleteq(queue,&rear,&data);
if(reply==-1)
printf("\nQueue is Empty! ");
else
printf("\nData %d is deleted ....",data);

break;
case 3:exit(0);
default: printf("\nInvalide choice ..Try again ");
break;
}
} while(1);
}
int insertq(int queue[max],int *rear, int *data)
{
if(*rear == max-1)
return (-1);
else //doubt about brackets
*rear=*rear+1;
queue[*rear]=*data;
return (1);

}
int deleteq(int queue[max],int *front, int *rear,int *data)
{
if(*front==*rear)
return (-1);
else//doubt about brackets
(*front)++;
*data=queue[*front];
return(1);

Output:

Practical No. 5
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct treerec
{
struct treerec *lchild;
struct treerec *rchild;
char data;
};

struct treerec *item;


struct treerec *rdata[50];
void main()
{
int i,n;
clrscr();
printf("\nEnter no of data-items to be entered in tree: ");
scanf("%d" ,&n);
printf("\nEnter data-items: ");
for(i=1;i<=n;i++)
{
rdata[i]=(struct treerec *)malloc(sizeof(struct treerec));
scanf("\r%c",&rdata[i]->data);
rdata[i]->lchild=NULL;
rdata[i]->rchild= NULL;
}
for(i=1;i<=n;i++)
{

rdata[i]->lchild=rdata[2*i];
rdata[i]->rchild=rdata[2*i+1];
}
item=rdata[1];
printf("\nGiven Data in Tree\n");
for(i=1 ;i<=n;i++)
{
printf("%c " ,rdata[i]->data);
}

printf("\n\nInorder tree traversal - \t");


inorder(item);
printf("\n\nPreorder tree traversal - \t");
preorder(item);
printf("\n\nPostorder tree traversal - \t");
postorder(item);
getch();
}
inorder(cnode)
struct treerec *cnode;
{
if(cnode != NULL)
{
inorder(cnode->lchild);
if(cnode->data != '_')
{
printf("%c" ,cnode->data);
}
inorder(cnode->rchild);
}

return (1);
}
preorder(cnode)
struct treerec *cnode;
{
if(cnode != NULL)
{
if(cnode->data != '_')
{
printf("%c" ,cnode->data);
}
preorder(cnode->lchild);
preorder(cnode->rchild);
}
return (1);
}
postorder(cnode)
struct treerec *cnode;
{
if(cnode != NULL)
{
postorder(cnode->lchild);
postorder(cnode->rchild);
if(cnode->data != '_')
{
printf("%c" ,cnode->data);
}
}
return (1);
}

Output:

Practical No. 6
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>

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

unsigned int getleafcount(struct node* node){


if(node==NULL)
return 0;
if(node->left==NULL && node->right==NULL)
return 1;
else
return (getleafcount(node->left)+getleafcount(node->right));
}

struct node*newnode(int data){


struct node*node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->left= NULL;
node->right= NULL;
return(node);
}
int main(){

struct node *root=newnode(1);


clrscr();
root->left=newnode(2);
root->right=newnode(3);
root->left->left=newnode(4);
root->left->right=newnode(5);
printf("Leaf count of the tree is %d",getleafcount(root));
getch();
return 0;
}

Output:

Practical No. 7
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct studinfo{
int data;
struct studinfo *next;
};
struct studinfo *start=NULL,*ptr,*disp;

void insertnode();
void deletenode();
void display();
void main() {
int ch;
clrscr();
do{
printf("\nMenu:\n------------");
printf("\n1.Insert node \n2.Delete node\n3.Display\n4.Exit");
printf("\nEnter Your Choice:");
scanf("%d",&ch);
fflush(stdin);
switch(ch){
case 1: insertnode();
break;
case 2: deletenode();
break;
case 3: display();
break;

case 4: exit(1);
break;
default:printf("\nInvalid choice entered! Enter again..");
break;
}
} while(1);
}

void insertnode(){
struct studinfo *newnode;
newnode=(struct studinfo *)malloc(sizeof(struct studinfo ));
printf("\nEnter data of for the node to be inserted .");
scanf("%d",&newnode->data);
ptr=start;
if(start == NULL){
start=newnode;
newnode->next= NULL;
ptr=newnode;
printf("\n\New node with data:%d inserted",newnode->data);
} else {
ptr->next=newnode;
newnode->next= NULL;
ptr=newnode;
printf("\nNew node with data:%d inserted",newnode->data);
}
fflush(stdin);
}

void deletenode(){
struct studinfo *del;

if( start== NULL)


printf("\nList is empty");
else {
ptr=start;
start=start->next;
free(ptr);
printf("\nDeleted");
}
}

void display(){
ptr=start;
for(disp=start; disp!=NULL; disp=disp->next){
if(ptr== NULL)
printf("\nList is empty");
else{
ptr=ptr->next;
printf("\nData: %d",disp->data);
}
}
}

Output:

Practical No. 8
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* add(struct node *head, int data)
{
struct node *tmp;
if(head == NULL)
{
head=(struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("\nError! memory is not available");
exit(0);
}
head-> data = data;
head-> next = head;
}
else
{
tmp = head;
while (tmp-> next != head)
tmp = tmp-> next;
tmp-> next = (struct node *)malloc(sizeof(struct node));
if(tmp -> next == NULL)
{

printf("\nError! memory is not available");


exit(0);
}
tmp = tmp-> next;
tmp->data = data;
tmp->next = head;
}
return head;
}

void printlist(struct node *head){


struct node *current;
current = head;
if(current!= NULL){
do{
printf("%d\t",current->data);
current = current->next;
} while (current!= head);
printf("\n");
} else
printf("\nThe list is empty");
}

void destroy(struct node *head){


struct node *current, *tmp;
current = head->next;
head->next = NULL;
while(current != NULL){
tmp = current->next;
free(current);

current = tmp;
}
}

void main(){
struct node *head = NULL;
clrscr();
head = add(head,1);
printlist(head);
head = add(head,20);
printlist(head);
head = add(head,10);
printlist(head);
head = add(head,5);
printlist(head);
destroy(head);
getch();
}

Output:

Practical No. 9
#include <stdio.h>
#include <conio.h>

void main(){
int i, j, temp;
int arr[5] = {25, 17,31,13,2};
clrscr();
printf("Bubble sort.\n" );
printf("\nArray before sorting:\n");
for (i = 0; i <= 4; i++)
printf("%d\t", arr[i]);

for (i = 0; i <= 3; i++){


for ( j = 0 ; j <= 3 - i ; j++ ){
if(arr[j] > arr[j+1]){
temp = arr[j] ;
arr[j] = arr[j + 1] ;
arr[j + 1] = temp;
}
}
}
printf ( "\n\nArray after sorting:\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;
getch();
}

Output:

Practical No. 10
#include <stdio.h>
#include <conio.h>
void makeheap (int [], int);
void heapsort (int [], int);

void main(){
int arr[10] = {11,2,9,13,57,25,17,1,90,3};
int i ;
clrscr();
printf("Heap Sort.\n");
makeheap(arr, 10);
printf("\nBefore Sorting:\n");
for(i=0; i<=9; i++)
printf("%d\t", arr[i]);
heapsort(arr, 10);
printf("\nAfter Sorting:\n");
for (i=0;i<=9;i++)
printf ("%d\t", arr[i]);
getch();
}
void makeheap(int x[],int n){
int i, val, s, f;
for (i=1;i < n;i++){
val = x[i];
s=i;
f=(s-1)/2;
while (s>0 && x[f]<val){
x[s] = x[f];
s=f;

f=(s-1)/2;
}
x[s] = val;
}
}

void heapsort (int x[], int n){


int i, s, f, ivalue ;
for (i=n-1;i>0;i--){
ivalue=x[i];
x[i]=x[0];
f=0;
if(i== 1)
s=-1;
else
s=1;
if(i>2 && x[2]>x[1])
s=2;
while(s>=0 && ivalue<x[s]){
x[f]=x[s];
f=s;
s=2*f+1;
if(s+1<=i-1 && x[s]<x[s+1])
s++;
if (s>i-1)
s=-1;
}
x[f]=ivalue;
}
}

Output:

Practical No. 11
#include <stdio.h>
#include <conio.h>
void main(){
int arr[5] = {25,17,31,13,2};
int i,j,k,temp;
clrscr();
printf ("Insertion sort.\n");
printf ("\nArray before sorting:\n");
for (i=0; i<=4; i++)
printf("%d\t", arr[i]);
for (i=1; i<=4; i++){
for (j=0; j<i; j++){
if (arr[j] > arr[i]){
temp = arr[j];
arr[j] = arr[i];
for (k=i; k>j; k--)
arr[k] = arr[k - 1];
arr[k + 1] = temp;
}
}
}
printf("\n\nArray after sorting:\n");
for(i=0; i<=4; i++)
printf( "%d\t", arr[i]);
getch();
}

Output:

Practical No. 12
#include <stdio.h>
#include <conio.h>
void main(){
int i, j,arr[50],temp;
clrscr();
for(i=0;i<5;i++){
printf("Enter no. %d: ",i+ 1);
scanf("%d" ,&arr[i]);
}
printf ( "\nBubble sort.\n" ) ;
printf ( "\n\nArray before sorting:\n\n\t") ;
for ( i = 0 ; i <= 4; i++ )
printf("%d\t", arr[i]) ;
for ( i= 0 ; i <= 3 ; i++ ){
for (j = 0 ; j <= 3 - i ;j++ ){
if ( arr[j] > arr[j + 1] ){
temp = arr[j] ;
arr[j] = arr[j + 1] ;
arr[j + 1] = temp;
}
}
}
printf ( "\n\nArray after sorting:\n\n") ;
for ( i = 0 ; i <= 4 ; i++ )
printf("\t%d", arr[i] );
getch();
}

Output:

Practical No. 13
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define newnode (struct node*)malloc(sizeof(struct node))
struct node
{
int data;
struct node *next;
};
//struct node *start;
struct node *create_list();
struct node *merge_list(struct node *fl, struct node *f2);
void main(){
struct node *n1,*n2, *n3;
int len,reply;
n1 = n2 = NULL;
clrscr();
printf("Join 2 circular LIsts");
printf("\nEnter first circular linked-list\n");
n1 = create_list();
printf("\nEnter second circular linked-list\n");
n2 = create_list();
printf("\nFirst list is: ");
print_list(n1);
printf("\nSecond list is: ");
print_list(n2);
n3 = merge_list(n1,n2);
printf("\nThe resultant joint list is: ");
print_list(n3);
getch();
}
struct node *create_list(){
struct node *f,*c,*p;
int tdata;
f=NULL;
printf("Enter data (use 0 to exit) : ");
scanf("%d" ,&tdata);
while(tdata != 0){
c = newnode;
if( c == NULL){
printf("\nInsuf. memory");
exit(0);
}
c->data = tdata;

c->next = NULL;
if(f==NULL)
f=c;
else
p->next = c;
p=c;
printf("Enter data (use 0 to exit) : ");
scanf("%d",&tdata);
}
return(f);
}

print_list(struct node *f){


struct node *t;
if(f == NULL){
printf("\nList is empty");
return;
}
t = f;
while (t != NULL){
printf("%4d",t->data);
t=t->next;
}
return;
}
struct node *join_list(struct node *f1, struct node *f2){
int reply;
struct node *t1, *t2;
if(f1 == NULL && f2 == NULL)
return(NULL);
else if(f1 !=NULL && f2 == NULL)
return(f1);
else if(f1 == NULL && f2 !=NULL)
return(f2);
else{
t1 = f1;
while (t1->next !=NULL)
t1= t1->next;
t1->next =f2;
return(f1);
}
}

Output:

Practical No. 14

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define newnode (struct node*)malloc(sizeof(struct node))
struct node{
int data;
struct node *next;
};
//struct node *start;
struct node *create_list();
struct node *join_list(struct node *f1, struct node *f2);
void main(){
struct node *f1, *f2, *f3;
int len,reply;
f1 = NULL; f2 = NULL;
clrscr();
printf("\nEnter first list\n");
f1 = create_list();
printf("\nEnter second list\n");
f2 = create_list();
printf("\nFirst list is:");
print_list(f1);
printf("\nSecond list is:");
print_list(f2);
f3 = join_list(f1 ,f2);
printf("\nThe resultant list is: ");
print_list(f3);
getch();
}
struct node *create_list(){
struct node *f,*c,*p;
int tdata;
f=NULL;
printf("\nEnter data(Use 0 to exit): ");
scanf("%d", &tdata);
while(tdata!=0){
c = newnode;
if(c == NULL){
printf("\nInsufficient memory ");
exit(0);
}
c->data = tdata;
c->next = NULL;
if(f== NULL)
f= c;

else
p->next =c;
p=c;
printf("Enter data (use 0 to exit) : ");
scanf("%d",&tdata);
}
return(f);
}
print_list(struct node *f){
struct node *t;
if(f == NULL){
printf("\nList is empty");
return;
}
t = f;
while (t != NULL){
printf("%4d",t->data);
t=t->next;
}
return;
}
struct node *join_list(struct node *f1, struct node *f2){
int reply;
struct node *t1, *t2;
if(f1 == NULL && f2 == NULL)
return(NULL);
else if(f1 !=NULL && f2 == NULL)
return(f1);
else if(f1 == NULL && f2 !=NULL)
return(f2);
else{
t1 = f1;
while (t1->next !=NULL)
t1= t1->next;
t1->next =f2;
return(f1);
}
}

Output:

Você também pode gostar