0% found this document useful (0 votes)
2 views

Lab Manual Ex7 To Ex12

The document contains C code for various data structures and algorithms, including linked lists, queues, and stacks. It provides functions for creating, inserting, deleting, and displaying elements in these data structures. The code also includes user interaction for selecting operations on these data structures.

Uploaded by

Nikhil Koli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual Ex7 To Ex12

The document contains C code for various data structures and algorithms, including linked lists, queues, and stacks. It provides functions for creating, inserting, deleting, and displaying elements in these data structures. The code also includes user interaction for selecting operations on these data structures.

Uploaded by

Nikhil Koli
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h> ins_beg()
#include<conio.h> {
int choice; newnode=(N*)malloc(sizeof(N));
struct node printf("Enter node element::");
{ scanf("%d",&newnode->info);
int info; if(start==NULL)
struct node *next; {
}; start=temp=newnode;
typedef struct node N; printf("It is the First Element in list");
N *start,*temp,*newnode,*temp1,*temp2; }
void main() else
{ {
clrscr(); temp=start;
printf("\n1-->Create List\n"); newnode->next=start;
printf("Insert Node"); start=newnode;
printf("\n2-->At Beg\t3-->At Pos\t4-->At End\n"); printf("Node is inserted at Beginning......");
printf("Delete Node"); }
printf("\n5-->At Beg\t6-->At Pos\t7-->At End\t"); }
printf("\n8-->Display\t9-->Search-Count\t10-- ins_end()
>Exit"); {
do newnode=(N*)malloc(sizeof(N));
{ printf("Enter node element::");
printf("\nEnter your choice::"); scanf("%d",&newnode->info);
scanf("%d",&choice); if(start==NULL)
switch(choice) {
{ start=temp=newnode;
case 1 : create_list(); printf("It is the First Element in list");
break; }
case 2 : ins_beg(); else
break; {
case 3 : ins_pos(); temp=start;
break; while(temp->next!=NULL)
case 4 : ins_end(); {
break; temp=temp->next;
case 5: del_beg(); }
break; temp->next=newnode;
case 6: del_pos(); printf("Node is inserted at Ending......");
break; }
case 7: del_end(); }
break; del_end()
case 8: display(); {
break; temp=start;
case 9: search_count(); while(temp->next!=NULL)
break; {
case 10: exit(0); temp1=temp;
break; temp=temp->next;
default: printf("Enter the correct choice"); }
break; temp1->next=NULL;
} printf("Ending Node %d is Deleted",temp->info);
}while(choice!=10); free(temp);
}
}
create_list() ins_pos()
{ {
int i,n; int pos,i;
printf("Enter how many nodes::"); newnode=(N*)malloc(sizeof(N));
scanf("%d",&n); printf("Enter node element::");
for(i=0;i<n;i++) scanf("%d",&newnode->info);
{ printf("Enter position for insertion::");
newnode = (N*)malloc(sizeof(N)); scanf("%d",&pos);
newnode->next=NULL; temp=start;
printf("Enter the data item\n"); if(pos==1)
scanf("%d", &newnode->info); {
if(start==NULL) newnode->next=start;
{ start=newnode;
start=temp=newnode; }
} else
else {
{ for(i=1;i<pos-1;i++)
temp->next=newnode; temp=temp->next;
temp=newnode; newnode->next=temp->next;
} temp->next=newnode;
}
}
} printf("Node is inserted at Position::%d......",pos);
display() }
{ del_pos()
temp = start; {
printf("\n status of the linked list is\n"); int pos,i=0;
while (temp != NULL) printf("Enter the position for deletion::");
{ scanf("%d",&pos);
printf("%d=>",temp->info); temp=start;
temp=temp->next; if(pos==1)
} {
printf("NULL\n"); start=temp->next;
} free(temp);
}
search_count() else
{ {
int i=0,count=0,n; for(i=1;i<pos-1;i++)
printf("Enter the node to search::"); {
scanf("%d",&n); temp=temp->next;
for(temp=start;temp!=NULL;temp=temp->next) }
{ temp1=temp->next;
if(temp->info==n) temp->next=temp1->next;
{ free(temp1);
printf("\nNode Found"); }
i++; }
} del_beg()
count++; {
} temp=start;
if(i==0) start=start->next;
printf("\nNode not found"); temp->next=NULL;
printf("\nNumber of nodes = %d",count); printf("Begining node %d is deleted",temp->info);
} free(temp);

}
/******Circular Singly Linked List*****/
#include<stdio.h> void create()
#include<stdlib.h> {
struct node int i,n;
printf("Enter the no of nodes::");
{ scanf("%d",&n);
int info; for(i=0;i<n;i++)
struct Node *next; {
}; newnode=(N*)malloc(sizeof(N));
typedef struct node N; printf("Enter the node value : ");
N *start,*last,*temp,*newnode,*temp1; scanf("%d",&newnode->info);
if(start==NULL)
void create(); start=last=newnode;
void ins_beg(); else
void ins_end(); {
void del_beg(); last->next=newnode;
void del_end(); last=newnode;
void display(); }
last->next=start;
int main() }
{ }
int chc;
clrscr(); void ins_beg()
printf("\n\t1.Create\n\t2.Ins_beg\t3.Ins_end\t"); {
printf("\n\t4.Delete Beg\t5.Delete End\n\t6.Display\t7.Exit"); newnode=(N*)malloc(sizeof(N));
do printf("Enter the node value : ");
{ scanf("%d",&newnode->info);
printf("\nEnter your choice : "); if(start==NULL)
scanf("%d",&chc); {
switch(chc) start=last=newnode;
{ }
case 1: create(); else
printf("List is created"); {
break; newnode->next=start;
case 2: ins_beg(); start=newnode;
printf("Node is inserted at Begin"); }
break; last->next=newnode;
case 3: ins_end(); }
printf("Node is inserted at End"); void del_beg()
break; {
case 4: del_beg(); temp=start;
printf("Beginning node is deleted"); if(start==NULL)
break; printf("\nList Empty :");
case 5: del_end(); else
printf("Ending node is deleted"); {
break; start=start->next;
case 6:printf("Status of list::"); last->next=start;
display(); free(temp);
break; }
case 7:exit(0);
break;
default: }
printf("\nInvalid choice :");
}
}while(chc!=7);
}
void ins_end()
{
newnode=(N*)malloc(sizeof(N));
printf("\nEnter the node value : "); void display()
scanf("%d",&newnode->info); {
if(start==NULL) temp=start;
{ if(start==NULL)
start=last=newnode; printf("\nEmpty");
} else
else {
{ for(;temp!=last;temp=temp->next)
last->next=newnode; printf("%d -> \t",temp->info);
last=newnode; printf("%d \t",temp->info);
} } }
last->next=start;
}

