Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help to put the link list into ascending order #include<iostream> using nam

ID: 3593373 • Letter: N

Question

Need help to put the link list into ascending order

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
private:
node *head, *tail;
public:
list()
{
  head = NULL;
  tail = NULL;
}
void createnode(int value)
{
  node *temp = new node;
  temp->data = value;
  temp->next = NULL;
  if (head == NULL)
  {
   head = temp;
   tail = temp;
   temp = NULL;
  }
  else
  {
   tail->next = temp;
   tail = temp;
  }
}
void display()
{
  node *temp = new node;
  temp = head;
  while (temp != NULL)
  {
   cout << temp->data << " ";
   temp = temp->next;
  }
}
void insert_start(int value)
{
  node *temp = new node;
  temp->data = value;
  temp->next = head;
  head = temp;
}
void insert_position(int pos, int value)
{
  node *pre = new node;
  node *cur = new node;
  node *temp = new node;
  cur = head;
  for (int i = 1; i<pos; i++)
  {
   pre = cur;
   cur = cur->next;
  }
  temp->data = value;
  pre->next = temp;
  temp->next = cur;
}
void delete_first()
{
  node *temp = new node;
  temp = head;
  head = head->next;
  delete temp;
}
void delete_last()
{
  node *current = new node;
  node *previous = new node;
  current = head;
  while (current->next != NULL)
  {
   previous = current;
   current = current->next;
  }
  tail = previous;
  previous->next = NULL;
  delete current;
}
void delete_position(int pos)
{
  node *current = new node;
  node *previous = new node;
  current = head;
  for (int i = 1; i<pos; i++)
  {
   previous = current;
   current = current->next;
  }
  previous->next = current->next;
}
};
int main()
{
list obj;
obj.createnode(25);
obj.createnode(50);
obj.createnode(90);
obj.createnode(40);
cout << " -------------------------------------------------- ";
cout << "---------------Displaying All nodes---------------";
cout << " -------------------------------------------------- ";
obj.display();
cout << " -------------------------------------------------- ";
cout << "-----------------Inserting At End-----------------";
cout << " -------------------------------------------------- ";
obj.createnode(55);
obj.display();
cout << " -------------------------------------------------- ";
cout << "----------------Inserting At Start----------------";
cout << " -------------------------------------------------- ";
obj.insert_start(50);
obj.display();
cout << " -------------------------------------------------- ";
cout << "-------------Inserting At Particular--------------";
cout << " -------------------------------------------------- ";
obj.insert_position(5, 60);
obj.display();
cout << " -------------------------------------------------- ";
cout << "----------------Deleting At Start-----------------";
cout << " -------------------------------------------------- ";
obj.delete_first();
obj.display();
cout << " -------------------------------------------------- ";
cout << "-----------------Deleing At End-------------------";
cout << " -------------------------------------------------- ";
obj.delete_last();
obj.display();
cout << " -------------------------------------------------- ";
cout << "--------------Deleting At Particular--------------";
cout << " -------------------------------------------------- ";
obj.delete_position(4);
obj.display();
cout << " -------------------------------------------------- ";
system("pause");
return 0;
}

Explanation / Answer

PLEASE REFER BELOW CPP CODE

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
private:
node *head, *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void createnode(int value)
{
node *temp = new node;
temp->data = value;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display()
{
node *temp = new node;
temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
void insert_start(int value)
{
node *temp = new node;
temp->data = value;
temp->next = head;
head = temp;
}
void insert_position(int pos, int value)
{
node *pre = new node;
node *cur = new node;
node *temp = new node;
cur = head;
for (int i = 1; i<pos; i++)
{
pre = cur;
cur = cur->next;
}
temp->data = value;
pre->next = temp;
temp->next = cur;
}
void delete_first()
{
node *temp = new node;
temp = head;
head = head->next;
delete temp;
}
void delete_last()
{
node *current = new node;
node *previous = new node;
current = head;
while (current->next != NULL)
{
previous = current;
current = current->next;
}
tail = previous;
previous->next = NULL;
delete current;
}
void delete_position(int pos)
{
node *current = new node;
node *previous = new node;
current = head;
for (int i = 1; i<pos; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
}
/* function to insert a new_node in a list. Note that this
function expects a pointer to head as this can modify the
head of the input linked list (similar to push())*/
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void sort_list()
{
// Initialize sorted linked list
struct node *sorted = NULL;

// Traverse the given linked list and insert every
// node to sorted
struct node *current = head;
while (current != NULL)
{
// Store next for next iteration
struct node *next = current->next;

// insert current in sorted linked list
sortedInsert(&sorted, current);

// Update current
current = next;
}

// Update head_ref to point to sorted linked list
head = sorted;
}
};
int main()
{
list obj;
obj.createnode(25);
obj.createnode(50);
obj.createnode(90);
obj.createnode(40);
cout << " -------------------------------------------------- ";
cout << "---------------Displaying All nodes---------------";
cout << " -------------------------------------------------- ";
obj.display();
cout << " -------------------------------------------------- ";
cout << "-----------------Inserting At End-----------------";
cout << " -------------------------------------------------- ";
obj.createnode(55);
obj.display();
cout << " -------------------------------------------------- ";
cout << "----------------Inserting At Start----------------";
cout << " -------------------------------------------------- ";
obj.insert_start(50);
obj.display();
cout << " -------------------------------------------------- ";
cout << "-------------Inserting At Particular--------------";
cout << " -------------------------------------------------- ";
obj.insert_position(5, 60);
obj.display();
cout << " -------------------------------------------------- ";
cout << "----------------Deleting At Start-----------------";
cout << " -------------------------------------------------- ";
obj.delete_first();
obj.display();
cout << " -------------------------------------------------- ";
cout << "-----------------Deleing At End-------------------";
cout << " -------------------------------------------------- ";
obj.delete_last();
obj.display();
cout << " -------------------------------------------------- ";
cout << "--------------Deleting At Particular--------------";
cout << " -------------------------------------------------- ";
obj.delete_position(4);
obj.display();
cout << " -------------------------------------------------- ";
cout << " -------------------------------------------------- ";
cout << "--------------Sorted Linked List------------------";
cout << " -------------------------------------------------- ";
obj.sort_list();
obj.display();
cout << " -------------------------------------------------- ";
system("pause");
return 0;
}

PLEASE REFER BELOW OUTPUT


--------------------------------------------------
---------------Displaying All nodes---------------
--------------------------------------------------
25 50 90 40
--------------------------------------------------
-----------------Inserting At End-----------------
--------------------------------------------------
25 50 90 40 55
--------------------------------------------------
----------------Inserting At Start----------------
--------------------------------------------------
50 25 50 90 40 55
--------------------------------------------------
-------------Inserting At Particular--------------
--------------------------------------------------
50 25 50 90 60 40 55
--------------------------------------------------
----------------Deleting At Start-----------------
--------------------------------------------------
25 50 90 60 40 55
--------------------------------------------------
-----------------Deleing At End-------------------
--------------------------------------------------
25 50 90 60 40
--------------------------------------------------
--------------Deleting At Particular--------------
--------------------------------------------------
25 50 90 40
--------------------------------------------------

--------------------------------------------------
--------------Sorted Linked List------------------
--------------------------------------------------
25 40 50 90
--------------------------------------------------

Process returned 0 (0x0) execution time : 0.046 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote