Thread: Delete All Items In A Linked List

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Delete All Items In A Linked List

    I can't seem to figure out how to do this. The code I have in there (RemoveAll function) keeps leading to many program crashes. Please help.

    Friends.h
    Code:
    class linkedlist {
    
    public:
    
    	char name[100];
    	int birthday;
    	int phone;
    	linkedlist* nextstudent;
    
    linkedlist ();
    void Insert();
    void RemoveAll();
    //void Reset();
    //int* GetNext();
    ~linkedlist ();
    
    };
    Friends.cpp
    Code:
    #include <iostream>
    #include "friends.h"
    using namespace std;
    
    int choice = 0;
    int counter = 0;
    
    linkedlist* student;
    linkedlist* headstudent;
    linkedlist* prevstudent = NULL;
    
    linkedlist functions;
    
    linkedlist::linkedlist () {
    
    strncpy_s(name, "No Input", 9);
    birthday = 0;
    phone = 0; }
    
    void linkedlist::Insert ()
    {
    	student = new linkedlist;
    	if (prevstudent == NULL){
    		headstudent = student; }
    	else {
    		prevstudent->nextstudent = student; }
    
    	cout<<"\nEnter the friend's name: ";
    	cin>>student->name;
    	cout<<"Enter the friend's birthday: (MMDD) ";
    	cin>>student->birthday;
    	cout<<"Enter the friend's phone number: (No Dashes) ";
    	cin>>student->phone;
    
    	cout<<"\n\n";
    
    	prevstudent = student;
    	prevstudent->nextstudent = NULL;
    	student = headstudent;
    
    }
    
    void linkedlist::RemoveAll () {
    
    	student = headstudent;
    
    	do {
    		prevstudent = student;
    		student = student->nextstudent;
        	delete prevstudent;
    	} while (student != NULL);
    
    }
    
    linkedlist::~linkedlist () {
    
    	RemoveAll(); }
    
    int Print()
    {
    	cout<<"\n\n";
    
    	if (counter == 0) {
    	cout<<functions.name<<"\nBirthday: "<<functions.birthday<<"\nPhone #: "<<functions.phone<<"\n\n"; }
    
    	else {
    
    	do {
    
    		cout<<student->name<<"\nBirthday: "<<student->birthday<<"\nPhone: "<<student->phone<<"\n\n";
    		student = student ->nextstudent;;
    	} while (student != NULL);
    
    	student = headstudent; }
    
    
    
    //	for (int n = 0;n < total;n++) {  
    //		cout<<"Name: "<<student->name<<"\nBirthday: "<<student->birthday<<"\nPhone #: "<<student->phone<<"\n\n"; }
    
    	return 0;
    }
    int main ()
    {
    
    	while (choice != 4) {
    
    	cout<<"Please select an option...\n\n1. Enter Data\n2. Print Data\n3. Erase All Data\n4. Quit\n\n";
    	cin>>choice;
    
    	if (choice == 1){
    		functions.Insert(); }
    	if (choice == 2) {
    		Print(); }
    	if (choice == 3) {
    		functions.RemoveAll(); }
    
    	}
    
    
    	cout<<"\n";
    	system("PAUSE");
    	return 0;
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    node of the list and whole list are 2 different object - so it should be 2 different classes...

    your concept is more C-like than C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Why would you want two different classes?

    It works fine with just one, it just crashes when I try to delete all the data stored in the list. The only problem is that something is wrong with my delete code.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That might actually be the very reason.

    Do you realize that deleting a linked list node invokes the destructor of "linked-list" which in turn tries to removeAll, leading to double deletion or something like that?

    You don't have a linked list. All you have are nodes that are all tied together through a horrible use of globals. A node is not a linked list. Therefore a node cannot destroy what it is part of. Only a real linked list can release all its nodes in the destructor.

    Another problem is that you can't use do loops if the linked list can be empty to start with. Also, what's with the default empty linked list and a global counter that is nowhere changed and prevents from displaying the entered data.
    Last edited by anon; 02-08-2009 at 04:45 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    I had no idea deleting a linked list node invoked the destructor...
    I fixed that and it works better now, but crashes the second time you try to remove all data.

    Default empty linked list?

    The counter is supposed to change to 1 when the Insert function runs, forgot to put that in there. This is so it prints out the default values instead of crashing like it did when you tried to have it print the data before you have entered anything. My code is a mess...

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We can't help furthur with the problem unless we can see what your edited code looks like.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the list is empty, the head pointer should be NULL. You can test that and display a message (e.g "list is empty"). You don't need a overcomplicated system using even more global variables.

    The crash is because you use a do loop, assuming that there is at least one node, not a while loop which can handle empty lists.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM