SLL 1
SLL 1
struct Node {
int data;
Node* next;
};
newNode->data = value;
if (head == NULL) {
newNode->next = NULL;
head = newNode;
} else {
newNode->next = head;
head = newNode;
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
temp = temp->next;
if (temp == NULL) {
cout << "Given node is not found in the list! Insertion not possible!" << endl;
delete newNode;
return;
newNode->next = temp->next;
temp->next = newNode;
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
temp = temp->next;
temp->next = newNode;
void deleteFromBeginning() {
if (head == NULL) {
return;
if (temp->next == NULL) {
head = NULL;
} else {
head = temp->next;
delete temp;
if (head == NULL) {
temp2 = temp1;
temp1 = temp1->next;
if (temp1 == NULL) {
cout << "Given node not found in the list! Deletion not possible." << endl;
return;
head = NULL;
delete temp1;
return;
if (temp1 == head) {
head = head->next;
delete temp1;
return;
if (temp1->next == NULL) {
temp2->next = NULL;
delete temp1;
return;
temp2->next = temp1->next;
delete temp1;
if (head == NULL) {
return;
if (temp1->next == NULL) {
delete temp1;
head = NULL;
return;
temp2 = temp1;
temp1 = temp1->next;
temp2->next = NULL;
void display() {
if (head == NULL) {
return;
temp = temp->next;
// Main function
int main() {
int choice,location,value,key;
char ch;
do
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter the value to be inserted:";
cin>>value;
insertAtBeginning(value);
break;
cin>>location;
cin>>value;
insertAfter(head,location,value);
break;
cin>>value;
insertAtEnd(head,value);
break;
case 4: deleteFromBeginning();
break;
cin>>key;
deleteNode(head,key);
break;
case 6: deleteFromEnd(head);
break;
case 7: display();
break;
default:cout<<"wrong choice";
cout<<"(Press y/n)";
cin>>ch;
}while(ch=='y');
return 0;