void del_end()
{
temp=start;
while(temp->next!=start)
{
temp1=temp;
temp=temp->next;
}
temp1->next=start;
last=temp1;
free(temp);
}
/***Simple Queue***/
#include<stdio.h> qdelete()
#include<conio.h> {
#define MAXSIZE 5 int num;
int front=-1, rear=-1,choice; if(front==-1)
int q[10]; {
void main() printf("Queue empty\n");
{ return;
clrscr(); }
printf("\n1-->Insert\n"); num=q[front];
printf("2-->Delete\n"); printf("Deleted item::\t%d",num);
printf("3-->Display\n"); if(front==rear)
printf("4-->Exit\n"); front=rear=-1;
do else
{ printf("\nEnter your choice::"); front++;
scanf("%d",&choice); }
switch(choice) qdisplay()
{ {
case 1:qinsert(); int i;
break; if(front==-1)
case 2:qdelete(); {
break; printf("Queue empty\n");
case 3:qdisplay(); return;
break; }
case 4:exit(0); else
break; {
default: printf("\nThe status of the Queue::");
printf("Enter correct choice"); for(i=front;i<=rear;i++)
break; {
} printf("%d\t",q[i]);
}while(choice!=4); }
} }
qinsert() }
{
int num; /* OUTPUT
if(rear==(MAXSIZE-1))
{ 1-->Insert
printf("Queue is full\n"); 2-->Delete
return; 3-->Display
} 4-->Exit
else Enter your choice::1
{ Enter no::1
printf("Enter no::"); Enter your choice::1
scanf("%d",&num); Enter no::2
if(front==-1) Enter your choice::3
{ The status of the Queue::1 2
front=rear=0; Enter your choice::2
} Deleted item:: 1
else Enter your choice::2
{ Deleted item:: 2
rear=rear+1; Enter your choice::3
} Queue empty
q[rear]=num; Enter your choice::4*/
}
}
/***********Circular Queue**************/

