Question

i want to complete this code to insert a new node in the middle of list...

i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code

#include <iostream>

#include <stdlib.h>

using namespace std ;

struct Node{

               int data;

               Node *link ;};

struct Node *head=NULL, *tail=NULL; /* pointers to Node*/

void InsertFront();

void InsertRear();

void DeleteFront();

void DeleteRear();

int main(){

               int choice;

               do{

                              cout << "1: Insert item at front\n";

                              cout << "2: Insert item at rear\n";

                              cout << "3: Delete item from front\n";

                              cout << "4: Delete item from rear\n";

                              cout << "6: Exit\n";

                              cout << "Enter your choice: ";

                              cin>>choice ;

                              switch (choice){

                              case 1:

                                                InsertFront( ) ;

                                                break ;

                              case 2:

                                                InsertRear( ) ;

                                                break ;

                              case 3:

                                                DeleteFront( ) ;

                                                break ;

                              case 4:

                                                DeleteRear( ) ;

                                                break ;

                              case 5:

                                                cout<<"Exiting Program\n" ;

                                                break ;

                              default:

                                             cout<<"Error! wrong choice \n" ;

                  } }while (choice != 6);

               return 0;}

void InsertFront(){

               Node *temp = new Node;// Create new Employee

               cout<< "Enter Integer Data: " ;

               cin>>temp->data ;

               temp->link = NULL ;

               if (head==NULL){

                              head=temp;

                              tail=temp;

               }else{temp->link=head;

head=temp;

               }}

void InsertRear( ){

               Node *temp = new Node;// Create new Employee

               cout<< "Enter Integer Data: " ;

               cin>> temp->data ;

               temp->link = NULL ;       

               if (head==NULL) {

                              head=temp;

                              tail=temp;

               }else{

                              tail->link=temp;

                              tail=temp;}}

void DeleteFront() {

               Node *temp = head ;

               if(head==NULL) cout<<"List is empty\n" ;

               else{if(head == tail){head = NULL ;tail=NULL ;}

                              else{head = head->link ;}

                              cout<< temp->data << " has been deleted\n" ;

                              delete temp; }

    cout << endl;}

void DeleteRear() {

               Node *temp = tail;

               if(head==NULL) cout<< "List is empty\n" ;

               else

               {Node *cur= head ;

                              if(head == tail)

                              {head=NULL;     

                                             tail = NULL;}

                              else {while(cur->link!=tail)

                                                            cur = cur->link ;

                                             cur->link = NULL ;

                                             tail = cur ;}

                                             cout<< temp->data << " has been deleted\n";

                                             delete temp ; }

cout << endl;}

Homework Answers

Answer #1

#include <iostream>

#include <stdlib.h>

using namespace std ;

struct Node{

int data;

Node *link ;};

struct Node *head=NULL, *tail=NULL; /* pointers to Node*/

void InsertFront();

void InsertRear();

void DeleteFront();

void DeleteRear();

void InsertNth(Node*);

void printList(Node*);

int main(){

int choice;

do{

cout << "1: Insert item at front\n";

cout << "2: Insert item at rear\n";

cout << "3: Delete item from front\n";

cout << "4: Delete item from rear\n";
  
cout << "5: Insert item in middle\n";
  
cout << "6: Print the linked list\n";

cout << "7: Exit\n";

cout << "Enter your choice: ";

cin>>choice ;

switch (choice){

case 1:

InsertFront( ) ;

break ;

case 2:

InsertRear( ) ;

break ;

case 3:

DeleteFront( ) ;

break ;

case 4:

DeleteRear( ) ;

break ;

case 5:
InsertNth(head); // Call the InsertNth function.

break ;
  
case 6:
printList(head); // Call the printList function.
  
break;

default:

cout<<"Error! wrong choice \n" ;

} }while (choice != 7);

return 0;}

void InsertFront(){

Node *temp = new Node;// Create new Employee

cout<< "Enter Integer Data: " ;

cin>>temp->data ;

temp->link = NULL ;

if (head==NULL){

head=temp;

tail=temp;

}else{temp->link=head;

head=temp;

}}

void InsertRear( ){

Node *temp = new Node;// Create new Employee

cout<< "Enter Integer Data: " ;

cin>> temp->data ;

temp->link = NULL ;   

if (head==NULL) {

head=temp;

tail=temp;

}else{

tail->link=temp;

tail=temp;}}

void DeleteFront() {

Node *temp = head ;

if(head==NULL) cout<<"List is empty\n" ;

else{if(head == tail){head = NULL ;tail=NULL ;}

else{head = head->link ;}

cout<< temp->data << " has been deleted\n" ;

delete temp; }

cout << endl;}

void DeleteRear() {

Node *temp = tail;

if(head==NULL) cout<< "List is empty\n" ;

else

{Node *cur= head ;

if(head == tail)

{head=NULL;   

tail = NULL;}

else {while(cur->link!=tail)

cur = cur->link ;

cur->link = NULL ;

tail = cur ;}

cout<< temp->data << " has been deleted\n";

delete temp ; }

cout << endl;}

// Insert the node at particular position

void InsertNth(Node *head)
{
int position;
Node *add = new Node;
  
cout<< "Enter Integer Data: " ;

cin>>add->data ;
  
add->link = NULL;
  
cout << "Enter the position: ";
  
cin>>position;
  
if(head == NULL)
head = add;
else if(position == 1)
{
add->link = head;
head = add;
}
else
{
Node *cur = head;
int d = 2;
while(cur != NULL)
{
if(d == position)
{
add->link = cur->link;
cur->link = add;
break;
}
cur = cur->link;
d++;
}
}
}

// For print the linked list value.

void printList(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->link;
}
cout << endl;
}

Output :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...