0

i wrote the following code to delete the nodes at the beginning and at the end of a doubly linked list....but the execution of these functions stopped in between and the program was aborted......

struct nodeb
{
    int value;
    nodeb *next;
    nodeb *pre;   //pre of first node and next of last node point to null...
    nodeb(int a,nodeb *ptr1=0, nodeb *ptr2=0):value(a), next(ptr1), pre(ptr2)
    {}
};

class doublelist
{
private:

nodeb *head1,*head2;

public:

doublelist():head1(0),head2(0)
  {cout<<"double list created"<<endl;}


void deletebeg()//delete beginning node
{
    if(head1->next==0)
    {
        nodeb *ptr=head1;
        head1=head2=0;
        delete ptr;
    }
    else 
    {
        nodeb *ptr=head1->next;
        nodeb *ptr1=head1;
        ptr->pre=0;
        head1=ptr;
        delete ptr1;
    }
}


void deleteend()//delete end node
{
    nodeb *ptr=head1;
    nodeb *ptr1;
    while(ptr->next!=0)
    {
        ptr1=ptr;
        ptr=ptr->next;
    } 

    delete ptr;
    ptr1->next=0;
}
};  //class ends here


int main()
{
    doublelist list1;
nodeb node(8);
nodeb node1(7);
nodeb node2(9);
nodeb node3(4);
list1.insertbeg(node);
list1.insertbeg(node1);
    list1.insertafter(node3,1);
list1.insertend(node2);  //insertbeg,insertafter and insertend are three functions i defined to        attach nodes at the beginning,at a particular location and at  the end of the list 
list1.deletebeg();
} 

can anyone please tell me the problem??this is the link to the three functions for insertions

