Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.6 kB
2
Indexable
Never
stack

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
    int x;
    struct node *next;
} * top;
void push();
void pop();
void peep();
int main()
{
    int op;
    do
    {

        printf("\n1) To Push node\n2) To Pop node");
        printf("\nEnter 0 for EXIT");
        printf("\nEnter your option :\n");
        scanf("%d", &op);
        switch (op)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 0:
            printf("\nEnd");
            break;
        default:
            printf("\nInvalid option");
            break;
        }
    } while (op != 0);
    return 0;
}
void push()
{
    struct node *ptr;
    ptr = (struct node *)malloc(sizeof(struct node));
    if (ptr == NULL)
    {
        printf("\nstack overflow");
        return;
    }
    printf("\nPush element in node=");
    scanf("%d", &ptr->x);
    ptr->next = top;
    top = ptr;
    peep();
}
void pop()
{
    struct node *ptr;
    ptr = top;
    if (top == NULL)
    {
        printf("\nStack is empty");
        return;
    }
    printf("\n%d is poped ", ptr->x);
    top = top->next;
    free(ptr);
    peep();
}
void peep()
{
    struct node *ptr;
    ptr = top;
    if (top == NULL)
    {
        printf("\nStack is empty");
        return;
    }
    printf("Element");
    while (ptr != NULL)
    {
        printf("\n%d", ptr->x);
        ptr = ptr->next;
    }
}

queue
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};
typedef struct node node;

node *front;
node *rear;
void insert();
void del();
void display();

int main ()
{
    int choice;
    while(choice != 4)
    {
        printf("\n 1.Insert an element\n 2.Delete an element\n 3.Exit\n ");
        printf("\n Enter your choice (1-3) :");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1: insert();
                    display();
                    break;
            case 2: del();
                    display();
                    break;
            case 3: exit(0);
                    break;
            default: printf("\n Invalid Choice ! \n");
        }
    }
}

void insert()
{
    struct node *ptr;
    int item;

    ptr = (struct node *) malloc (sizeof(struct node));
    if(ptr == NULL)
    {
        printf("\n Overflow \n");
        return;
    }
    else
    {
        printf("\n Enter value : \n");
        scanf("%d", &item);
        ptr -> data = item;
        if(front == NULL)
        {
            front = ptr;
            rear = ptr;
            front -> next = NULL;
            rear -> next = NULL;
        }
        else
        {
            rear -> next = ptr;
            rear = ptr;
            rear->next = NULL;
        }
    }
}

void del()
{
    struct node *ptr;
    if(front == NULL)
    {
        printf("\n Underflow \n");
        return;
    }
    else
    {
        printf("\n %d is Deleted ", front -> data);
        ptr = front;
        front = front -> next;
        free(ptr);
    }
}

void display()
{
    struct node *ptr;
    ptr = front;
    if(front == NULL)
    {
        printf("\n Queue is Empty !! ");
    }
    else
    {   printf("\n Queue :\n");
        while(ptr != NULL)
        {
            printf(" %d ", ptr -> data);
            ptr = ptr -> next;
        }
    }
    printf("\n");
}