You are on page 1of 7

//Program no: 9

//Program to insert and delete numbers in a list

A linked list (or more clearly, "singly-linked list") is a data structure that consists of a sequence
of nodes each of which contains a reference (i.e., a link) to the next node in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node

Linked lists are among the simplest and most common data structures. They can be used to
implement several other common abstract data structures, including stacks, queues, associative
arrays, and symbolic expressions, though it is not uncommon to implement the other data
structures directly without using a list as the basis of implementation.

The singly-linked list is the most basic of all the pointer-based data structures. A singly-linked list is
simply a sequence of dynamically allocated storage elements, each containing a pointer to its successor.
Despite this obvious simplicity, there are myriad implementation variations.

In the program below, move pointer points to the current node and prev pointer points to the previous
node, start pointer points to the first node of the list, tmp and tmpp are the pointers used during the
insertion of a node. AGAIN,A and B are the labels used to shift control to various points through goto
statement. bool is a Boolean function used to check whether the item to be deleted is present in the list
or not.

//Program
#include<iostream.h>
struct node //structure of a node
{
int info;
node *next;
};
int main() //main function
{
int opt,item,bool=0,ins;
node *start=NULL; node *move=NULL; node *prev=NULL; node *tmp=NULL;
node *tmpp=NULL;

31 | P a g e
tmpp=new node; start=new node; tmp=new node;
//creation of linked list
move=new node; prev=new node; start=NULL;
if(start==NULL) //first node
{
cout << "\nenter the value of node\n";
cin>>move->info;
move->next=NULL;
start=move;
cout<<"\nFirst node inserted in the list ";
}
AGAIN:
cout << "ENTER CHOICE:\n1. insertion\n2. deletion\n3. Display\n4.exit\n";
cin >> opt;
switch (opt)
{
case 1: //insertion of a node
{
cout << "\ninsertion\n";
cout<<"\nEnter the value\n";
cin >> ins;
move = start;
prev=NULL;
if(move->info<ins)
{
A:
prev=move;

32 | P a g e
if( move->next!=NULL)
{
move=move->next;
if(move->info<ins)
goto A;
tmpp->info=ins;
tmpp->next=prev->next;
prev->next=tmpp;
}
if(move->next==NULL)
{
move->next=new node;
move=move->next;
move->info=ins; //ins(value) inserted at the end
move->next=NULL;
}
}
else if(move->info>ins && move !=start )
{
prev->next=new node;
tmp=prev->next;
tmp->info=ins;
tmp->next=move;
}
else if (move==start && move->info>ins) //first node
{
tmp->info=ins;

33 | P a g e
tmp->next=start;
start=tmp;
}
goto AGAIN;
}
case 2: //deletion of a node
{
cout<<"\nenter the element to be deleted\n";
cin>>item;
move = start;
B:
if(move->info==item && move==start) //delete the first node
{
move=move->next;
start=move;
bool=1;
}
if(move->info==item && move!=start)
{
prev->next=move->next;
move=move->next;
bool=1;
}
else if(move->next!=0) //point to next node
{
prev=new node;
prev=move;

34 | P a g e
move=move->next;
prev->next=move;
goto B;
}
else if(bool=0)
cout<<"\nelement not found in the list\n";
goto AGAIN;
}
case 3: //display of the list
{
move=start;
cout<<"\nlist:\n" ;
do
{
cout<<move->info<<"\n";
move=move->next;
}
while(move->next!=NULL);
cout<<move->info<<"\n"; //last node
goto AGAIN;
}
case 4: //exit
break;
default:
{cout<<"\nwrong choice\n";
goto AGAIN;}
}

35 | P a g e
return 0;
}

Output:

36 | P a g e
37 | P a g e

You might also like