Ds
Mon Dec 12 2022 07:42:26 GMT+0000 (Coordinated Universal Time)
Saved by @ishraq01
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
struct node{
int info;
struct node *next;
};
struct node *getnode()
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
if (p == NULL)
printf("\nNEW NODE CANNOT BE GENERATED");
else
return (p);
}
void insert_start(struct node **q)
{
struct node *p;
p = getnode();
if (p != NULL)
{
printf("\nENTER THE NODE INFO VALUE:");
scanf("%d", &p->info);
p->next = *q;
*q = p;
}
}
void delete_start(struct node **q)
{
int x;
struct node *p;
if (*q == NULL)
printf("\nLINK LIST IS EMPTY");
else
{
p = *q;
x = p->info;
*q = p->next;
free(p);
}
}
void delete_q(struct node **q)
{
int x;
struct node *p;
if (*q == NULL)
printf("\nQUEUE IS EMPTY");
else
{
p = *q;
x = p->info;
*q = p->next;
free(p);
}
}
void pop(struct node **q)
{
int x;
struct node *p;
if (*q == NULL)
printf("\nLINK LIST IS EMPTY");
else
{
p = *q;
x = p->info;
printf("%d is deleted",x);
*q = p->next;
free(p);
}
}
void insert_last(struct node **q)
{
struct node *p, *temp;
p = getnode();
if (p != NULL)
{
printf("\nENTER THE NODE INFO VALUE:");
scanf("%d", &p->info);
temp = *q;
if (*q == NULL)
{
*q = p;
p->next = NULL;
}
else
{
while (temp->next != NULL)
temp = temp->next;
p->next = temp->next;
temp->next = p;
}
}
}
void insert_at(struct node **q)
{
int loc, i;
struct node *p,*t=*q;
printf("\nENTER THE LOCATION OF A NODE:");
scanf("%d", &loc);
for (i = 1; i <= loc; i++)
t= t->next;
if (t== NULL)
printf("\nYOU HAVE LESS NUMBER OF NODES:");
else
{
p = getnode();
if (p != NULL)
{
printf("\nENTER THE NODE INFO VALUE:");
scanf("%d", &p->info);
p->next = t->next;
t->next = p;
}
}
}
void delete_at(struct node **q)
{
struct node *prev, *p;
int i, x, loc;
if (*q == NULL)
printf("\nLINK LIST IS EMPTY");
else
{
printf("\nENTER THE LOCATION:");
scanf("%d", &loc);
p = *q;
if (loc == 1)
{
x = p->info;
*q = p->next;
}
else
{
for (i = 1; i < loc; i++)
{
prev = p;
p = p->next;
}
if (p == NULL)
printf("\nLINK LIST HAS LESS NUMBER OF NODES");
else
{
x = p->info;
prev->next = p->next;
free(q);
}
}
}
}
void delete_last(struct node **q)
{
struct node *p = *q, *prev;
//int x;
if (*q == NULL){
printf("\nLINK LIST IS EMPTY");
return;
}
if (p->next == NULL)
{
printf("%d deleted",p->info);
*q=NULL;
return;
}
else
{
while (p->next != NULL)
{
prev = p;
p = p->next;
}
printf("%d is deleted",p->info);
prev->next = NULL;
free(p);
}
}
void traverse(struct node *q)
{
if (q == NULL)
printf("\nLINK LIST IS EMPTY");
else
{
while (q != NULL)
{
printf("%d\t", q->info);
q = q->next;
}
}
}
void count(struct node *q)
{
int count = 0;
if (q == NULL)
printf("\nTHERE IS NO NODE");
else
{
while (q != NULL)
{
count = count + 1;
q = q->next;
}
}
printf("\nNUMBER OF NODES ARE:%d", count);
}
int main()
{
struct node *list;
list = NULL;
int choice;
char ch;
//clrscr();
while (1)
{
printf("\n1.INSERT OPERTATION AT START:");
printf("\n2.DELETE OPERATION AT START:");
printf("\n3.INSERT OPERATION AT LAST:");
printf("\n4.DELETE OPERTAION AT LAST:");
printf("\n5.INSERT OPERATION AT GIVEN LOCATION:");
printf("\n6.DELETE OPERATION AT GIVEN LOCATION:");
printf("\n7.COUNT THE NUMBER OF NODES:");
printf("\n8.TRAVERSE THE LINK LIST:");
printf("\n9.PUSH OPERATION AT STACK:");
printf("\n10.DELETE OPERATION AT STACK:");
printf("\n11.INSERT OPERATION AT QUEUE:");
printf("\n12.DELETE OPERATION AT QUEUE:");
printf("\n13.EXIT:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_start(&list);
break;
case 2:
delete_start(&list);
break;
case 3:
insert_last(&list);
break;
case 4:
delete_last(&list);
break;
case 5:
insert_at(&list);
break;
case 6:
delete_at(&list);
break;
case 7:
count(list);
break;
case 8:
traverse(list);
break;
case 9:
insert_start(&list);
break;
case 10:
pop(&list);
break;
case 11:
insert_start(&list);
break;
case 12:
delete_q(&list);
case 13:
exit(0);
break;
default:
printf("\nWRONG CHOICE");
}
}
return 0;
}



Comments