top of page

Search Results

61 items found for ""

  • Linear Search In C

    #include void main() { int size; printf("\nEnter the size of array : "); scanf("%d",&size); int a[size]; int i=0,e,count=0; for(i=0;i

  • Stack Using Queue

    A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue operations. When an element is pushed onto the stack, it is enqueued into the enqueue queue. When an element is popped from the stack, it is dequeued from the dequeue queue along with all other elements, except the last one, and enqueued back into the enqueue queue in reverse order. The last element is then dequeued from the dequeue queue and returned. #include #include #define MAX_SIZE 100 // Define the queue data structure struct queue { int data[MAX_SIZE]; int front; int rear; }; // Initialize a queue void init_queue(struct queue *q) { q->front = -1; q->rear = -1; } // Check if the queue is empty int is_empty(struct queue *q) { return q->front == -1; } // Check if the queue is full int is_full(struct queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } // Enqueue an element void enqueue(struct queue *q, int element) { if (is_full(q)) { printf("Error: Queue is full\n"); exit(EXIT_FAILURE); } if (is_empty(q)) { q->front = 0; } q->rear = (q->rear + 1) % MAX_SIZE; q->data[q->rear] = element; } // Dequeue an element int dequeue(struct queue *q) { if (is_empty(q)) { printf("Error: Queue is empty\n"); exit(EXIT_FAILURE); } int element = q->data[q->front]; if (q->front == q->rear) { q->front = -1; q->rear = -1; } else { q->front = (q->front + 1) % MAX_SIZE; } return element; } // Define the stack data structure struct stack { struct queue enqueue_queue; struct queue dequeue_queue; }; // Initialize a stack void init_stack(struct stack *s) { init_queue(&s->enqueue_queue); init_queue(&s->dequeue_queue); } // Push an element onto the stack void push(struct stack *s, int element) { enqueue(&s->enqueue_queue, element); } // Pop an element from the stack int pop(struct stack *s) { if (is_empty(&s->enqueue_queue)) { printf("Error: Stack is empty\n"); exit(EXIT_FAILURE); } while (!is_empty(&s->enqueue_queue) && s->enqueue_queue.front != s->enqueue_queue.rear) { int element = dequeue(&s->enqueue_queue); enqueue(&s->dequeue_queue, element); } int element = dequeue(&s->enqueue_queue); struct queue temp = s->enqueue_queue; s->enqueue_queue = s->dequeue_queue; s->dequeue_queue = temp; return element; } // Main function to test the stack int main() { struct stack s; init_stack(&s); push(&s, 1); push(&s, 2); push(&s, 3); printf("%d\n", pop(&s)); printf("%d\n", pop(&s)); printf("%d\n", pop(&s)); return 0; } In this implementation, the queue struct represents a queue and the stack struct represents a stack. The push_queue is used to enqueue elements onto the stack, and the pop_queue is used to dequeue elements from the stack. When an element is popped, the implementation checks if the pop_queue is empty. If it is, it dequeues all the elements except the last one from the push_queue and enqueues them onto the pop_queue. The last element is then dequeued from the push_queue and returned as the popped element. The implementation uses the enqueue() and dequeue() functions to enqueue and dequeue elements onto and from the queues.

  • Queue Using Stack

    A queue can be implemented using two stacks in C. The basic idea is to use one stack for enqueue operations and another for dequeue operations. When an element is enqueued, it is pushed onto the enqueue stack. When an element is dequeued, it is popped from the dequeue stack. If the dequeue stack is empty, all the elements from the enqueue stack are popped and pushed onto the dequeue stack, effectively reversing the order of the elements. #include #include #define MAX_SIZE 100 // Define the stack data structure struct stack { int data[MAX_SIZE]; int top; }; // Initialize a stack void init stack(struct stack *s) { s->top = -1; } // Check if the stack is empty int is empty(struct stack *s) { return s->top == -1; } // Check if the stack is full int is full(struct stack *s) { return s->top == MAX_SIZE - 1; } // Push an element onto the stack void push(struct stack *s, int element) { if (is full(s)) { printf("Error: Stack is full\n"); exit(EXIT_FAILURE); } s->top++; s->data[s->top] = element; } // Pop an element from the stack int pop(struct stack *s) { if (is empty(s)) { printf("Error: Stack is empty\n"); exit(EXIT_FAILURE); } int element = s->data[s->top]; s->top--; return element; } // Define the queue data structure struct queue { struct stack enqueue stack; struct stack dequeue stack; }; // Initialize a queue void init queue(struct queue *q) { init stack(&q->enqueue stack); init stack(&q->dequeue stack); } // Enqueue an element void enqueue(struct queue *q, int element) { push(&q->enqueue stack, element); } // Dequeue an element int dequeue(struct queue *q) { if (is empty(&q->dequeue stack)) { while (!is empty(&q->enqueue stack)) { int element = pop(&q->enqueue stack); push(&q->dequeue stack, element); } } return pop(&q->dequeue stack); } // Main function to test the queue int main() { struct queue q; init queue(&q); enqueue(&q, 1); enqueue(&q, 2); enqueue(&q, 3); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); return 0; } In this implementation, the stack struct represents a stack and the queue struct represents a queue. The enqueue_stack is used to push elements onto the queue, and the dequeue_stack is used to pop elements from the queue. When an element is dequeued, the implementation checks if the dequeue_stack is empty. If it is, it pops all the elements from the enqueue_stack and pushes them onto the dequeue_stack in reverse order, effectively reversing the order of the elements. The implementation uses the push() and pop() functions to push and pop elements onto and from the stacks.

  • Queue Using Array

    #include #include #include #define size 5 int queue[size]; int front = -1; int rear = -1; void enque(void); void deque(void); void peek(void); void isEmpty(void); void isFull(void); void display(void); void main() { int c,e; do{ switch(c) { case 1: enque(); break; case 2: deque(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1:Enque\t 2:Deque\t 3:Peek\t 4:isEmpty\t 5:isFull\t 6:Display\t 7:Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); } void enque() { int v; printf("\nEnter value : "); scanf("%d",&v); if(rear==size-1) { printf("\nQueue is full!!"); } else if(front=-1 && rear==-1) { front=rear=0; queue[rear]=v; } else { rear++; queue[rear]=v; } } void display() { int i=0; if(front==-1 && rear==-1) { printf("\nQueue is empty"); } else { for(i=front;i

  • Queue Using Single Pointer Linked List

    #include #include #include #define size 5 int top=-1; void enque(void); void deque(void); void peek(void); void isEmpty(void); void isFull(void); void display(void); struct queue{ int data; struct node *next; }; struct queue *head=0; void main() { int c,e; do{ switch(c) { case 1: enque(); break; case 2: deque(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1.Enque\t2.Deque\t3.Peek\t4.isEmpty\t5.isFull\t6.Display\t7.Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); } void enque() { struct queue *newnode,*temp; if(topdata); newnode->next=0; top++; if(head==0) { head=temp=newnode; } else { temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } } else { printf("\nQueue is full"); } } void display() { struct queue *temp; temp=head; while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } } void deque() { struct queue *temp,*prevnode; printf("\n\n----Deletion From End----"); temp=head; top--; head=head->next; free(temp); printf("\nTop : %d",top); } void peek() { struct queue *temp; temp=head; while(temp->next!=0) { temp=temp->next; } printf("\nTop : %d",top); printf("\nPeek : %d",temp->data); } void isEmpty() { if(top==-1) { printf("\nQueue is empty"); } else { printf("\nQueue is not empty"); } } void isFull() { if(top==size-1) { printf("\nQueue is full"); } else { printf("\nQueue is not full"); } }

  • Queue Using Single Pointer Array

    #include #include #include #define size 100 int queue[size]; int top=-1; void enque(void); void deque(void); void peek(void); void isEmpty(void); void isFull(void); void display(void); void main() { int c,e; do{ switch(c) { case 1: enque(); break; case 2: deque(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Enque\n2 : Deque\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); } void enque() { int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nQueue is full!!"); } else { top++; queue[top]=v; } } void display() { int i; if(top==-1) { printf("\nQueue is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",queue[i]); } } } void deque() { if(top==-1) { printf("\nQueue is empty!!"); } else { int i; for(i=0;i<=top;i++) { queue[i]=queue[i+1]; } top--; } } void peek() { printf("\nTop : %d",top); printf("\nTop Value: %d",queue[top]); } void isEmpty() { if(top==-1) { printf("\nQueue is empty!!"); } else { printf("\nQueue is not empty!!"); } } void isFull() { if(top==size-1) { printf("\nQueue is full!!"); } else { printf("\nQueue is not full!!"); } }

  • Minimum & Maximum Element in a Stack

    #include #include #include #define size 100 int stack[size]; int top=-1; void push(void); void pop(void); void peek_top(void); void isEmpty(void); void isFull(void); void display(void); void max(void); void min(void); void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: max(); break; case 8: min(); break; case 9: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1) Push\t2) Pop\t3) Peek\t4) isEmpty\t5) isFull\t6) Display\t7) Maximum\t8) Minimum\t9) Exit"); printf("\n\nEnter your choice : "); scanf("%d",&c); }while(c); } void push() { int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nStack is full!!"); } else { top++; stack[top]=v; } } void display() { int i; if(top==-1) { printf("\nStack is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",stack[i]); } } } void pop() { if(top==-1) { printf("\nStack is empty!!"); } else { top--; } } void peek_top() { printf("\nTop : %d",top); printf("\nTop Value: %d",stack[top]); } void isEmpty() { if(top==-1) { printf("\nStack is empty!!"); } else { printf("\nStack is not empty!!"); } } void isFull() { if(top==size-1) { printf("\nStack is full!!"); } else { printf("\nStack is not full!!"); } } void max() { int mx=stack[0]; int i; for(i=0;i<=top;i++) { if(mxstack[i]) { mn=stack[i]; } } printf("\nMinimum value : %d",mn); }

  • Stack Using Linked List

    #include #include #include #define size 5 int top=-1; void push(void); void pop(void); void peek_top(void); void isEmpty(void); void isFull(void); void display(void); struct stack{ int data; struct node *next; }; struct stack *head=0; void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display\n7 : Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); } void push() { struct stack *newnode,*temp; if(topdata); newnode->next=0; top++; if(head==0) { head=temp=newnode; } else { temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } } else { printf("\nStack is full"); } } void display() { struct stack *temp; temp=head; while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } } void pop() { struct stack *temp,*prevnode; printf("\n\n----Deletion From End----"); temp=head; top--; while(temp->next!=0) { prevnode=temp; temp=temp->next; } if(temp==head) { head=0; } else { prevnode->next=0; } free(temp); printf("\nTop : %d",top); } void peek_top() { struct stack *temp; temp=head; while(temp->next!=0) { temp=temp->next; } printf("\nTop : %d",top); printf("\nPeek : %d",temp->data); } void isEmpty() { if(top==-1) { printf("\nStack is empty"); } else { printf("\nStack is not empty"); } } void isFull() { if(top==size-1) { printf("\nStack is full"); } else { printf("\nStack is not full"); } }

  • Stack Using Array

    #include #include #include #define size 100 int stack[size]; int top=-1; void push(void); void pop(void); void peek_top(void); void isEmpty(void); void isFull(void); void display(void); void main() { int c,e; do{ switch(c) { case 1: push(); break; case 2: pop(); break; case 3: peek_top(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1 : Push\n2 : Pop\n3 : Peek\n4 : isEmpty\n5 : isFull\n6 : Display"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); } void push() { int v; printf("\nEnter value : "); scanf("%d",&v); if(top==size-1) { printf("\nStack is full!!"); } else { top++; stack[top]=v; } } void display() { int i; if(top==-1) { printf("\nStack is empty"); } else { for(i=0;i<=top;i++) { printf("--%d",stack[i]); } } } void pop() { if(top==-1) { printf("\nStack is empty!!"); } else { top--; } } void peek_top() { printf("\nTop : %d",top); printf("\nTop Value: %d",stack[top]); } void isEmpty() { if(top==-1) { printf("\nStack is empty!!"); } else { printf("\nStack is not empty!!"); } } void isFull() { if(top==size-1) { printf("\nStack is full!!"); } else { printf("\nStack is not full!!"); } }

  • Doubly Circular Linked List

    A doubly circular linked list is a type of linked list where each node has both a "next" and a "previous" pointer, allowing for traversal in both directions. Additionally, the last node in the list points back to the first node, forming a circle. #include #include struct Node { int data; struct Node* next; struct Node* prev; }; struct DoublyCircularLinkedList { struct Node* head; int count; }; struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; node->prev = NULL; return node; } struct DoublyCircularLinkedList* create_list() { struct DoublyCircularLinkedList* list = (struct DoublyCircularLinkedList*)malloc(sizeof(struct DoublyCircularLinkedList)); list->head = NULL; list->count = 0; return list; } void append(struct DoublyCircularLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; node->next = node; // make the node point to itself node->prev = node; } else { node->next = list->head; node->prev = list->head->prev; list->head->prev->next = node; list->head->prev = node; } list->count++; } int remove_node(struct DoublyCircularLinkedList* list, int data) { if (list->head == NULL) { return 0; } struct Node* current = list->head; do { if (current->data == data) { if (current == list->head) { list->head = current->next; } current->prev->next = current->next; current->next->prev = current->prev; free(current); list->count--; return 1; } current = current->next; } while (current != list->head); return 0; } void print_list(struct DoublyCircularLinkedList* list) { if (list->head == NULL) { return; } struct Node* current = list->head; do { printf("%d ", current->data); current = current->next; } while (current != list->head); printf("\n"); } int main() { struct DoublyCircularLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0; }

bottom of page