Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Circular queue implementation

#include
#define max 5
struct queue
{
int data[max],front,rear;
};
struct queue q;
int isfull()
{
if((q.rear+2)%max==q.front)
return 1;
else
return 0;
}
int isempty()
{
if ((q.rear+1)%max==q.front)
return 1;
else
return 0;
}
void enqueue()
{
int val;
if (isfull())
printf("\nQueue is full!");
else
{
printf("\nEnter the value : ");
scanf("%d",&val);
q.rear=(q.rear+1)%max;
q.data[q.rear]=val;
}
}
int dequeue()
{
int x;
if (isempty())
printf("\nQueue is empty!");
else
{
x=q.data[q.front];
q.front=(q.front+1)%max;
return x;
}
}
void display()
{
int i;
if (isempty())
printf("\nQueue is empty!");
else
{
printf("\nThe elements in the queue are...");
for(i=q.front;i!=q.rear;i=(i+1)%max)
printf("\n%d",q.data[i]);
printf("\n%d",q.data[q.rear]);
}
}
main()
{
q.rear=-1;
q.front=0;
int choice;
int isfull();
int isempty();
int dequeue();
void enqueue();
void display();
printf("\n1-Insert 2-Delete 3-Display 4-Exit");
do
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: printf("The deleted element is %d",dequeue()); break;
case 3: display(); break;
}
}while(choice!=4);
}

Data structures : single linked list program

#include
struct node
{
int data;
struct node *next;
}*head,*temp1,*curr;
void insert();
void delete();
void count();
void display();
void insert()
{
struct node *temp;
int val;
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
{
printf("\nMemory Full!");
return;
}
printf("Enter the number : ");
scanf("%d",&val);
temp->data=val;
temp->next=NULL;
if (head==NULL)
{
head=temp;
return;
}
if (head->next==NULL (head->next!=NULL && temp->data <>data))
{
if (temp->data <>data)
{
temp->next=head;
head=temp;
}
else
head->next=temp;
return;
}
for(curr=head->next,temp1=head;curr!=NULL;curr=curr->next,temp1-temp1->next)
{
if (curr->data > temp->data)
{
temp->next=curr;
temp1->next=temp;
return;
}
}
temp1->next=temp;
return;
}
void delete()
{
int x,found=0;
struct node *temp;
printf("Enter the node to delete : ");
scanf("%d",&x);
if (x==head->data)
{
temp=head;
head=head->next;
free(temp);
found=1;
return;
}
for(temp=head,curr=head->next;curr!=NULL;curr=curr->next,temp=temp->next)
{
if (curr->data==x && found==0)
{
found=1;
temp1=curr;
if (curr->next==NULL)
temp->next=NULL;
else
{
curr=curr->next;
temp->next=curr;
}
free(temp1);
}
}
if (!found)
printf("\n Element not found!");
}
void count()
{
int c=0;
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next,c++);
printf("\nNumber of nodes : %d",c);
}
void display()
{
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d-->",temp->data);
}
main()
{
int wish=1,choice;
head=NULL;
do
{
printf("\n1.Insert\n2.Delete\n3.Count\n4.Display\n5.Exit\n");
printf("Enter Your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: count(); break;
case 4: display(); break;
}
}while(choice!=5);
}

Data structures : Stack using pointers program

#include
int pop();
void push();
int isempty();
void display();
struct node
{
int data;
struct node *next;
};
struct node *temp;
struct node *top=NULL;
void push()
{
int value;
printf("\nEnter the value : ");
scanf("%d",&value);
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
printf("\nMemory is not available!");
else
{
temp->data=value;
if (top==NULL)
temp->next=NULL;
else
temp->next=top;
top=temp;
}
}
int pop()
{
struct node *temp;
int value;
if (isempty())
printf("\nStack is empty!");
else
{
temp=top;
value=temp->data;
top=top->next;
free(temp);
return value;
}
}
int isempty()
{
if (top==NULL)
return 1;
else
return 0;
}
void display()
{
struct node *temp;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(temp=top;temp!=NULL;temp=temp->next)
printf("%d\n",temp->data);
}
}
main()
{
int ch;
printf("\n1.Push 2.Pop 3.Display 4.Exit\n");
do
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: printf("\nDeleted element is %d",pop()); break;
case 3: display(); break;
}
}while(ch!=4);
}

Data structures : Stack using array program



#include
#define max 10
struct stack
{
int data[max];
int top;
};
struct stack s;
main()
{
s.top=-1;
int choice;
int isfull();
int isempty();
void push();
int pop();
void display();
do
{
printf("\n1-Add element 2-Delete element 3-Display element 4-Exit\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: printf("\nThe deleted element is %d",pop()); break;
case 3: display();
}
}while(choice!=4);
}
int isempty()
{
if(s.top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(s.top==max-1)
return 1;
else
return 0;
}
void push()
{
int value;
printf("\nEnter the value... ");
scanf("%d",&value);
if(s.top{
s.top+=1;
s.data[s.top]=value;
printf("\n# Value Inserted #");
}
else
printf("\nStack Overflow!");
}
int pop()
{
int x;
if (isempty())
{
printf("\nStack empty!");
return -1;
}
else
{
x=s.data[s.top];
s.top-=1;
return x;
}
}
void display()
{
int i;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(i=0;i<=s.top;i++)
printf("%d\n",s.data[i]);
printf("\nThe number of elements in the stack are %d",i);
}
}


Data structures : Queue using array program



#include
#define max 10

struct queue
{
int front,rear,data[max];
};
struct queue q;

main()
{
int ch;
q.front=0;
q.rear=-1;
int isempty();
void display();
void enqueue();
void count();
int isfull();
int dequeue();
do
{
printf("\n1-Add 2-Delete 3-Display 4-Count 5-Exit\nEnter ur choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue(); break;
case 2: printf("\nThe deleted element is %d",dequeue()); break;
case 3: display(); break;
case 4: count(); break;
}
}while(ch!=5);
}

int isfull()
{
if (q.rear==max-1)
return 1;
else
return 0;
}

int isempty()
{
if (q.rear return 1;
else
return 0;
}

void count()
{
printf("\nThe number of elements in the queue are %d",q.rear-q.front+1);
}

void enqueue()
{
int val;
if (isfull())
printf("\nQueue is full!");
else
{
printf("\nEnter the element... ");
scanf("%d",&val);
q.rear+=1;
q.data[q.rear]=val;
}
}

int dequeue()
{
int x;
if (isempty())
printf("\nQueue is empty!");
else
{
x=q.data[q.front];
q.front+=1;
return x;
}
}

void display()
{
int i;
printf("\nThe elements in the queue are...\n");
for(i=q.front;i<=q.rear;i++)
printf("%d\n",q.data[i]);
}

Data structures : Queue using pointers program

#include
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;

void enqueue()
{
struct node *temp;
int val;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the value : ");
scanf("%d",&val);
temp->data=val;
temp->next=NULL;
if (rear==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
}

void dequeue()
{
struct node *temp;
int x;
if (front==NULL)
printf("\nQueue is Empty!");
else
{
if (front==rear)
{
printf("\n%d deleted",front->data);
front=rear=NULL;
}
else
{
temp=front;
front=front->next;
printf("\n%d deleted!",temp->data);
free(temp);
}
}
}

void display()
{
struct node *temp;
printf("\nThe elements in the queue are...\n");
for(temp=front;temp!=NULL;temp=temp->next)
printf("%d\n",temp->data);
}

main()
{
int choice;
void enqueue();
void dequeue();
void display();
printf("\n1-Insert 2-Delete 3-Display 4-Exit ");
do
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
}
}while(choice!=4);
}