AvinashK
  • 3,309
  • 8
  • 43
  • 94
  • I don't see `head1` declared anywhere – Sam Dufel Aug 23 '11 at 19:29
  • 7
    Since this is not homework (you didn't specify the `homework` tag), stop wasting your time rewriting linked lists and use `std::list`. – Thomas Matthews Aug 23 '11 at 19:32
  • It's very hard to tell, you've only posted part of your code, and the part you've posted doesn't compile. Post all your code. – john Aug 23 '11 at 19:38
  • Please learn to indent your code correctly. It's in some weird indentation style and a mix of tabs and spaces. – Flame Aug 23 '11 at 19:58
  • First it is not clear what the intent of `deletebeg()` and `deleteend()` are. Are they supposed to mean delete the first/last (and only the first/last) element of the list? Are they supposed to delete the whole list starting from the beginning/end? If you mean only to delete the last element in `deleteend()` why start from the beginning and traverse the list to the end and not start from the end? – Flame Aug 23 '11 at 20:07
  • @Flame: I think its too much to expect logic or clarity from a newbies code. All we do ask is something we can compile and therefore work with. – john Aug 23 '11 at 20:10
  • @Thomas Matthews: perhaps he just wants to learn – BlackBear Aug 23 '11 at 22:15
  • @sam head1 is the pointer to the first element of the linked list – AvinashK Aug 24 '11 at 02:48
  • @flame deletebeg and deleteend only delete the first and last element of the list...and the matter that i started with the head1 to delete the last element is my discretion(though i know its time taking but i wanted to try it out like that) – AvinashK Aug 24 '11 at 02:52
  • @avinash Then your implementation of `deleteend()` is still wrong. When you delete the end, you need to update the end pointer (head2?) to point to the /new/ end of the list. – Flame Aug 24 '11 at 16:05

4 Answers4

2

Now I can see all the code the problem is very simple. Your deletebeg function is deleting the beginning node with delete, but you didn't allocate the node with new. You should only delete memory if you created it using new.

Normally when people write linked list classes they allocate the nodes inside the list methods using new. Then they can safely delete the nodes inside the methods. You are doing the deletes but you are not using new. So you need to rewrite your main function like this

int main()
{
    doublelist list1;
    list1.insertbeg(8); // add 8 to beginning of list
    list1.insertbeg(7); // add 7 to beginning of list
    list1.insertafter(4,1); // add 4 after first item of list
    list1.insertend(9); // add 9 to end of list
    list1.deletebeg();
} 

Then you need to rewrite your methods like this

void insertbeg(int value)//insert beginning
{
    nodeb* a = new nodeb(value); // allocate node inside of method using new
    if(head1==0)
    {
        head1=a;
        head2=a;
        a->next=0;
        a->pre=0;
    }
    else
    {
        nodeb *ptr=head1;
        ptr->pre=a;
        a->pre=0;
        a->next=ptr;
        head1=a;
    }
}

I've only shown insertbeg, you need to change all your insert methods in the same way.

I'm not promising that's the only problem, but make this change and you'll be on the right way. If you have more problems then post again, but remember post complete code. It's the only way you'll get help with problems like this.

john
  • 85,011
  • 4
  • 57
  • 81
  • @john...i did try out this method before...but the problem is that nodeb objects that you define inside the function will get deleted as soon as the function finishes execution...and then head1 and head2 will point nowhere...thats why i passed the nodeb object created in main to function by reference...secondly if delete can deallocate only those memories which have been allocated by new then how can we deliberatily delete an object not created by new – AvinashK Aug 24 '11 at 11:27
  • @awinash - That is wrong, objects you allocate with new get deleted only when you use delete. That code I posted is correct, why don't you try it? When you said you tried it before I bet you did not use new in your methods and instead allocated the nodes on the stack, then what you said would be true, but allocating with new is different. Secondly you cannot delete an object not create by new, and you don't need to. It's clear you don't understand object allocation. Perhaps you need to do some reading? – john Aug 24 '11 at 11:53
  • @Awinash - I've added another answer, which I think will explain the misunderstanding you have. – john Aug 24 '11 at 12:06
0

I'm a little baffled by this code excerpt, but I'll assume this is the entire excerpt for this...

The functions deletebeg and deleteend aren't declared anywhere in the class definition, only after it. Normally, it would look something like:

class List{
    void aFunction(List * argument);
};  

void List::aFunction(List * argument){
    do something
};

But all that aside, do not make your own linked list, it is much faster and will make your life easier using std::list<int> (replacing int with whatever data type you are making a list for).

There are lots of reasons for this, but the main one is that you only don't have to write it, but you don't have to debug it either. The linked list implementation you created, for instance, uses a recursive function to delete itself (when a function calls itself, it's recursive). If the linked list is very large, this can cause a stack overflow, caused by calling too many functions within functions. It's things like this that are a nightmare to track down and find, and distract you from the real reason you are programming. That is assuming that reason isn't making a linked list. :P

Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
0

While it isn't usual to see functions declared outside a class in C++, it isn't impossible. But that would mean that head1 is a global variable somewhere that isn't shown.

You left out the part of that is actually calling deletebeg and deleteend so it is hard to tell exactly what is happening. Perhaps you are making use of a pointer after it has been deleted.

Also, while NULL is usually zero, there is no guarantee that is the case for your compiler. You should be using NULL instead of zero.

My guess is that you called deleteend with a node that for whatever reason had head1==0, then ptr1 was never initialized, and the program crashed on the last line of deleteend when you tried to dereference the uninitialized pointer.

Poodlehat
  • 360
  • 1
  • 2
  • 9
  • Actually the standard does guarantee that a pointer can be assigned or compared to 0, there's no need to insist on NULL. – Mark Ransom Aug 23 '11 at 22:31
0

Further to the comments on my previous answer

This code is wrong

void insertbeg(int value)//insert beginning
{
    nodeb a(value); // create node on the stack
    if(head1==0)
    {
        head1=&a;
        head2=&a;
        a.next=0;
        a.pre=0;
    }
    else
    {
        nodeb *ptr=head1;
        ptr->pre=&a;
        a.pre=0;
        a.next=ptr;
        head1=&a;
    }
}

The code above would have the problem you described, when you said 'head1 and head2 will point nowhere'. But this code is completely different

void insertbeg(int value)//insert beginning
{
    nodeb* a = new nodeb(value); // allocate node inside of method using new
    if(head1==0)
    {
        head1=a;
        head2=a;
        a->next=0;
        a->pre=0;
    }
    else
    {
        nodeb *ptr=head1;
        ptr->pre=a;
        a->pre=0;
        a->next=ptr;
        head1=a;
    }
}

It's different because it uses new to create the objects. When you use new the objects don't get destroyed when you exit the function. That's what new means. But when you do use new you also have to use delete when you have finished with the nodes.

john
  • 85,011
  • 4
  • 57
  • 81
  • @john...i tried the code successfully...there is one more problem [with this code i wrote](http://textuploader.com/?p=6&id=mKR7d)...first in the search operation it returned true if number was in the list but it returned true even if it was not there...secondly in the main function there were errors....for 'b' it said jump to case label...for 'a' it said crosses initialisation of singlelist *list1...for 'c' conflicting types of doublelist *list1..for 'a' again 'previous declaration as singlelist *list1'... – AvinashK Aug 25 '11 at 04:20
  • @avinash: Your search function is correct, the problem is somewhere else. As I said before if you want problems like this to be fixed you must post all the code because the problem is usually somewhere different from where you think it is. – john Aug 25 '11 at 06:15
  • @avinash: These a just the rules of C++, you cannot jump over a declaration, whether you are doing that with goto or whether you are doing it with a switch label, see [here](http://stackoverflow.com/questions/2392655/what-are-the-sins-of-crosses-initialization) for more details. – john Aug 25 '11 at 06:17
  • @john...[uploaded the entire search code's class](http://textuploader.com/?p=6&id=d7e8q) – AvinashK Aug 25 '11 at 07:19
  • @avinash, please upload **the entire program**. How many times do I have to say this? – john Aug 25 '11 at 09:38
  • sorry i didnt respond...but i found the error...it was in the search function itself...the condition i gave earlier was in the while loop was while(ptr->value!=0 && ptr!=0)..what this did was that when it incremented ptr till 0(if number is not there) and then stopped when ptr==0 but in the loop before terminating it checked if ptr->value!=0..now as ptr=0, ptr->value is not defined...so what we have to do is that just reverse the conditions..when first condition is not met it does not go to the second condition...[the corrected function is here](http://textuploader.com/?p=6&id=IsMOu) – AvinashK Sep 03 '11 at 19:57