#include<stdio.h> cqdelete()
#include<conio.h> {
#define MAXSIZE 5 int num;
int cq[10]; if(front==-1)
int front=-1,rear=-1; {
int choice; printf("Queue is Empty\n");
void main() return;
{ }
clrscr(); else
printf("--------1.Insert-------\n"); {
printf("------- 2.Delete-------\n"); num=cq[front];
printf("------- 3.Display------\n"); printf("Deleted element is ::\t%d",num);
printf("--------4.Exit---------\n"); if(front==rear)
do front=rear=-1;
{ else
printf("\nEnter your choice::"); front=(front+1)%MAXSIZE;
scanf("%d",&choice); }
switch(choice) }
{ cqdisplay()
case 1 : cqinsert(); {
break; int i;
case 2 : cqdelete(); if(front==-1)
break; {
case 3 : cqdisplay(); printf("Queue empty\n");
break; return;
case 4: return; }
} else
}while(choice!=4); {
} printf("\nThe status of the Queue::");
for(i=front;i<=rear;i++)
cqinsert() {
{ printf("%d\t",cq[i]);
int num; }
if(front==(rear+1)%MAXSIZE) }
{ }
printf("Queue is full\n"); /* OUTPUT
return; --------1.Insert-------
} ------- 2.Delete-------
else ------- 3.Display------
{ --------4.Exit---------
printf("Enter the element to be Enter your choice::1
inserted::"); Enter the element to be inserted::1
scanf("%d",&num); Enter your choice::1
if(front==-1) Enter the element to be inserted::2
front=rear=0; Enter your choice::3
else The status of the Queue::1 2
rear=(rear+1) % MAXSIZE; Enter your choice::2
cq[rear]= num; Deleted element is :: 1
} Enter your choice::2
} Deleted element is :: 2
Enter your choice::2
Queue is Empty

Enter your choice::4*/


/**** STACK ****/

#include<stdio.h> pop()
#include<conio.h> {
#define MAXSIZE 5 int item;
if(Top == -1)
int stack[MAXSIZE]; {
int Top=-1; printf("The stack is Empty");
void main() }
{ else
int choice; {
char ch; item = stack[Top];
clrscr(); Top = Top-1;
printf("\n1. PUSH "); printf("\nThe deleted element is :: %d",item);
printf("\n2. POP "); }
printf("\n3. DISPLAY "); }
printf("\n4. EXIT"); display()
do {
{ int i;
printf("\nEnter your choice::"); if(Top == -1)
scanf("%d",&choice); {
switch(choice) printf("The Stack is Empty");
{ }
case 1: push(); else
break; {
case 2: pop(); printf("Status of Stack::");
break; for(i=Top;i>=0;i--)
case 3: display(); {
break; printf("\t%d",stack[i]);
case 4: exit(0); }
break; }
default: printf("\nYou Entered Wrong Choice"); }
}
} /* OUTPUT
while(choice!=4);
} 1. PUSH
push() 2. POP
{ 3. DISPLAY
int item; 4. EXIT
if(Top == MAXSIZE - 1) Enter your choice::1
{ Enter the element to be inserted::1
printf("\nThe Stack Is Full"); Enter your choice::1
} Enter the element to be inserted::2
else Enter your choice::3
{ Status of Stack:: 2 1
printf("Enter the element to be inserted::"); Enter your choice::2
scanf("%d",&item); The deleted element is :: 2
Top= Top+1; Enter your choice::2
stack[Top] = item; The deleted element is :: 1
} Enter your choice::3
} The Stack is Empty
Enter your choice::4 */
/**** STACK Using Linked List****/
#include<stdio.h> push()
#include<conio.h> {
if(top==NULL)
struct node {
{ top=newnode;
int info; }
struct node *next; else
}; {
temp=top;
typedef struct node N; newnode->next=temp;
N *top,*temp,*newnode; top=newnode;
}
void main() }
{ pop()
int i,n; {
int choice; if(top==NULL)
clrscr(); printf("Stack is empty");
printf("\n1. PUSH "); else
printf("\n2. POP "); {
printf("\n3. DISPLAY "); temp=top;
printf("\n4. EXIT"); top=top->next;
do printf("The Deleted element is::%d",temp->info);
{ free(temp);
printf("\nEnter your choice::"); }
scanf("%d",&choice);
newnode=(N*)malloc(sizeof(N)); }
newnode->next=NULL; display()
switch(choice) {
{
case 1: temp=top;
printf("Enter Element::"); printf("Status of List::");
scanf("%d",&newnode->info); if(top==NULL)
push(); printf("\nList is Empty");
break; while(temp!=NULL)
case 2: {
pop(); printf("\t%d",temp->info);
break; temp=temp->next;
case 3: }
display(); }
break; /* OUTPUT
case 4: 1. PUSH
exit(0); 2. POP
break; 3. DISPLAY
default: 4. EXIT
printf("\nYou Entered Wrong Choice........"); Enter your choice::1
} Enter Element::1
} Enter your choice::1
while(choice!=4); Enter Element::2
} Enter your choice::3
Status of List:: 2 1
Enter your choice::2
The Deleted element is::2
Enter your choice::2
The Deleted element is::1
Enter your choice::2
Stack is empty
Enter your choice::4*/
/**** QUEUE Using Linked List****/
#include<stdio.h> qdelete()
#include<conio.h> {
struct node if(front == NULL)
{ printf("\nQueue is Empty!!!\n");
int info; else
struct node *next; {
}; temp=front;
typedef struct node N; front = front -> next;
N *front=NULL,*rear=NULL,*temp,*newnode; printf("Deleted element: %d\n", temp->info);
free(temp);
void main() }
{ }
int choice, value; qdisplay()
clrscr(); {
printf("\n:: Queue Implementation using Linked List ::\n"); if(front == NULL)
printf("\n****** MENU ******\n"); printf("\nQueue is Empty!!!\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n"); else
do {
{ temp=front;
printf("Enter your choice: "); while(temp!=NULL)
scanf("%d",&choice); {
switch(choice) printf("%d--->",temp->info);
{ temp = temp -> next;
case 1: }
qinsert(); printf("NULL\n");
break; }
case 2: }
qdelete();
break; /* OUTPUT
case 3:
qdisplay(); :: Queue Implementation using Linked List ::
break;
case 4: ****** MENU ******
exit(0); 1. Insert
break; 2. Delete
default: printf("\nEnter Wrong Choice"); 3. Display
} 4. Exit
}while(choice!=4); Enter your choice: 1
} Enter the element to insert::1
qinsert() Insertion is Success!!!
{ Enter your choice: 1
newnode=(N*)malloc(sizeof(N)); Enter the element to insert::2
printf("Enter the element to insert::"); Insertion is Success!!!
scanf("%d",&newnode->info); Enter your choice: 3
newnode -> next = NULL; 1--->2--->NULL
if(front==NULL) Enter your choice: 2
front=rear=newnode; Deleted element: 1
else Enter your choice: 2
{ Deleted element: 2
rear -> next = newnode; Enter your choice: 2
rear = newnode; Queue is Empty!!!
} Enter your choice: 4*/
printf("Insertion is Success!!!\n");
}
#include <stdio.h> N *CreateBST(N *root, int item)
struct tnode {
{ if(root == NULL)
int data; {
struct tnode *right; root = (N *)malloc(sizeof(N));
struct tnode *left; root->left = root->right = NULL;
}; root->data = item;
typedef struct tnode N; return root;
N *CreateBST(N*, int); }
void main() else
{ {
N *root = NULL; if(item < root->data )
int choice, item, n, i; root->left = CreateBST(root->left,item);
clrscr(); else if(item > root->data )
printf("\n\nBinary Search Tree Operations\n"); root->right = CreateBST(root->right,item);
printf("\n1. Creation of BST"); else
printf("\n2. Traverse in Inorder"); printf(" Duplicate Element!!!");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder"); return(root);
printf("\n5. Exit\n"); }
do }
{
printf("\nEnter Choice : "); Inorder(N *root)
scanf("%d",&choice); {
switch(choice) if( root != NULL)
{ {
case 1: Inorder(root->left);
root = NULL; printf(" %d ",root->data);
printf("BST for How Many Nodes ? "); Inorder(root->right);
scanf("%d",&n); }
for(i = 1; i <= n; i++) }
{
printf("Enter data for node %d : ", i); Preorder(N *root)
scanf("%d",&item); {
root = CreateBST(root,item); if( root != NULL)
} {
printf("\nBST with %d nodes is ready to Use!!\n", n); printf(" %d ",root->data);
break; Preorder(root->left);
case 2: printf("\nBST Traversal in INORDER \n"); Preorder(root->right);
Inorder(root); }
break; }
case 3: printf("\nBST Traversal in PREORDER \n");
Preorder(root); Postorder(N *root)
break; {
case 4: printf("\nBST Traversal in POSTORDER \n"); if( root != NULL)
Postorder(root); {
break; Postorder(root->left);
case 5: exit(0); Postorder(root->right);
break; printf(" %d ",root->data);
default: printf("\n\nInvalid Option \n\n"); }
break; }
}
} while(choice != 5);
getch();
}

You might also like