Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 25th, 2004, 2:14 PM   #1
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
This program is a ADT i created that is a Circularly Doubly Linked List.
when i run the current program i get a segmentation fault is there anyone who can help look thru my code and see if they know what it is doing wrong that is causing this. here is all my code thanks

main.cpp:
#include <iostream>
#include "SortedList.h"
using namespace std;
using namespace SortedListNameSpace;

void output(SortedList<int> list)
{
  int num;
  if (!list.isEmpty())
  {
   cout << "sorted list\n";
   for (int i=1; i<=list.getLength(); i++)
   {
     list.retrieve(i,num);
     cout << num << endl;
   }
  }
}

int main()
{
  SortedList<int> list;
  int num,index;
  cout << "enter ints, 0 ends list: \n";
  cin >> num;
  while (num!=0)
  {
   try
   {
    cout << "instert" << endl;
      list.insert(num);
    cout << "instert after" << endl;
     }
   catch (ListException error)
   {
     cout << error.what() << endl;
   }
   cin >> num;
  }
  output(list);

  cout << "enter int to be deleted: ";
  cin >> num;
  try
  {
   index = list.getindex(num);
   list.remove(index);
  }
  catch (ListException error)
  {
   cout << error.what() << endl;
  }
  output(list);

  try
  {
   list.retrieve(0,num); // will throw exception
  }
  catch (ListIndexOutOfRangeException error)
  {
   cout << error.what() << endl;
  }

  try
  {
   list.remove(0); // will throw exception
  }
  catch (ListIndexOutOfRangeException error)
  {
   cout << error.what() << endl;
  }

  cout << "the number of SortedList<int> objects: " << SortedList<int>::HowMany() << endl;

  return 0;
}

SortedList.h:
#ifndef _SORTEDLIST_H_
#define _SORTEDLIST_H_

#include <iostream>
#include <string>
#include "ListExceptions.h"

using namespace std;

namespace SortedListNameSpace
{
    template <class ListItemType>
    class SortedList
    {
        public:
            SortedList();
            ~SortedList();
            SortedList(const SortedList&);
            bool isEmpty() const;
            int getLength() const;
            void insert(const ListItemType& newItem) throw(ListException);
            void remove(int index) throw(ListIndexOutOfRangeException);
            void retrieve(int index, ListItemType& dataItem) const throw(ListIndexOutOfRangeException);
            int getindex (const ListItemType& dataItem) const throw (ListException);
            static int HowMany();
        private:
            struct SortedListNode
            {
                SortedListNode *prev;
                ListItemType item;
                SortedListNode *next;
            };
            SortedListNode *head;
            SortedListNode *curPtr;
            int size;
            static int count;

    };
}

#include "SortedList.cpp"

#endif

SortedList.cpp:
#include <iostream>
#include <string>
#include <cstddef>
#include <cassert>
#include "SortedList.h"

using namespace std;
using namespace SortedListNameSpace;

template <class ListItemType>
int SortedList<ListItemType>::count=0;

// constructor
template <class ListItemType>
SortedList<ListItemType>::SortedList() : size(0)
{
    cout << "constructor" << endl;
    SortedListNode *head = new SortedListNode;
    head->prev = head;
    head->next = head;
    curPtr = head;
    count++;
}

// destructor
template <class ListItemType>
SortedList<ListItemType>::~SortedList()
{
    while (!isEmpty())
    {
        remove(1);
    }
}

// copy constructor
template <class ListItemType>
SortedList<ListItemType>::SortedList(const SortedList& sList)//: size(sList.size)
{
    if (sList.head == NULL)
        head = NULL;
    else
    {
        head = new SortedListNode;
        head->item = sList.head->item;

        SortedListNode *newPtr = head;
        for (SortedListNode *origPtr = sList.head->next; origPtr != NULL; origPtr = origPtr->next)
        {
            newPtr->next = new SortedListNode;
            assert(newPtr->next != NULL);
//           assert(newPtr->prev != NULL);
            newPtr = newPtr->next;
            newPtr = newPtr->prev;
            newPtr->item = origPtr->item;
        }

        newPtr->next = NULL;
    }
}

template <class ListItemType>
bool SortedList<ListItemType>::isEmpty() const
{
    return size == 0;
}

template <class ListItemType>
int SortedList<ListItemType>::getLength() const
{
    return size;
}

