#include <iostream>

using namespace std;

//Linked List: Delete a node at nth position

struct Node {
    int data;
    Node* next;
};

Node* head; //Global

void Insert(int data) { //Insert an integer at the end of list
    Node* temp1 = new Node;
    temp1->data = data;
    temp1->next = head;
    head = temp1;
}

void Print() { //Print all elements in the list
    Node* temp1 = head;
    while (temp1 != NULL) {
        cout << " " << temp1->data;
        temp1 = temp1->next;
    }
    cout << endl;
}

void Delete(int n) { //Delete node at position n
    Node* temp1 = head;
    if (n == 1) {
        head = temp1->next; //head now points to second node
        delete temp1;
        return;
    }
    for (int i = 0; i < n - 2; i++)
        temp1 = temp1->next; //temp1 points to (n - 1)th Node
    Node* temp2 = temp1->next; //nth Node
    temp1->next = temp2->next; // (n + 1)th Node
    delete temp2;

}

Here in the main function I'm trying to enter elements from the keyboard(with while loop) in the linked list. The problem is that when I add elements the program automatically deletes the second element and don't let me to enter n.
Here's what I mean:
http://oi68.tinypic.com/v6nno2.jpg
Notice that I enter letter to stop the loop.

int main() {

    head = NULL; //Empty list
    int num;
    while(cin>>num)
{
Insert(num);
}

    Print();
    int n;
    cout << "Enter a position" << endl;
    cin >> n;
    Delete(n);
    Print();
    return 0;
}

Recommended Answers

All 3 Replies

Member Avatar for RudyM

Next time just attach the screenshot, no need to link us to a site with crappy ads.

The problem is that when I add elements the program automatically deletes the second element

Are you sure about that? My guess is that it's just dumb luck that it deletes the second element. For example, when I ran your program, it did not do that.

and don't let me to enter n.

This is the REAL problem. Who cares what happens after it does not let you enter a value for n. First things first. Solve your input problem.

Let's look at your first loop. The user enters integers one by one. It escapes that loop when you give it a non-integer. It tries to read it into an integer value and gets stuck and gives up. cin is no longer in a good state since the failbit got set when you entered a letter when it expected an integer. To move on, you need to do two things. 1) Reset the failbit 2) Get rid of any excess input in the stdin stream.

Add the following two lines before asking for the position to delete:

cin.clear(); // resets the failbit
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // gets rid of excess characters in the stdin stream

Add #include <limits> at the top of the program.

AssertNull Thank you. I tried with cin.ignore() and cin.clear(). But I not guessed to use std::basic_istream::ignore.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.