//nexted list to insert middle
#include <iostream>
using namespace std;
#include<stdio.h>
struct node
{
int data;
node* next;
};
struct node* first=NULL;
struct node* prev=NULL;
struct node* ptr=NULL;
struct node* last=NULL;
void insert_middle();
void display();
void deleteKey();
int main()
{
display();
deleteKey();
display();
insert_middle();
display();
insert_middle();
display();
insert_middle();
display();
//insert_middle();
//display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
return 0;
}
void insert_middle()
{
cout<<"insert_middle"<<endl;
node *newnode = new node;
cout<<"Enter the data to insert:"<<endl;
cin>>newnode->data;
newnode->next=NULL;
int pos;
cout<<"Enter the position to enter:"<<endl;
cin>>pos;
int length=0;
node* ptr=first;
while(ptr)
{
ptr=ptr->next;
length++;
}
//cout<<"length="<<length<<endl;
if(pos==1)//111111111
{
if(first==NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else
{
newnode->next=first;
first=newnode;
}
}
else if(pos >1 && pos <=length)////////////2-length
{
ptr=first;
for(int i =1; i<pos;i++)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=newnode;
newnode->next=ptr;
}
else//out side of length
{
cout<<"Out of range....Inserting at last"<<endl;
if(first==NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else
{
ptr=first;
while(ptr->next!=NULL)//ptr-next is must as segflt
{
ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=NULL;
}
}
}
void display()
{
cout<<"display"<<endl;
node* current=first;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}
void deleteKey()
{
cout<<"delteKey"<<endl;
int key;
if(first == NULL)
{
printf("\nEmpty Linked List. Deletion not possible.\n");
}
else
{
printf("\nEnter the data of the node to be deleted: ");
scanf("%d", &key);
ptr = first;
while((ptr->next != NULL) && (ptr->data != key))
{
cout<<"while"<<endl;
prev = ptr;
ptr = ptr->next;
}
if(ptr->data == key)
{
if(ptr==first)
{
cout<<"if"<<endl;
node * temp=first;
first=first->next;
delete temp;
}
else
{
cout<<"else"<<endl;
prev->next = ptr->next;
delete ptr;
}
printf("\nNode with data %d deleted.\n", key);
}
else
{
printf("\nValue %d not found. Deletion not possible.\n", key);
}
}
}
/*output
display
delteKey
Empty Linked List. Deletion not possible.
display
insert_middle
Enter the data to insert:
11
Enter the position to enter:
1
display
11
insert_middle
Enter the data to insert:
22
Enter the position to enter:
1
display
22 11
insert_middle
Enter the data to insert:
33
Enter the position to enter:
2
display
22 33 11
delteKey
Enter the data of the node to be deleted: 11
while
while
else
Node with data 11 deleted.
display
22 33
delteKey
Enter the data of the node to be deleted: 22
if
Node with data 22 deleted.
display
33
delteKey
Enter the data of the node to be deleted: 22
Value 22 not found. Deletion not possible.
display
33
delteKey
Enter the data of the node to be deleted: 33
if
Node with data 33 deleted.
display
*/
#include <iostream>
using namespace std;
#include<stdio.h>
struct node
{
int data;
node* next;
};
struct node* first=NULL;
struct node* prev=NULL;
struct node* ptr=NULL;
struct node* last=NULL;
void insert_middle();
void display();
void deleteKey();
int main()
{
display();
deleteKey();
display();
insert_middle();
display();
insert_middle();
display();
insert_middle();
display();
//insert_middle();
//display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
return 0;
}
void insert_middle()
{
cout<<"insert_middle"<<endl;
node *newnode = new node;
cout<<"Enter the data to insert:"<<endl;
cin>>newnode->data;
newnode->next=NULL;
int pos;
cout<<"Enter the position to enter:"<<endl;
cin>>pos;
int length=0;
node* ptr=first;
while(ptr)
{
ptr=ptr->next;
length++;
}
//cout<<"length="<<length<<endl;
if(pos==1)//111111111
{
if(first==NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else
{
newnode->next=first;
first=newnode;
}
}
else if(pos >1 && pos <=length)////////////2-length
{
ptr=first;
for(int i =1; i<pos;i++)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=newnode;
newnode->next=ptr;
}
else//out side of length
{
cout<<"Out of range....Inserting at last"<<endl;
if(first==NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else
{
ptr=first;
while(ptr->next!=NULL)//ptr-next is must as segflt
{
ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=NULL;
}
}
}
void display()
{
cout<<"display"<<endl;
node* current=first;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}
void deleteKey()
{
cout<<"delteKey"<<endl;
int key;
if(first == NULL)
{
printf("\nEmpty Linked List. Deletion not possible.\n");
}
else
{
printf("\nEnter the data of the node to be deleted: ");
scanf("%d", &key);
ptr = first;
while((ptr->next != NULL) && (ptr->data != key))
{
cout<<"while"<<endl;
prev = ptr;
ptr = ptr->next;
}
if(ptr->data == key)
{
if(ptr==first)
{
cout<<"if"<<endl;
node * temp=first;
first=first->next;
delete temp;
}
else
{
cout<<"else"<<endl;
prev->next = ptr->next;
delete ptr;
}
printf("\nNode with data %d deleted.\n", key);
}
else
{
printf("\nValue %d not found. Deletion not possible.\n", key);
}
}
}
/*output
display
delteKey
Empty Linked List. Deletion not possible.
display
insert_middle
Enter the data to insert:
11
Enter the position to enter:
1
display
11
insert_middle
Enter the data to insert:
22
Enter the position to enter:
1
display
22 11
insert_middle
Enter the data to insert:
33
Enter the position to enter:
2
display
22 33 11
delteKey
Enter the data of the node to be deleted: 11
while
while
else
Node with data 11 deleted.
display
22 33
delteKey
Enter the data of the node to be deleted: 22
if
Node with data 22 deleted.
display
33
delteKey
Enter the data of the node to be deleted: 22
Value 22 not found. Deletion not possible.
display
33
delteKey
Enter the data of the node to be deleted: 33
if
Node with data 33 deleted.
display
*/
No comments:
Post a Comment