0% found this document useful (0 votes)
48 views6 pages

#Include #Include #Include #Include Using Namespace Struct Int

This document defines functions for creating and manipulating a singly linked list (SLL) in C++. It includes functions for: - Creating the first node of an SLL - Inserting nodes at the beginning, end, or middle of an SLL - Printing all nodes of an SLL - Inserting a node at any specified location in an SLL - Deleting nodes from the beginning, end, or middle of an SLL - Deleting a node from any specified location in an SLL - A main function that allows the user to insert, delete, and print nodes by selecting options from a menu

Uploaded by

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

#Include #Include #Include #Include Using Namespace Struct Int

This document defines functions for creating and manipulating a singly linked list (SLL) in C++. It includes functions for: - Creating the first node of an SLL - Inserting nodes at the beginning, end, or middle of an SLL - Printing all nodes of an SLL - Inserting a node at any specified location in an SLL - Deleting nodes from the beginning, end, or middle of an SLL - Deleting a node from any specified location in an SLL - A main function that allows the user to insert, delete, and print nodes by selecting options from a menu

Uploaded by

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

#include<conio.

h>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>

using namespace std;


struct node
{int num;
node *next;
};

//&&&&&&&&&&&&& Create First Node In SLL &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


void first_node(node* &head,int n)
{
head=new node;
head->num=n;
head->next=NULL;
}
//&&&&&&&&&&&&& Insert Node In Beginning &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void inbegining(node* &head,int n)
{
if(head==NULL)
first_node(head,n);
else
{node *temp,*q;
q=head;
temp=new node;
temp->num=n;
temp->next=q;
head=temp;
}
}
//&&&&&&&&&&&& Insert Node In End &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void inend(node* &head,int n)


{
if(head==NULL)
first_node(head,n);
else
{
node *p,*q;
p=head;
while(p->next!=NULL)
p=p->next;
q=new node;
q->num=n;
q->next=NULL;
p->next =q;
}
}
//&&&&&&&&&&&&& Insert Node In Middle &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void inmiddle(node* &head,int n,int k)


{ if(head==NULL)
first_node(head,n);
else
{int i;
node *p,*d,*x;

p=head;
for(i=0;i<k;i++)
{x=p;
p=p->next;
}
d=new node;
d->num=n;
d->next=p;
x->next=d;
}
}
//&&&&&&&&&& Print SLL &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void print(node* head)


{node *p;
p=head;
/*while(p!=NULL)
{
cout<<p->num<<" --> ";
p=p->next;
} */
/*if (p==NULL)
cout<<"null";
else{
cout<<p->num<<"->";
print(p->next);}
*/
for (node *p=head;p!=NULL;p=p->next)
cout<<p->num<<" --> ";

}
//&&&&&&&&&&& Insert In Any Location &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void inany_location(node* &head,int n,int k)


{int count=0;
node *p;
p=head;
while(p!=NULL)
{count++;
p=p->next;
}
if(k==1)
{
cout<<"the inbegining\n";
inbegining(head,n);
}
else
if(k==count+1)
{
cout<<"the inend\n";
inend(head,n);
}
else
if(k<count)
{
cout<<"the inmiddle\n";
inmiddle(head,n,k);
}
else
if(k>count)
cout<<"the is not found node error\n";
}
//&&&&&&&&&&&&& Insertion &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void insertion(node* &head)
{
int ch,k,j,x,m,g,n;
while(8)
{
cout<<"1::inbeggining\n";
cout<<"2::inend\n";
cout<<"3::inmiddle\n";
cout<<"4::inany location\n";
cout<<"5::exit\n";
cout<<"choice the number\n";
cin>>ch;
switch(ch)
{case 1:cout<<"the inbeggining and number\n";
cin>>n;
inbegining(head,n);
break;
case 2:cout<<"the inend and number\n";
cin>>x;
inend(head,x);
break;
case 3:cout<<"the inmiddle\n";
cout<<"enter number\n";
cin>>g;
cout<<"the location\n";
cin>>k;
inmiddle(head,g,k);
break;
case 4:cout<<"in any location \n";
cout<<"enter number\n";
cin>>m;
cout<<" location\n";
cin>>j;
inany_location(head,m,j);
break;
case 5:cout<<"exit\n";
break;
} break;}
}
//&&&&&&&&&&&& Del From begin &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void debeggining(node* &head)


{
node *p;
if(head==NULL)
cout<<"error\n";
else
{p=head;
head=head->next;
delete p;
}
}
//*********** Delete from End *************************************

void deend(node* &head)


{
node *p,*x,*d;
if(head==NULL)
cout<<"error\n";
else
{p=head;
while(p->next!=NULL)
{
x=p;
p=p->next;
}
d=p;
x->next=NULL;
delete d;
}
}
//************ Delete From Middle **************************************

void demiddle(node* &head,int l)


{
int i;
node *x,*p;
p=head;
for(i=0;i<l;i++)
{x=p;
p=p->next;
}
x->next=p->next;
delete p;
}
//********* Delete from any Location ************************************

void deany_location(node* &head,int l)


{
int count1=0;
node *p;
p=head;
while(p!=NULL)
{count1++;
p=p->next;
}
if(l==0)
{
cout<<"the debeggiining\n";
debeggining(head);
}
else
if(l==count1+1)
{
cout<<"the deend\n";
deend(head);
}
else
if(l<count1)
{
cout<<"the demiddle\n";
demiddle(head,l);
}
else
if(l>count1)
cout<<"the is not found node error2\n";
}
//&&&&&&&&&&&& Deletion &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void deletion(node* &head)
{int k,l,ch;
while(8)
{cout<<"1:debeggining\n";
cout<<"2:deend\n";
cout<<"3:demiddle\n";
cout<<"4:deany location\n";
cout<<"5:exit\n";
cout<<"choice th number\n";
cin>>ch;
switch(ch)
{case 1:cout<<" delete in beggining\n";
debeggining(head);
break;
case 2:cout<<"delete in middle\n";
deend(head);
break;
case 3:cout<<"delete inmddle and enter location\n";
cin>>k;
demiddle(head,k);
break;
case 4: cout<<"delete inany location and enter location\n";
cin>>l;
deany_location(head,l);
break;
case 5:
break;
}break;}
}
//&&&&&&&&&&&&& Main &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
void main()
{
node* head=NULL;
int ch;
while(2)
{
cout<<"1::insertion\n";
cout<<"2::deletion\n";
cout<<"3::print\n";
cout<<"4::exit\n";
cout<<"choes the number\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"the insertion\n";
insertion(head);
break;
case 2:cout<<"the deletion\n";
deletion(head);
break;
case 3:cout<<"the print\n";
print(head);
break;
case 4:
exit(0);
}}
}

You might also like