Hi,
i'm trying to delete last node of linked list and print the remaining list at each step.
for this i created 'Print' function.
please take a look at this code:

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

void Print(Node *head){
    if(head==0) return;
    else{
        Node *temp=head;
        Node *prev=head;
        while(temp!= NULL){ cout<<temp->data<<" ";temp = temp->next;}
        //cout<<"..."<<endl;
        temp=head;
        while(temp->next!=0){
            prev=temp;
            temp=temp->next;
        }
        delete temp;
        temp=0;
        prev->next=temp;
        Reverse(head);
    }
}
int main() {
    int n;
    cin>>n;
    int num;

    Node *head=0;

    for(int i=0;i<n;i++){
        cin>>num;
        Node *temp=new Node();
        temp->data=num;
        temp->next=head;
        head=temp;

    }

    Print(head);
    return 0;
}

someone please tell me .....why the hell it's not working???....... :(

Might not fix the problem, but the two loops in lines 11-17 can be combined into just one loop. Why iterate through the linked list twice when once will do? And what is Reverse() supposed to do?

Edited 3 Years Ago by Ancient Dragon

damn... actually, Reverse=Print
In my original code its reverse but here i used print.... so.........
And talking about the two loops, code-11 was just to check whether its printing correct or not....

Your head not pointing to first Node in main function you keep changing your head.

Edited 3 Years Ago by ookhan

First of all check the 'for' loop in main()..actually you are making your fisrt mistake there,you are pointing the 'head' after the every itration to the new node created which is wrong..'head' should only point the fisrt node..so do it like this and debug it then

for(int i=0;i<n;i++)
{
        cout<<"Enter your number to insert in the node\n";
        cin>>num;
        if(head==0)    //this condtion gets true only for the first node//
        { 
           Node *temp=new Node;
           temp->data=num;
           temp->next=NULL;
           head=temp;
        }
        else
        {
             Node *temp=head;
             while(temp->next!=NULL)   //this loop brings you to the last node//
             {
                temp=temp->next;
             }
             Node *r=new Node;   //new node creation method//
             r->data=num;
             r->next=NULL;
             temp->next=r;


        }
 }

Edited 3 Years Ago by UFOOOOOOOOOOO

I don't think that is the problem -- he is inserting the new node at the head of the linked list not at the list's tail, and to do that you have to make the new node the head of the list and point the next pointer to the previous head node like he did.

In my original code its reverse but here i used print.... so.......

Why the recursion?

Edited 3 Years Ago by Ancient Dragon

Now 80% of your problem is solved,the remaining you should understand and implement it yourself..thanks

Edited 3 Years Ago by UFOOOOOOOOOOO

first of all he is creating the nodes incorrectly...in others words he is appending the nodes wrong...in this case the code i provided is correctly appending nodes and there is no need of prev because this is not doubly linked list.Now all he has to do is traverse the linked list to the last node through a simple loop and keep printing the elements and when he reaches the last node he should delete temp...problem solved..and if he wants to print the list in reverse order then he should use prev else there is no need of prev

Edited 3 Years Ago by UFOOOOOOOOOOO

first of all he is creating the nodes incorrectly.

Not if he want to add the new nodes at the head of the linked list. Yours is correct if you want to add the new node to the tail of the list. There is a difference.

Now all he has to do is traverse the linked list to the last node through a simple loop and keep printing the elements and when he reaches the last node he should delete temp...problem solved

Yes, I agree, he is deleting the wrong node in the print() function, no need to keep track of the previous node.

i'm trying to delete last node of linked list and print the remaining list at each step.

So, each time you advance the next pointer you have to print the value of all remaining nodes? To do that you will need a second loop inside the main loop

temp = head;
whle(temp->next != 0)
{
    node* temp1 = temp;
    while(temp1->next != 0)
    {
       cout << temp1->data << ' ';
       temp1 = temp1-> next;
    }
    cout << '\n';
    temp = temp->next;
}
// now delete temp

Edited 3 Years Ago by Ancient Dragon

Guys looking at the post carefully, the poster's code is still not answered. He wanted to print a linklist data and then delete the last node. looking at his print function, it looks like he was trying to build a doubly link list not a single list. This is why he started 2 while iterations. If he was working on a linklist, there was not need for a double iterations at all. why double iterations for? whats the point ...?. Like Ancient Drogon asked?

If thats not the case, then i think the poster must come out clearer what he intend to achieve.

okay guys forget about the main function.....
just consider there's a print function which takes one arguement- the head of a linked list.

suppose my list is:
1 2 3 4
then i want that print function to print something like this:
1 2 3 4
1 2 3
1 2
1

basically delete the last node every time.

void Print(Node *head) {
    if (head == NULL)
        return;
    else {
        int i = 0;
        Node *temp = head;
        Node *prev = head;

        while (temp->next != NULL) {
            if(i!=0)
                prev = prev->next;
            i++;
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << temp->data << " ";
        if(head == temp)
            return;
        delete temp;
        prev->next=NULL;
        cout << "\n";
        Print(head);
    }
}
This article has been dead for over six months. Start a new discussion instead.