Posts

Showing posts from September 17, 2017

Queue Implementation Using Linked List

Image
Queue implementation using linked list gives following advantages: Linked list allow dynamic allocation of memory Insertion and deletion is easy  Efficient memory utilization due to run time allocation Deletion of elements free the memory, that can further utilized by other programs Implementation of Queue First step to define the structure of the node Define two pointer called rear and front assigned with NULL Create the List of Operation in Main function Decide the condition for Queue empty or full Last inserted elements always pointed by rear #include #include struct node { int data; struct  node *next; }*front=NULL,*rear=NULL; void Inqueue() { int n; struct node *newNode=(struct node*)malloc(sizeof(struct node)); printf("Insert the data"); scanf("%d",&n); newNode->data=n; if(rear==NULL) { front=newNode; rear=newNode; newNode->next=NULL; } else { rear->next=newNode; rear=newNode; } } void Dequeue() { if(front==NULL) printf(&q

Queue Implementation Using Array

Image
Queue is an another Abstract Data Type, where insertion and deletion can be performed from the one end. In Queue Data Structure, the elements are inserted and deleted in the First in First Out fashion. In Queue, the insertion is done using rear and deletion is performed through front Applications of Queue Network Programming Operating System Process Scheduling Waiting Queue Access the Shared resource (printer) Multiprograming Basic Operation of Queue EnQueue Operation (Insert the data) DeQueue Operation (Deletion of data) EnQueue Operation DeQueue Operation Queue Implementation Array Implementation Linked List Implementation Array Implementation of Queue #include #include int max=10; int a[10]; int front=-1,rear=-1; void Inqueue() { if(rear==max) printf("Queue is full"); else { int n; if(front=-1) front=0; rear=rear+1; printf("Insert the data\n"); scanf("%d",&n); a[rear]=n; printf(&quo

Conversion of Arithmetic Expression Using Stack

Image
Stack has various application as we have discussed in the previous post. In this post, we will see how stack is useful to convert the user input expression into a meaningful result. There are basically three types of expression: Infix expression Postfix expression Prefix expression We generally write any equation in the form of infix expression, but computer system convert this expression into Post-fix or Prefix expression for evaluating the results. Example: Infix expressions:      A+B , X*(Y+Z)                 Postfix expression:    AB+ , XYZ+*                 Prefix expression:     +AB, *X+YZ The transformation  of any Infix expression into Postfix expression is performed based on the precedence of the operators.  The higher precedence operator evaluation is performed first, later the others operators.   If two operator precedence is equal then we will evaluate the expression left to right,  whichever operator coming first evaluate it. Operator Pr

Stack Implementation Using Linked-List

Image
The stack implementation using the linked list. Advantages: Always takes constant time to push and pop the elements Can grow to infinite size Disadvantages: More time take to find the k th element in the list Require more memory storage for each data element Implementation of Stack using the Linked List First step is to define the node structure Variable name called TOP for indexing  Check the condition for Empty and Full of the list Make a function to display the List elements Make a function for PUSH and POP operation #include #include struct node { int data; struct node *next; }*top=NULL; void push() { int n; struct node *newNode; newNode=(struct node*)malloc(sizeof(struct node)); printf("enter the data\n"); scanf("%d",&n); newNode->data=n; if (top==NULL) newNode->next=NULL; else newNode->next=top; top=newNode; printf("insert is sucessfull"); } void pop() { if (top==NULL) printf(&quo

Stack Abstract Data Structure with Array Implementation

Image
Stack is a abstract data type which store the elements in a particular order called Last in first out (LIFO), where insertion is called PUSH operation and deletion called POP operation. The basic operations of stack are: PUSH insert the element into the stack POP deletion of the element from the stack PEEK return the TOP item of the stack Stack implementation can be possible using: Linked List Array Array implementation of stack  has following advantages and disadvantages: Advantages: Best Performance Disadvantage: Fixed Size Stack Applications Compiler (parsing the data between the brackets) Operating system (program stack) Artificial intelligence(finding the path) Stack implementation using an array: First define the size of the array Take a index variable called TOP=-1 If TOP==MAX then stack is full If TOP==-1 stack is empty PUSH operation is performed with increment of TOP POP operation is performed with TOP decrements #inc