Untitled

 avatar
unknown
plain_text
3 years ago
3.1 kB
5
Indexable
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("\n...MENU...");
		printf("\n1)Push node\n2)Pop node");
		printf("\nenter 0 for EXIT");
		printf("\nEnter your option :");
		scanf("%d",&op);
		switch(op)
		{
			case 1:
				push();
				break;
			case 2:
				pop();
				break;
			case 0:
				printf("\n...BYE BYE!...");
				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;
	}
	while(ptr!=NULL)
	{
		printf("\nelement=%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 ---Main Menu--- \n");
        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");
}