Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ds Circular Queue

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Data Structure PROGRAM

#include<stdio.h>
# define max 5
int queue[max],front=-1,rear=-1;
void enqueue(int element){
int i=front;
if(front==-1&&rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
printf("Inserted->%d",element);
}
else if((rear+1)%max==front){
printf("Queue is overflow");
}
else{
rear=(rear+1);
queue[rear]=element;
printf("Inserted->%d",element);
}
}
void dequeue(){
if((front==-1)&&(rear==-1)){
printf("\n Queue is underflow");
}
else if(front==rear){
printf("\n The Dequeue element -> %d",queue[front]);
front=-1;
rear=-1;
}
else{
printf("\n The dequeue element is %d",queue[front]);
Data Structure PROGRAM

front=(front+1)%max;
}

}
void display(){
int i=front;
if(front==-1&&rear==-1){
printf("\n Queue is empty");
}
else{
printf("\n Elements in a queue are:");
while(i<=rear)
{
printf("%d \t",queue[i]);
i++;
}
}
printf("\n");
}
int main(){
//int front=-1,rear=-1;
int ch,x,element;
do{
printf(" \n Circular Queue");
printf("\n 1. Enqueue\n2. Dequeue\n 3.Diplay \n 4.Exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the element:");
scanf("%d",&element);
enqueue(element);
Data Structure PROGRAM

break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\n Exit the program");
break;
default:
printf("\n Invalid choice");

}while(ch!=4);
}
Data Structure PROGRAM

EXPECTED INPUT/OUTPUT
Data Structure PROGRAM
Data Structure PROGRAM
Data Structure PROGRAM

Program: Infix to Postfix

#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
Data Structure PROGRAM

e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}
…………………………………………………………………………………………………………………………………………………………….

Input/Output
Data Structure PROGRAM

Simple Linked List

#include <stdio.h>
#include <stdlib.h>

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

struct node *head = NULL;


struct node *current = NULL;

//display the list


void printList() {

struct node *ptr = head;

printf("\n[head] =>");
//start from the beginning
while(ptr != NULL) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
}

printf(" [null]\n");
}

//insert link at the first location


void insert(int data) {
//create a link
struct node *link = (struct node*)
malloc(sizeof(struct node));

//link->key = key;
link->data = data;

//point it to old first node


link->next = head;

//point first to new first node


head = link;
}
Data Structure PROGRAM

int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);

printList();
return 0;
}

// Linked list implementation in C

#include <stdio.h>
#include <stdlib.h>

// Creating a node
struct node {
int value;
struct node *next;//self referential structure
};

// print the linked list value


void printLinkedlist(struct node *p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}

int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;

// Allocate memory
one = malloc(sizeof(struct node));
Data Structure PROGRAM

two = malloc(sizeof(struct node));


three = malloc(sizeof(struct node));

// Assign value values


one->value = 1;
two->value = 2;
three->value = 3;

// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;

// printing node-value
head = one;
printLinkedlist(head);
}
Data Structure PROGRAM

Binary Search Tree

#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the binary search tree


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a value into the binary search tree


struct Node* insert(struct Node* root, int value) {
// If the tree is empty, create a new node and return it
if (root == NULL)
return createNode(value);

// Otherwise, recur down the tree


if (value < root->data)
root->left = insert(root->left, value);
else if (value > root->data)
root->right = insert(root->right, value);

// return the unchanged node pointer


return root;
}

// Function to perform inorder traversal of the binary search tree


void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root != NULL) {
Data Structure PROGRAM

printf("%d ", root->data);


inorder(root->left);
inorder(root->right);
}
}

void postorder(struct Node* root) {


if (root != NULL) {

inorder(root->left);
inorder(root->right);
printf("%d ", root->data);
}
}
// Function to print the binary search tree in a graphical format
void printTree(struct Node* root, int space) {
if (root == NULL)
return;
// Increase distance between levels
space += 3;
// Process right child first
printTree(root->right, space);
// Print current node after space
printf("\n");
for (int i = 5; i < space; i++)
printf(" ");
printf("%d\n", root->data);
// Process left child
printTree(root->left, space);
}

int main() {
struct Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

printf("Inorder traversal of the BST: ");


inorder(root);
printf("\n");
printf("preorder traversal of the BST: ");
preorder(root);
printf("\n");
Data Structure PROGRAM

printf("postorder traversal of the BST: ");


postorder(root);
printf("\n");

printf("\nBinary Search Tree (Tree Format):\n");


printTree(root, 0);

return 0;
}

You might also like