0% found this document useful (0 votes)
20 views5 pages

Google Keep Document

Uploaded by

mtk9427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Google Keep Document

Uploaded by

mtk9427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<stdio.

h>
//#include<conio.h>
#include<stdlib.h>
// User define functions
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAtSpecific(int,int,int);
void display();
void deleteAtBeginning();
void deleteAtEnd();
void deleteAtSpecific(int);

struct Node //self reference structure


{
int data;
struct Node *next;
}*head = NULL;

void main()
{
int choice,value,choice1,pos1,pos2;
// clrscr();
while(1){
mainMenu:
printf("\n\n---------------- MENU ------------");
printf("n1. Insert\n2. Display\n3. Delete\n4. Exit\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
while(1){
printf("Where you want to insert: \n 1. At Beginning \n2. At End \n3. Between \n Enter your
choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the two values where you wanto insert: ");
scanf("%d%d",&pos1,&pos2);
insertAtSpecific(value,pos1,pos2);
break;
default: printf("\nEntercorrect Options !!!\n\n");
goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3:
printf("How do you want to Delete: \n1. From Beginning\n2. From End\n3. Spesific\nEnter your
choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: deleteAtBeginning();
break;
case 2: deleteAtEnd(value);
break;
case 3: printf("Enter the value which you wanto delete: ");
scanf("%d",&pos2);
deleteAtSpecific(pos2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
break;
case 4:
exit(0);
default:
printf("\nWrong input!!! Try again!!\n\n");
}
}
}

void insertAtBeginning(int value)


{
struct Node *newNode; //create new node
newNode = (struct Node*)malloc(sizeof(struct Node)); //Memory allocation for new node
newNode->data = value; // assign value to new node
if(head == NULL) // to check wether the list is empty or not

{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");
}
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("\nOne node inserted!!!\n");
}
void insertAtSpecific(int value, int pos1, int pos2)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head; //initilize temp ptr with head
while(temp->data != pos1 && temp->data != pos2){
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
}
printf("\nOne node inserted!!!\n");
}

void deleteAtBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
void deleteAtEnd()
{
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}
void deleteAtSpecific(int delValue)
{
struct Node *temp1 = head, *temp2;
while(temp1->data != delValue)
{
if(temp1 -> next == NULL){
printf("\nGiven node not found in the list!!!");
goto functionEnd;
}
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = temp1 -> next;
free(temp1);
printf("\nOne node deleted!!!\n\n");
functionEnd:
}
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy