Completely Solved C, C++ Programs Assignment.




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

C program to implement Circular Queue

Filed Under: ,

Program C program to implement Circular Queue
#include <stdio.h>
#define MAX 10 /* The maximum size of the queue */
#include <stdlib.h>

void insert(int queue[], int *rear, int front, int value)
{
*rear= (*rear +1) % MAX;
if(*rear == front)
{
printf("The queue is full can not insert a valuen");
exit(0);
}
queue[*rear] = value;
}

void delete(int queue[], int *front, int rear, int * value)
{
if(*front == rear)
{
printf("The queue is empty can not delete a valuen");
exit(0);
}
*front = (*front + 1) % MAX;
*value = queue[*front];
}

void main()
{
int queue[MAX];
int front,rear;
int n,value;
front=0; rear=0;
do
{
do
{
printf("Enter the element to be insertedn");
scanf("%d",&value);
insert(queue,&rear,front,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to delete an elementn");
scanf("%d",&n);
while( n == 1)
{
delete(queue,&front,rear,&value);
printf("The value deleted is %dn",value);
printf("Enter 1 to delete an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be inserted
10
Enter 1 to continue
1
Enter the element to be inserted
20
Enter 1 to continue
1
Enter the element to be inserted
30
Enter 1 to continue
1
Enter the element to be inserted
40
Enter 1 to continue
1
Enter the element to be inserted
50
Enter 1 to continue
1
Enter the element to be inserted
60
Enter 1 to continue
1
Enter the element to be inserted
70
Enter 1 to continue
1
Enter the element to be inserted
80
Enter 1 to continue
1
Enter the element to be inserted
90
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 10
Enter 1 to delete an element
1
The value deleted is 20
Enter 1 to delete an element
0
Enter 1 to continue
1
Enter the element to be inserted
100
Enter 1 to continue
1
Enter the element to be inserted
110
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 30
Enter 1 to delete an element
1
The value deleted is 40
Enter 1 to delete an element
0
Enter 1 to continue
1
Enter the element to be inserted
120
Enter 1 to continue
1
Enter the element to be inserted
130
Enter 1 to continue
0
Enter 1 to delete an element
0
Enter 1 to continue
0

Back to main directory:  Data Structure 



The Concept of Circular Queues

Filed Under: ,

The problem with the previous implementation is that the insert function gives a queue-full signal even if a considerable portion is free. This happens because the queue has a tendency to move to the right unless the ‘front’ catches up with the ‘rear’ and both are reset to 0 again (in the delete procedure). To overcome this problem, the elements of the array are required to shift one position left whenever a deletion is made. But this will make the deletion process inefficient. Therefore, an efficient way of overcoming this problem is to consider the array to be circular, as shown in Figure


Back to main directory:  Data Structure 



C Program to implement queue using Linked list

Filed Under: ,

Implement Queue using link list Initially, the list is empty, so both the front and rear pointers are NULL. The insert function creates a new node, puts the new data value in it, appends it to an existing list, and makes the rear pointer point to it. A delete function checks whether the queue is empty, and if not, retrieves the data value of the node pointed to by the front, advances the front, and frees the storage of the node whose data value has been retrieved.
If the above strategy is used for creating a queue with four data values —10, 20, 30, and 40, the queue gets created as shown in Figure
Program C program for implementation of a stack using the linked list
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};

void insert(struct node **front, struct node **rear, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
/* creates new node
using data value
passed as parameter */
if(temp==NULL)
{
printf("No Memory available Errorn");
exit(0);
}
temp->data = value;
temp->link=NULL;
if(*rear == NULL)
{
*rear = temp;
*front = *rear;
}
else
{
(*rear)->link = temp;
*rear = temp;
}
}

void delete(struct node **front, struct node **rear, int *value)
{
struct node *temp;
if((*front == *rear) && (*rear == NULL))
{
printf(" The queue is empty cannot delete Errorn");
exit(0);
}
*value = (*front)->data;
temp = *front;
*front = (*front)->link;
if(*rear == temp)
*rear = (*rear)->link;
free(temp);
}

void main()
{
struct node *front=NULL,*rear = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be insertedn");
scanf("%d",&value);
insert(&front,&rear,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to delete an elementn");
scanf("%d",&n);
while( n == 1)
{
delete(&front,&rear,&value);
printf("The value deleted is %dn",value);
printf("Enter 1 to delete an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be inserted
10
Enter 1 to continue
1
Enter the element to be inserted
20
Enter 1 to continue
1
Enter the element to be inserted
30
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 10
Enter 1 to delete an element
1
The value deleted is 20
Enter 1 to delete an element
0
Enter 1 to continue
1
Enter the element to be inserted
40
Enter 1 to continue
1
Enter the element to be inserted
50
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 30
Enter 1 to pop an element
1
The value deleted is 40
Enter 1 to delete an element
1
The value deleted is 50
Enter 1 to delete an element
1
The queue is empty, cannot delete Error

Back to main directory:  Data Structure 



C program to implement a queue using an array

Filed Under: ,

Array implementation of Queue Since a queue is also a list, it can be implemented using an array or it can be implemented using a linked representation.When an array is used to implement a queue, then the insert and delete operations are realized using the operations available on an array. The limitation of an array implementation is that the queue cannot grow and shrink dynamically as per the requirement.
Program C program to implement a queue by using an array
#include <stdio.h>
#define MAX 10 /* The maximum size of the queue */
#include <stdlib.h>

void insert(int queue[], int *rear, int value)
{
if(*rear < MAX-1)
{
*rear= *rear +1;
queue[*rear] = value;
}
else
{
printf("The queue is full can not insert a valuen");
exit(0);
}
}

void delete(int queue[], int *front, int rear, int * value)
{
if(*front == rear)
{
printf("The queue is empty can not delete a valuen");
exit(0);
}
*front = *front + 1;
*value = queue[*front];
}
void main()
{
int queue[MAX];
int front,rear;
int n,value;
front=rear=(-1);
do
{
do
{
printf("Enter the element to be insertedn");
scanf("%d",&value);
insert(queue,&rear,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to delete an elementn");
scanf("%d",&n);
while( n == 1)
{
delete(queue,&front,rear,&value);
printf("The value deleted is %dn",value);
printf("Enter 1 to delete an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be inserted
10
Enter 1 to continue
1
Enter the element to be inserted
20
Enter 1 to continue
1
Enter the element to be inserted
30
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 10
Enter 1 to delete an element
1
The value deleted is 20
Enter 1 to delete an element
0
Enter 1 to continue
1
Enter the element to be inserted
40
Enter 1 to continue
1
Enter the element to be inserted
50
Enter 1 to continue
0
Enter 1 to delete an element
1
The value deleted is 30
Enter 1 to delete an element
1
The value deleted is 40
Enter 1 to delete an element
0
Enter 1 to continue
0

Back to main directory:  Data Structure 



The Concept of Queues

Filed Under: ,

[Queue]  A queue is also a list of elements with insertions permitted at one end—called the rear, and deletions permitted from the other end—called the front. This means that the removal of elements from a queue is possible in the same order in which the insertion of elements is made into the queue. Thus, a queue data structure exhibits the FIFO (first in first out) property. insert and delete are the operations that are provided for insertion of elements into the queue and the removal of elements from the queue, respectively. Shown in Figure 3 are the effects of insert and delete operations on the queue.


Back to main directory:  Data Structure 



C program for implementation of a stack using the linked list

Filed Under: ,

Implementation of Stack using Linked List Initially the list is empty, so the top pointer is NULL. The push function takes a pointer to an existing list as the first parameter and a data value to be pushed as the second parameter, creates a new node by using the data value, and adds it to the top of the existing list. A pop function takes a pointer to an existing list as the first parameter, and a pointer to a data object in which the popped value is to be returned as a second parameter. Thus it retrieves the value of the node pointed to by the top pointer, takes the top point to the next node, and destroys the node that was pointed to by the top.
If this strategy is used for creating a stack with the previously used four data values: 10, 20, 30, and 40, then the stack is created as shown in Figure 2.
Program C program for implementation of a stack using the linked list
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *push(struct node *p, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
/* creates new node
using data value
passed as parameter */
if(temp==NULL)
{
printf("No Memory available Errorn");
exit(0);
}
temp->data = value;
temp->link = p;
p = temp;
return(p);
}

struct node *pop(struct node *p, int *value)
{
struct node *temp;
if(p==NULL)
{
printf(" The stack is empty can not pop Errorn");
exit(0);
}
*value = p->data;
temp = p;
p = p->link;
free(temp);
return(p);
}

void main()
{
struct node *top = NULL;
int n,value;
clrscr();
do
{
do
{
printf("Enter the element to be pushedn");
scanf("%d",&value);
top = push(top,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);

printf("Enter 1 to pop an elementn");
scanf("%d",&n);
while( n == 1)
{
top = pop(top,&value);
printf("The value poped is %dn",value);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output
Enter the element to be pushed
10
Enter 1 to continue
1
Enter the element to be pushed
20
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 20
Enter 1 to pop an element
1
The value poped is 10
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
30
Enter 1 to continue
1
Enter the element to be pushed
40
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 40
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
50
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 50
Enter 1 to pop an element
1
The value popped is 30
Enter 1 to pop an element
0
Enter 1 to continue
0
Back to main directory:  Data Structure 



C program to implement a stack using an array

Filed Under: ,

Welcome to mycomputerscience.net! your source for free data structures online course,data structure final exam,computer data structures and interactive data structure visualizations,array implementation of stack.
Array implementation of Stack When an array is used to implement a stack, the push and pop operations are realized by using the operations available on an array. The limitation of an array implementation is that the stack cannot grow and shrink dynamically as per the requirement.
Program A complete C program to implement a stack using an array.
#include <stdio.h>
#define MAX 10 /* The maximum size of the stack */
#include <stdlib.h>
void push(int stack[], int *top, int value)
{
if(*top < MAX )
{
*top = *top + 1;
stack[*top] = value;
}
else
{
printf("The stack is full can not push a valuen");
exit(0);
}
}
void pop(int stack[], int *top, int * value)
{
if(*top >= 0 )
{
*value = stack[*top];
*top = *top - 1;
}
else
{
printf("The stack is empty can not pop a valuen");
exit(0);
}
}
void main()
{
int stack[MAX];
int top = -1;
int n,value;
do
{
do
{
printf("Enter the element to be pushedn");
scanf("%d",&value);
push(stack,&top,value);
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
while( n == 1)
{
pop(stack,&top,&value);
printf("The value poped is %dn",value);
printf("Enter 1 to pop an elementn");
scanf("%d",&n);
}
printf("Enter 1 to continuen");
scanf("%d",&n);
} while(n == 1);
}
Output:
Enter the element to be pushed
10
Enter 1 to continue
1
Enter the element to be pushed
20
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 20
Enter 1 to pop an element
0
Enter 1 to continue
1
Enter the element to be pushed
40
Enter 1 to continue
1
Enter the element to be pushed
50
Enter 1 to continue
0
Enter 1 to pop an element
1
The value popped is 50
Enter 1 to pop an element
1
The value popped is 40 Enter 1 to pop an element
1
The value popped is 10 Enter 1 to pop an element
0
Enter 1 to continue
0
Back to main directory:  Data Structure 



Concept of Stack

Filed Under: ,

Welcome to mycomputerscience.net! your source for free data structures online course,data structure final exam,computer data structures and interactive data structure visualizations,use of stack in computer science.
[stack] A stack is simply a list of elements with insertions and deletions permitted at one end—called the stack top. That means that it is possible to remove elements from a stack in reverse order from the insertion of elements into the stack. Thus, a stack data structure exhibits the LIFO (last in first out) property. Push and pop are the operations that are provided for insertion of an element into the stack and the removal of an element from the stack, respectively. Shown in Figure 1 are the effects of push and pop operations on the stack.
Figure 1: Stack operations.
Since a stack is basically a list, it can be implemented by using an array or by using a linked representation.
There are many applications requiring the use of the data structures stacks and queues. The most striking use of a data structure stack is the runtime stack that a programming language uses to implement a function call and return. Similarly, one of the important uses of a data structure queue is the process queue maintained by the scheduler. Both these data structures are modified versions of the list data structure, so they can be implemented using arrays or linked representation.
Back to main directory:  Data Structure 



Computer data structures and interactive data structure visualizations

Filed Under:

mycomputerscience.net! your source for free data structures online course,data structure final exam,computer data structures and interactive data structure visualizations.Get data structures lab manual,learn data structures online and data structure video lecture.
[Stacks]
The Concept of Stack
Array implementation of Stack(C Program Implementation)
C program to implement a stack using an array
Implementation of a stack using linked list
C Program to implement stack using Linked list
[Queues]
The Concept of Queues
Array implementation of a queue (C implementation)
C program to implement a queue using an array
Implementation of a queue using linked list
C Program to implement queue using Linked list
[Circular Queues]
The Concept of Circular Queues
Sample C program to implement Circular Queue
[Linked List]
The Concept of Linked List
C program for building and printing the elements of the linked list
Recursive C program to insert a node into a linked list
C Program Sorting and reversing a linked list
C implementation of deleting a specified node in a singly linked list
C implementation of inserting a node after the specified node in a singly linked list
C implementation of inserting a node in a sorted list
C implementation of counting the number of nodes of a linked list
C implementation of merging of two sorted list
C implementation of erasing a linked list
C implementation of Polynomial representation of a linked list
C implementation of representation of sparse matrices
C program for building and printing the elements of the circular linked list
C Program to splitting the list with 2n nodes into two separate and equal lists
C program of merging of two circular linked list
C implementation of reverse the direction of links in a singly linked circular list
[Double Linked List]
Introduction of a Doubly Linked list
Application of Double Linked Lists to memory management
C Program to implement a double linked list
C program to insert a node in a double linked list
C program to deleting a node in a double linked list
[Infix,Postfix,Prefix]   
Introduction of Infix,Postfix,Prefix
Conversion Techniques
Algorithm to the Infix to Postfix Convertor Function
C Program to Convert Infix to Postfix
C Program to Convert Infix to Postfix using Stack
Algorithm to the Infix to Prefix Convertor Function
C Program to convert Infix to Prefix Using Stack
[Tree]
The Concept of Trees  Introduction | Definition of a Tree | Degree of a Node of a Tree.
Binary tree and it's representation Introduction | Representation of a Binary Tree.
Binary tree traversal Introduction.
Binary search tree Introduction | Program: Creating a Binary Search Tree | Explanation of problems.
Counting number of nodes ina binary search tree Introduction | Solved Program with explanation.
Swapping left and right subtrees of a given binary tree Introduction.
Searching for a target key in a binary search tree Introduction.
Deletion of a node from a binary search tree Deletion of a node with two children | Deletion of a Node with One Child | Deletion of a Node with No Child.
Application of a binary search tree