Anonymous November 28, 2022 at 4:44 am void printList(struct Node* node){ while (node != NULL) { cout << node->item << " "; node = node->next; } cout << endl << endl;} Reply
Anonymous November 28, 2022 at 4:44 am #include using namespace std; struct Node{ int item; struct Node* next;}; struct Node* head = NULL; void insertAtBeginning(int data){ struct Node* new_node = new Node(); new_node->item = data; new_node->next = head; head = new_node;} void insertAtEnd(int data){ struct Node* new_node = new Node(); struct Node* last = head; new_node->item = data; new_node->next = NULL; if (head == NULL) { head = new_node; } else { while (last->next != NULL) { last = last->next; } last->next = new_node; }} void insertAfter(struct Node* prev_node, int data){ if (prev_node == NULL) { cout << "nThe given previous node cannot be NULL.."; } else { struct Node* new_node = new Node(); new_node->item = data; new_node->next = prev_node->next; prev_node->next = new_node; }} void printList(struct Node* node){ while (node != NULL) { cout << node->item << " "; node = node->next; } cout << endl << endl;} bool insertAfterValue(int value, int data){ struct Node* p = new Node(); p=head; while(p !=NULL) { if(p->item==value){ struct Node* new_node = new Node(); new_node->item = data; new_node->next = p->next; p->next = new_node; break; } else { p=p->next; } return false; }} bool search(struct Node *p,int x){ while(p !=NULL) { if(p->item==x) return true; p=p->next; }} int main(){ insertAtBeginning(5); insertAtBeginning(4); insertAtBeginning(3); insertAtBeginning(2); insertAtBeginning(1); cout << "Linked list: "; printList(head); insertAtEnd(10); insertAtEnd(11); insertAtEnd(12); insertAtEnd(13); insertAtEnd(14); cout << "Linked list: "; printList(head); insertAfter(head->next->next->next->next, 55); cout<<"inserting after value:"<<insertAfterValue(13,20)<<endl; cout << "Linked list after inserting 55: "; printList(head); cout<<"Searching:"<<search(head,13); return 0;} Reply
void printList(struct Node* node)
{
while (node != NULL)
{
cout << node->item << " ";
node = node->next;
}
cout << endl << endl;
}
#include
using namespace std;
struct Node
{
int item;
struct Node* next;
};
struct Node* head = NULL;
void insertAtBeginning(int data)
{
struct Node* new_node = new Node();
new_node->item = data;
new_node->next = head;
head = new_node;
}
void insertAtEnd(int data)
{
struct Node* new_node = new Node();
struct Node* last = head;
new_node->item = data;
new_node->next = NULL;
if (head == NULL)
{
head = new_node;
}
else
{
while (last->next != NULL)
{
last = last->next;
}
last->next = new_node;
}
}
void insertAfter(struct Node* prev_node, int data)
{
if (prev_node == NULL)
{
cout << "nThe given previous node cannot be NULL..";
}
else
{
struct Node* new_node = new Node();
new_node->item = data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
}
void printList(struct Node* node)
{
while (node != NULL)
{
cout << node->item << " ";
node = node->next;
}
cout << endl << endl;
}
bool insertAfterValue(int value, int data)
{
struct Node* p = new Node();
p=head;
while(p !=NULL)
{
if(p->item==value){
struct Node* new_node = new Node();
new_node->item = data;
new_node->next = p->next;
p->next = new_node;
break;
}
else
{
p=p->next;
}
return false;
}
}
bool search(struct Node *p,int x)
{
while(p !=NULL)
{
if(p->item==x) return true;
p=p->next;
}
}
int main()
{
insertAtBeginning(5);
insertAtBeginning(4);
insertAtBeginning(3);
insertAtBeginning(2);
insertAtBeginning(1);
cout << "Linked list: ";
printList(head);
insertAtEnd(10);
insertAtEnd(11);
insertAtEnd(12);
insertAtEnd(13);
insertAtEnd(14);
cout << "Linked list: ";
printList(head);
insertAfter(head->next->next->next->next, 55);
cout<<"inserting after value:"<<insertAfterValue(13,20)<<endl;
cout << "Linked list after inserting 55: ";
printList(head);
cout<<"Searching:"<<search(head,13);
return 0;
}
https://notepad.ltd/