linklist full

mail@pastecode.io avatar
unknown
plain_text
10 months ago
3.8 kB
8
Indexable
Never
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

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

node*head=NULL;

void infront()
{
	int data1;
	node *n = new node;
	printf("\nEnter the data: ");
	scanf("%d", &data1);
	n->data = data1;
	n->link = head;
	head = n;
}

void inmiddle()
{
	int data1, key, f=0;
	if(head == NULL)
		printf("\nUnderflow");
	else
	{
		printf("\nEnter the point: ");
		scanf("%d", &key);
		node *ptr = head;
		while(ptr->link!=NULL)
		{
			if(ptr->data == key)
			{
				node *n = new node;
				printf("Enter the data: ");
				scanf("%d", &data1);
				n->data = data1;
				n->link = ptr->link;
				ptr->link = n;
				f=1;
				break;
			}
			ptr = ptr->link;
		}
		if(f==0)
			printf("\nKey not found.");
	}
}

void inend()
{
	int data1;
	if(head == NULL)
		printf("\nUnderflow");
	else
	{
		node *n = new node;
		printf("\nEnter the data: ");
		scanf("%d", &data1);
		node *ptr = head;
		while(ptr->link!=NULL)
		{
			ptr = ptr->link;
		}
		n->data = data1;
		n->link = ptr->link;
		ptr->link = n;
	}
}

void defront()
{
	if(head == NULL)
		printf("\nUnderflow.");
	else
		head = head->link;
}

void demiddle()
{
	if(head == NULL)
		printf("\nUnderflow.");
	else
	{
		node *ptr = head;
		int key, f=0;
		printf("\nEnter the key: ");
		scanf("%d", &key);
		while(ptr->link->link != NULL)
		{
			if(ptr->data == key)
			{
				ptr->link = ptr->link->link;
				f=1;
				break;
			}
			ptr = ptr->link;
		}
		if(f==0)
			printf("Key not found.");
	}
}

void deend()
{
	if(head == NULL)
		printf("Underflow.");
	else
	{
		node *ptr = head;
		int key;
		while(ptr->link->link != NULL)
			ptr = ptr->link;
		ptr->link = ptr->link->link;
	}
}

void display()
{
	node *temp = head;
	if(head == NULL)
		printf("Underflow.");
	else
	{
		while(temp!=NULL)
		{
			printf("%d---> ",temp->data);
			temp = temp->link;
		}
	}
}
void main()   //main menu//
{
char choice, ch,in,d;
clrscr();
x:
printf("\n1.Insertion\n2.Deletion\n3. Display\nEnter Your Choice: ");
scanf("%d", &ch);
if(ch==1)
{
y:
 printf("\t\t<<Insertion Menu>>");
  printf("\n1.Insertion at front!\n2.Insertion at middle\n3.Insertion at end\n4.Display\nChoose your option");
 scanf("%d",&in);
 if(in==1)
 {
   printf("\nInsertion at front!");
   infront();
 }
 else if(in==2)
 {
   printf("\nInsertion at any position!");
   inmiddle();
 }
 else if(in==3)
 {
   printf("\nInsertion at End");
   inend();
 }
  else if(in==4)
 {
   printf("\nDisplay Operation\n");
   display();
 }
 else
 {
 printf("Invalid Option");
 }
 printf("\nDo you wish to continue in insertion menu press 10: ");
 scanf("%d",&choice);
 if(choice==10)
 {
 goto y;
 }
 else{
 goto k;
 }//insertion ends
}
else if(ch==2)    //deletion start
{
 p:
 printf("\t\t<<Deletion Menu Menu>>");
 printf("\n1.Deletion at front!\n2.Deletion at middle\n3.Deletion at end\n4.Display\nChoose your option");
 scanf("%d",&d);
 if(d==1)
 {
   printf("\nDeletion at front!");
   defront();
 }
 else if(d==2)
 {
   printf("\nDeletion at Any position!");
   demiddle();
 }
 else if(d==3)
 {
   printf("\nDeletion at End");
   deend();
 }
  else if(d==4)
 {
   printf("\nDisplay Operation\n");
   display();
 }
 else
 {
 printf("Invalid Option");
 }
 printf("\nDo you wish to continue deletion menu press 10: ");
 scanf("%d",&choice);
 if(choice==10)
 {
 goto p;
 }
 else
 {
 goto k;
 }//Deletion end
}
 else if(ch==3)
 {
   printf("\nDisplay Operation:\n");
   display();
 }
else
{
printf("Invalid option");
}
k:
printf("\nDo you wish to continue main menu press 10: ");
scanf("%d",&choice);
if(choice==10)
{
goto x;
}
getch();
}