Posts

Queue using Linked List

Queue using linked list /*******************************************************************************/ #include<stdio.h> #include<conio.h> #include <stdlib.h> struct Node {  int data;  struct Node *next; }*front=NULL,*rear=NULL; void enqueue() {  struct Node *newnode=(struct Node*)malloc(sizeof(struct Node));  printf("Enter data:");  scanf("%d",&newnode->data);  newnode->next=NULL;  if(front==NULL)  {   front=newnode;   rear=newnode;  }  else  {   rear->next=newnode;   rear=newnode;  } } int dequeue() {     if (front==NULL)        {printf("Queue is empty...underflow");return -1;}     else     {      struct Node *temp;      temp=front;      int rv=front->data;      front=front->next;      if(front==NULL) rear=NULL;      free(temp);      return(rv);     } } void displayQ() {  struct Node *temp=front;  while(temp!=NULL)  {   printf(" %d---->",temp->data);   temp=temp->next;  }  printf(" NULL"

Singly Linked List Implementation

Singly Linked List /**************************************************************************/ #include<stdio.h> #include<stdlib.h> struct Node {  int data;  struct Node *next; }*head=NULL,*tail=NULL,*temp=NULL,*trav=NULL,*prev=NULL; void create() {  temp=(struct Node*)malloc(sizeof(struct Node));  printf("Enter data:");  scanf("%d",&temp->data);  temp->next=NULL;  if(head==NULL)  {   head=temp;   tail=temp;  }  else  {   tail->next=temp;   tail=temp;  } } void display() {  temp=head;  while(temp!=NULL)  {   printf(" %d---->",temp->data);   temp=temp->next;  }  printf(" NULL"); }  void insert_begin()  {      temp=(struct Node*)malloc(sizeof(struct Node));      printf("Enter data:");      scanf("%d",&temp->data);      temp->next=NULL;      if(head==NULL)      {         head=temp;          tail=temp;      }      else      {          temp->next=head;          head=temp;      }     }

Stack using Linked List

Stack using linked list /*******************************************************************************/ #include<stdio.h> #include<conio.h> #include <stdlib.h> struct Node {  int data;  struct Node *next; }; struct Node *top=NULL; void push(int value) {     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));     newNode->data = value;     newNode->next = top;     top = newNode; } int pop() {     if (top == NULL) {         printf("Stack is empty.\n");         return -1;  // or any value to indicate an error     }     int poppedValue = top->data;     struct Node* temp = top;     top = top->next;     free(temp);     return poppedValue; } void displayStack() {     if (top == NULL) {         printf("Stack is empty.\n");     } else {         struct Node* temp = top;         printf("Stack content:\n");         while (temp != NULL) {             printf("%d\n", temp->data);             temp = temp->next;

Infix to Prefix conversion using Stack

  #include<stdio.h> #include<math.h> #include<string.h> #include <stdlib.h> #define MAX 120 void push(int); char pop(); void infix_to_prefix(); int precedence (char); char *strrev(char *str); char stack[120],infix[120],prefix[120]; int top = -1; int main() { printf("\nINPUT THE INFIX EXPRESSION : "); scanf("%s",infix); infix_to_prefix(); return 0; } // method that pushes the elements onto the character stack void push(int pos) { if(top == MAX-1) { printf("\nSTACK OVERFLOW\n"); } else { top++; stack[top] = infix[pos]; }} // method that removes character from stack and returns them char pop() { char ch; if(top < 0) { printf("\nSTACK UNDERFLOW\n"); exit(0); } else { ch = stack[top]; stack[top] = '\0'; top--; return(ch); } return 0; } // method that converts String from infix to prefix // all the strings are assumed to be valid expressions void infix_to_prefix() { int i = 0,j = 0; strrev(infix); // Reverse the infix

Evaluation of Postfix Expression

Evaluation of a Postfix Expression ( single digit) #include <stdio.h> #include <ctype.h> #define MAXSTACK 100  #define POSTFIXSIZE 100  /* declare stack and its top pointer to be used during postfix expression evaluation*/ int stack[MAXSTACK]; int top = -1;  /* define push operation */ void push(int item) {     if (top >= MAXSTACK - 1) {         printf("stack over flow");         return;     }     else {         top = top + 1;         stack[top] = item;     } } /* define pop operation */ int pop() {     int item;     if (top < 0) {         printf("stack under flow");     }     else {         item = stack[top];         top = top - 1;         return item;     } } /* define function that is used to input postfix expression and to evaluate it */ void EvalPostfix(char postfix[]) {     int i;     char ch;     int val;     int A, B;     /* evaluate postfix expression */     for (i = 0; postfix[i] != '$'; i++) {         ch = postfix[i];         if

Infix To Postfix Conversion using Stack

Infix to postfix conversion #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> #define SIZE 100 /* declared here as global variable because stack[] * is used by more than one fucntions */ char stack[SIZE]; int top = -1; /* define push operation */ void push(char item) { if(top >= SIZE-1) { printf("\nStack Overflow."); } else { top = top+1; stack[top] = item; } } /* define pop operation */ char pop() { char item ; if(top <0) { printf("stack under flow: invalid infix expression"); getchar(); /* underflow may occur for invalid expression */ /* where ( and ) are not matched */ exit(1); } else { item = stack[top]; top = top-1; return(item); } } /* define function that is used to determine whether any symbol is operator or not (that is symbol is operand) * this fucntion returns 1 if symbol is opreator else return 0 */ int is_operator(char symbol) { if(s

Double Ended Queue

Double Ended Queues C Program- Double ended queue using arrays /***********************************************************************/ #include<stdio.h> #include<stdlib.h> #define MAX 10   int deque_arr[MAX]; int front=-1; int rear=-1;   void insertFront(int item); void insertRear(int item); int deleteFront(); int deleteRear(); void display(); int isEmpty(); int isFull();   int main() {         int choice,item;         while(1)         {                 printf("\n\n1.Insert at the front end\n");                 printf("2.Insert at the rear end\n");                 printf("3.Delete from front end\n");                 printf("4.Delete from rear end\n");                 printf("5.Display\n");                 printf("6.Quit\n");                 printf("\nEnter your choice : ");                 scanf("%d",&choice);                   switch(choice)                 {                 case 1:      

Priority Queue using Arrays/heap/Linked List

 Priority Queues #include <stdio.h> #include <stdlib.h> #define MAX 50   void insert(int); void delete(int); void create(); void check(int); void display();   int prique[MAX]; int front, rear;   void main() {     int n, ch;       printf("\n1 - Insert an element into queue");     printf("\n2 - Delete an element from queue");     printf("\n3 - Display queue elements");     printf("\n4 - Exit");       create();       while (1)     {         printf("\nEnter your choice : ");             scanf("%d", &ch);           switch (ch)         {         case 1:              printf("\nEnter value to be inserted : ");             scanf("%d",&n);             insert(n);             break;         case 2:             printf("\nEnter value to delete : ");             scanf("%d",&n);             delete(n);             break;         case 3:              display();             break;