template <class ListItemType>
void SortedList<ListItemType>::insert(const ListItemType& newItem) throw(ListException)
{
    ++size;
    SortedListNode *newPtr = new SortedListNode;
    ListItemType data;
    cout << "hellodude4" << endl;
    for(int i = 1; data > newItem && i < getLength(); i++)
    {
        cout << "hello\n";
        retrieve(i, data);
    }
    newPtr->next = curPtr;
    newPtr->prev = curPtr->prev;
    curPtr->prev = newPtr;
    newPtr->prev->next = newPtr;
    cout << size << endl;
    cout << "size is; " << size << endl;
}

template <class ListItemType>
void SortedList<ListItemType>::remove(int index) throw(ListIndexOutOfRangeException)
{
    if ((index < 1) || (index > getLength()))
        throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: Retrieve Index is Out of Range.");
    else
    {
        SortedListNode *cur = head;
        for (int i = 1; i < index; ++i)
        {
            cur = cur->next;
        }
        cur->prev->next = cur->next;
        cur->next->prev = cur->prev;
        size--;
    }
}

template <class ListItemType>
void SortedList<ListItemType>::retrieve(int index, ListItemType& dataItem) const throw(ListIndexOutOfRangeException)
{
    cout << "size is:retrieve: " << size << endl;
    cout << "hellodude1" << endl;
    if ((index < 1) || (index > getLength()))
        throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: Retrieve Index is Out of Range.");
    else
    {
        cout << "hellodude2" << endl;
        SortedListNode *cur = head;
        cout << "hellodude3" << endl;
        for (int i = 1; i < index; ++i)
        {
            cout << "hellodude" << endl;
            cur = cur->next;
        }
        cout << "hellodude5" << endl;
        dataItem = cur->item;
        cout << "hellodude6" << endl;
    }
}

template <class ListItemType>
int SortedList<ListItemType>::getindex(const ListItemType& dataItem) const throw(ListException)
{

    SortedListNode *cur = head;
    int i = 1;
    while (dataItem != cur->item)
    {
        cur = cur->next;
        i++;
        if (i < getLength())
            throw ListException("ListException: Data give does not exist in the list.");
    }
    return i;
}

template <class ListItemType>
int SortedList<ListItemType>::HowMany()
{
    return count;
}

ListExceptions.h:
#ifndef _LISTEXCEPTIONS_H_
#define _LISTEXCEPTIONS_H_
#include <stdexcept>
#include <string>
using namespace std;
class ListIndexOutOfRangeException: public out_of_range
{
  public:
   ListIndexOutOfRangeException(const string & message = "")
            : out_of_range(message.c_str())
   { }
}; // end ListIndexOutOfRangeException

class ListException: public logic_error
{
  public:
   ListException(const string & message = "")
            : logic_error(message.c_str())
   { }
}; // end ListException
#endif

thanks for the help.
if i find something i will let you know what i found

edit: i have narrowed it down to being in my copy constructor
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Oct 25th, 2004, 2:52 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
here is the make file for anyone who is trying to compile this with out it
CC=g++
OBJECTS= main.o

prog5:     $(OBJECTS)
            $(CC) -o prog5 $(OBJECTS)

main.o:     main.cpp SortedList.cpp SortedList.h
            $(CC) -c main.cpp

clean:
            rm -rf core.* prog5 $(OBJECTS)
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Oct 25th, 2004, 3:36 PM   #3
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
updated copy constructor
// copy constructor
template <class ListItemType>
SortedList<ListItemType>::SortedList(const SortedList& sList)//: size(sList.size)
{
    cout << "copy constructor beginning" << endl;
    if (sList.head == NULL)
        head = NULL;
    else
    {
        cout << "copy constructor, begin else" << endl;
        head = new SortedListNode;
        cout << "copy constructor, else 2" << endl;
        head->item = sList.head->item;
        cout << "copy constructor, else 3" << endl;
        SortedListNode *newPtr = head;
        cout << "copy constructor, else 4" << endl;
        for (SortedListNode *origPtr = sList.head->next; origPtr != NULL; origPtr = origPtr->next)
        {
            cout << "copy constructor, else 5, for loop" << endl;
            newPtr->next = new SortedListNode;
            cout << "copy constructor, else 6, for loop" << endl;
            assert(newPtr->next != NULL);
            cout << "copy constructor, else 7, for loop" << endl;
//           assert(newPtr->prev != NULL);
            newPtr = newPtr->next;
            cout << "copy constructor, else 8, for loop" << endl;
//           newPtr = newPtr->prev;
            cout << "copy constructor, else 9, for loop" << endl;
            //newPtr->item = origPtr->item;
            cout << "copy constructor, else 10, for loop" << endl;
        }
        cout << "copy constructor, else 11" << endl;
        newPtr->next = NULL;
    }
    cout << "copy constructor ending" << endl;
}
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC