•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,434 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,942 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2861 | Replies: 1
![]() |
•
•
Join Date: Nov 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I'm fighting a battle with a linked list and I am currently losing. I am using the toolkit, pointers, nodes and a class. My code is still buggy because after building it, it just runs for a while then stops. Please help. Here is what I have, an implementation file of the linked list, everything else was given.
<< moderator edit: added
would appreciate a nudge in the right direction.
I'm fighting a battle with a linked list and I am currently losing. I am using the toolkit, pointers, nodes and a class. My code is still buggy because after building it, it just runs for a while then stops. Please help. Here is what I have, an implementation file of the linked list, everything else was given.
//Provided by Arylee McSweaney
//CLASS implemented: Sequence3(See sequence3.h for documentation)
//INVARIANT for the sequence class:
// 1. The items in the sequence are stored in a linked list.
// 2. The head pointer of the list is stored in the member variable head_ptr.
// 3. The total number of items in the list is stored in the member variable many_nodes.
// 4. The current position of the cursor is stored in the member variable cursor_ptr
// 5. The position of the previous node is stored in the member variable precursor_ptr.
#include <assert> //provides assert
#include <cstdlib> //provides NULL, size_t
#include "node1.h" //Provides node and the linked list functions
#include "sequence3.h"
using namespace std;
namespace main_savitch_5
{
sequence::sequence()
//Library facilities used: cstdlib
{
head_ptr = NULL;
tail_ptr = NULL;
cursor_ptr = NULL;
precursor_ptr = NULL;
many_nodes = 0;
}
sequence::sequence(const sequence& source)
//Library facilitie used: node1.h
{
node *tail_ptr; //Needed for argument of list_copy
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
sequence::~sequence()
//Library facilities used: node1.h
{
list_clear(head_ptr);
many_nodes = 0;
}
//MODIFICATION MEMBER FUNCTIONS
void sequence::start()
{//Postcondition: The first item on the sequence becomes the current item
//(but if the sequence is empty, then there is no current item).
precursor_ptr = NULL;
cursor_ptr=head_ptr;
many_nodes ++;
}
void sequence::advance()
{//Precondition: is_item returns true.
//Postcondition: If the current item was already the last item on the
//sequence, then there is no longer any current item. Otherwise, the new
//current item is the item immediately after the original current item.
assert(is_item());
cursor_ptr = cursor_ptr->link();
++many_nodes;
if(precursor_ptr == NULL)
precursor_ptr = head_ptr;
else
precursor_ptr = precursor_ptr->link();
}
void sequence::insert(const value_type& entry)
{//Postcondition: A new copy of entry has been inserted in the sequence before
//the current item. If there was no current item, then the new entry has
//been inserted at the front of the sequence. In either case, the newly
//inserted item is now the current item of the sequence.
if (is_item( ))
list_insert(precursor_ptr, entry);
else
list_head_insert(head_ptr, entry);
cursor_ptr = precursor_ptr = head_ptr;
cursor_ptr = cursor_ptr->link();
many_nodes++;
}
void sequence::attach(const value_type& entry)
{//Postcondition: A new copy of entry has been inserted in the sequence after
//the current item. If there was no current item, then the new entry has
//been attached to the end of the sequence. In either case, the newly
//inserted item is now the current item of the sequence.
if(is_item())
list_insert(cursor_ptr->link(), entry);
else
list_head_insert(head_ptr, entry);
cursor_ptr = head_ptr;
cursor_ptr=cursor_ptr->link();
++many_nodes;
}
void sequence::remove_current()
{//Precondition: is_item returns true.
//Postcondition: The current item has been removed from the sequence, and the
//item after this (if there is one) is now the new current item.
node *target_ptr;
value_type target;
target = cursor_ptr->data(); //gives target_ptr a value to point to
target_ptr = list_search(head_ptr, target);
//case where item to be removed is not the head pointer or the tail pointer
if(target_ptr != NULL && head_ptr != NULL){
target_ptr->set_data(head_ptr->data());
list_head_remove(head_ptr);}
--many_nodes;
//case where item to be removed is the head pointer
if(target_ptr = head_ptr){
list_head_remove(head_ptr);}
--many_nodes;
//case where item to be removed is the tail pointer
if(target_ptr->link() == NULL);
target_ptr->set_data(head_ptr->data());
list_head_remove(head_ptr);
cursor_ptr = head_ptr;
precursor_ptr->set_link(NULL);
tail_ptr = precursor_ptr;
--many_nodes;
}
void sequence::operator =(const sequence& source)
{//There is a linked list available for copy.
//Postcondition: The new linked list will be identical to the first.
node *tail_ptr;
if(this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
//CONSTANT MEMBER FUNCTIONS
sequence::size_type sequence::size() const {return many_nodes;}
sequence::value_type sequence::current() const
{//Precondition: is_item( ) returns true.
//Postcondition: The item returned is the current item on the sequence.
/*Current() returns the data stored in the current node, not the node itself
and not a pointer to this node. */
return cursor_ptr->data();
}
bool sequence::is_item() const
{//Postcondition: A true return value indicates that there is a valid
//"current" item that may be retrieved by activating the current
//member function (listed below). A false return value indicates that
//there is no valid current item.
return(size()>0);
}
}[code][/code] tags >>would appreciate a nudge in the right direction.
Last edited by Dave Sinkula : Nov 9th, 2005 at 9:32 am.
•
•
Join Date: Jul 2004
Location: North East Indiana
Posts: 491
Reputation:
Rep Power: 5
Solved Threads: 20
When it stops, what does it tell you? (a.exe has encountered a problem and needs to close?)
When messing with linked lists, I find my biggest problems are segmentation faults, which I sort of have to guess at since WinXP decided it'd be a good idea to hide the error messages in a generic one and give you only a core dump.
Check the code library for examples of linked lists and things that work if you need a bit of help with them.
When messing with linked lists, I find my biggest problems are segmentation faults, which I sort of have to guess at since WinXP decided it'd be a good idea to hide the error messages in a generic one and give you only a core dump.
Check the code library for examples of linked lists and things that work if you need a bit of help with them.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C Forum
- Previous Thread: Pop-Up Creation
- Next Thread: Printing text to a printer.


Linear Mode