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);...
How do i stop the loop if no next value in the list and prevent it...
How do i stop the loop if no next value in the list and prevent it from exit the program instead back to main menu. Let say i add food detail then i display it. It will display the detail but it will exit after it. struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; }; void Foodsystem::Place_Order(){ temp=new food; cout<<endl; cin.ignore(); cout<<"Enter your Name"; getline(cin,temp->name); cout<<"enter contact number::"; cin>>temp->contact_number; cin.ignore(); cout<<"enter address::"; getline(cin,temp->address); cout<<"customer_id::"; getline(cin,temp->customer_id);...
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...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
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.”....