Linked List Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 48
Reputation: ayk-retail is an unknown quantity at this point 
Solved Threads: 0
ayk-retail ayk-retail is offline Offline
Light Poster

Linked List Problem

 
0
  #1
Mar 30th, 2008
I have a major problem. I need to write a program using linked lists that will display...
A B C D E F
and then...
F E D C B A
without using a tail pointer. When I run the program, it displays the first set of letters but crashes immediately thereafter. Visual Studio does not display any errors within the program. I was given some functions to call for for the program. Here is the code...

Implementation Code:
  1. #include <iostream>
  2. #include "node1.h"
  3. #include <cassert>
  4. #include <cstdlib>
  5. using namespace std;
  6. using namespace main_savitch_5;
  7.  
  8.  
  9. void print_list( node* head_ptr);
  10. node* reverse_list(node* head_ptr);
  11.  
  12. void main()
  13. {
  14. node* head_ptr;
  15.  
  16. list_head_insert(head_ptr, 'F');
  17. list_head_insert(head_ptr, 'E');
  18. list_head_insert(head_ptr, 'D');
  19. list_head_insert(head_ptr, 'C');
  20. list_head_insert(head_ptr, 'B');
  21. list_head_insert(head_ptr, 'A');
  22.  
  23. print_list(head_ptr);
  24. cout << endl;
  25. reverse_list(head_ptr);
  26. print_list(head_ptr);
  27. return;
  28. }
  29.  
  30. void print_list(node* ptr)
  31. {
  32. for (; ptr; ptr = ptr->link())
  33. cout << ptr->data() << " ";
  34. cout << endl;
  35. return;
  36. }
  37.  
  38.  
  39. node* reverse_list(node* head_ptr)
  40. {
  41. cout << "Reversed..." << endl;
  42. node* result_ptr = NULL;
  43. node* curr_ptr = NULL;
  44. while (head_ptr)
  45. {
  46. curr_ptr = head_ptr;
  47. head_ptr = head_ptr->link( );
  48. curr_ptr->set_link(result_ptr);
  49. result_ptr = curr_ptr;
  50. }
  51. return result_ptr;
  52. //node* buffer1;
  53. //node* buffer2;
  54. //int i = 5;
  55. //buffer2 = ptr->info;
  56. //for (buffer1 = ptr->info; ptr; ptr = ptr->next)
  57. //{
  58. // buffer2 =
  59.  
  60. //}
  61. //for (; ptr; ptr = ptr->next)
  62. //cout << ptr->info << " ";
  63. }

Here is the driver code:
  1. // FILE: node1.cxx
  2. // IMPLEMENTS: The functions of the node class and the
  3. // linked list toolkit (see node1.h for documentation).
  4. // INVARIANT for the node class:
  5. // The data of a node is stored in data_field, and the link in link_field.
  6.  
  7. #include "node1.h"
  8. #include <cassert> // Provides assert
  9. #include <cstdlib> // Provides NULL and size_t
  10. using namespace std;
  11.  
  12. namespace main_savitch_5
  13. {
  14. size_t list_length(const node* head_ptr)
  15. // Library facilities used: cstdlib
  16. {
  17. const node *cursor;
  18. size_t answer;
  19.  
  20. answer = 0;
  21. for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
  22. ++answer;
  23.  
  24. return answer;
  25. }
  26.  
  27. void list_head_insert(node*& head_ptr, const node::value_type& entry)
  28. {
  29. head_ptr = new node(entry, head_ptr);
  30. }
  31.  
  32. void list_insert(node* previous_ptr, const node::value_type& entry)
  33. {
  34. node *insert_ptr;
  35.  
  36. insert_ptr = new node(entry, previous_ptr->link( ));
  37. previous_ptr->set_link(insert_ptr);
  38. }
  39.  
  40. node* list_search(node* head_ptr, const node::value_type& target)
  41. // Library facilities used: cstdlib
  42. {
  43. node *cursor;
  44.  
  45. for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
  46. if (target == cursor->data( ))
  47. return cursor;
  48. return NULL;
  49. }
  50.  
  51. const node* list_search(const node* head_ptr, const node::value_type& target)
  52. // Library facilities used: cstdlib
  53. {
  54. const node *cursor;
  55.  
  56. for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
  57. if (target == cursor->data( ))
  58. return cursor;
  59. return NULL;
  60. }
  61.  
  62. node* list_locate(node* head_ptr, size_t position)
  63. // Library facilities used: cassert, cstdlib
  64. {
  65. node *cursor;
  66. size_t i;
  67.  
  68. assert (0 < position);
  69. cursor = head_ptr;
  70. for (i = 1; (i < position) && (cursor != NULL); i++)
  71. cursor = cursor->link( );
  72. return cursor;
  73. }
  74.  
  75. const node* list_locate(const node* head_ptr, size_t position)
  76. // Library facilities used: cassert, cstdlib
  77. {
  78. const node *cursor;
  79. size_t i;
  80.  
  81. assert (0 < position);
  82. cursor = head_ptr;
  83. for (i = 1; (i < position) && (cursor != NULL); i++)
  84. cursor = cursor->link( );
  85. return cursor;
  86. }
  87.  
  88. void list_head_remove(node*& head_ptr)
  89. {
  90. node *remove_ptr;
  91.  
  92. remove_ptr = head_ptr;
  93. head_ptr = head_ptr->link( );
  94. delete remove_ptr;
  95. }
  96.  
  97. void list_remove(node* previous_ptr)
  98. {
  99. node *remove_ptr;
  100.  
  101. remove_ptr = previous_ptr->link( );
  102. previous_ptr->set_link( remove_ptr->link( ) );
  103. delete remove_ptr;
  104. }
  105.  
  106. void list_clear(node*& head_ptr)
  107. // Library facilities used: cstdlib
  108. {
  109. while (head_ptr != NULL)
  110. list_head_remove(head_ptr);
  111. }
  112.  
  113. void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
  114. // Library facilities used: cstdlib
  115. {
  116. head_ptr = NULL;
  117. tail_ptr = NULL;
  118.  
  119. // Handle the case of the empty list.
  120. if (source_ptr == NULL)
  121. return;
  122.  
  123. // Make the head node for the newly created list, and put data in it.
  124. list_head_insert(head_ptr, source_ptr->data( ));
  125. tail_ptr = head_ptr;
  126.  
  127. // Copy the rest of the nodes one at a time, adding at the tail of new list.
  128. source_ptr = source_ptr->link( );
  129. while (source_ptr != NULL)
  130. {
  131. list_insert(tail_ptr, source_ptr->data( ));
  132. tail_ptr = tail_ptr->link( );
  133. source_ptr = source_ptr->link( );
  134. }
  135. }
  136.  
  137. }

Here is the header code:
  1. // FILE: node1.h
  2. // PROVIDES: A class for a node in a linked list, and list manipulation
  3. // functions, all within the namespace main_savitch_5
  4. //
  5. // TYPEDEF for the node class:
  6. // Each node of the list contains a piece of data and a pointer to the
  7. // next node. The type of the data is defined as node::value_type in a
  8. // typedef statement. The value_type may be any
  9. // of the built-in C++ classes (int, char, ...) or a class with a copy
  10. // constructor, an assignment operator, and a test for equality (x == y).
  11. //
  12. // CONSTRUCTOR for the node class:
  13. // node(
  14. // const value_type& init_data = value_type(),
  15. // node* init_link = NULL
  16. // )
  17. // Postcondition: The node contains the specified data and link.
  18. // NOTE: The default value for the init_data is obtained from the default
  19. // constructor of the value_type. In the ANSI/ISO standard, this notation
  20. // is also allowed for the built-in types, providing a default value of
  21. // zero. The init_link has a default value of NULL.
  22. //
  23. // NOTE:
  24. // Some of the functions have a return value which is a pointer to a node.
  25. // Each of these functions comes in two versions: a non-const version (where
  26. // the return value is node*) and a const version (where the return value
  27. // is const node*).
  28. // EXAMPLES:
  29. // const node *c;
  30. // c->link( ) activates the const version of link
  31. // list_search(c,... calls the const version of list_search
  32. // node *p;
  33. // p->link( ) activates the non-const version of link
  34. // list_search(p,... calls the non-const version of list_search
  35. //
  36. // MEMBER FUNCTIONS for the node class:
  37. // void set_data(const value_type& new_data)
  38. // Postcondition: The node now contains the specified new data.
  39. //
  40. // void set_link(node* new_link)
  41. // Postcondition: The node now contains the specified new link.
  42. //
  43. // value_type data( ) const
  44. // Postcondition: The return value is the data from this node.
  45. //
  46. // const node* link( ) const <----- const version
  47. // node* link( ) <----------------- non-const version
  48. // See the note (above) about the const version and non-const versions:
  49. // Postcondition: The return value is the link from this node.
  50. //
  51. // FUNCTIONS in the linked list toolkit:
  52. // size_t list_length(const node* head_ptr)
  53. // Precondition: head_ptr is the head pointer of a linked list.
  54. // Postcondition: The value returned is the number of nodes in the linked
  55. // list.
  56. //
  57. // void list_head_insert(node*& head_ptr, const node::value_type& entry)
  58. // Precondition: head_ptr is the head pointer of a linked list.
  59. // Postcondition: A new node containing the given entry has been added at
  60. // the head of the linked list; head_ptr now points to the head of the new,
  61. // longer linked list.
  62. //
  63. // void list_insert(node* previous_ptr, const node::value_type& entry)
  64. // Precondition: previous_ptr points to a node in a linked list.
  65. // Postcondition: A new node containing the given entry has been added
  66. // after the node that previous_ptr points to.
  67. //
  68. // const node* list_search(const node* head_ptr, const node::value_type& target)
  69. // node* list_search(node* head_ptr, const node::value_type& target)
  70. // See the note (above) about the const version and non-const versions:
  71. // Precondition: head_ptr is the head pointer of a linked list.
  72. // Postcondition: The pointer returned points to the first node containing
  73. // the specified target in its data member. If there is no such node, the
  74. // null pointer is returned.
  75. //
  76. // const node* list_locate(const node* head_ptr, size_t position)
  77. // node* list_locate(node* head_ptr, size_t position)
  78. // See the note (above) about the const version and non-const versions:
  79. // Precondition: head_ptr is the head pointer of a linked list, and
  80. // position > 0.
  81. // Postcondition: The pointer returned points to the node at the specified
  82. // position in the list. (The head node is position 1, the next node is
  83. // position 2, and so on). If there is no such position, then the null
  84. // pointer is returned.
  85. //
  86. // void list_head_remove(node*& head_ptr)
  87. // Precondition: head_ptr is the head pointer of a linked list, with at
  88. // least one node.
  89. // Postcondition: The head node has been removed and returned to the heap;
  90. // head_ptr is now the head pointer of the new, shorter linked list.
  91. //
  92. // void list_remove(node* previous_ptr)
  93. // Precondition: previous_ptr points to a node in a linked list, and this
  94. // is not the tail node of the list.
  95. // Postcondition: The node after previous_ptr has been removed from the
  96. // linked list.
  97. //
  98. // void list_clear(node*& head_ptr)
  99. // Precondition: head_ptr is the head pointer of a linked list.
  100. // Postcondition: All nodes of the list have been returned to the heap,
  101. // and the head_ptr is now NULL.
  102. //
  103. // void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
  104. // Precondition: source_ptr is the head pointer of a linked list.
  105. // Postcondition: head_ptr and tail_ptr are the head and tail pointers for
  106. // a new list that contains the same items as the list pointed to by
  107. // source_ptr. The original list is unaltered.
  108. // void list_piece(
  109. // const node* start_ptr, const node* end_ptr,
  110. // node*& head_ptr, node*& tail_ptr
  111. // )
  112. // Precondition: start_ptr and end_ptr are pointers to nodes on the same
  113. // linked list, with the start_ptr node at or before the end_ptr node
  114. // Postcondition: head_ptr and tail_ptr are the head and tail pointers for a
  115. // new list that contains the items from start_ptr up to but not including
  116. // end_ptr. The end_ptr may also be NULL, in which case the new list
  117. // contains elements from start_ptr to the end of the list.
  118. //
  119. // DYNAMIC MEMORY usage by the toolkit:
  120. // If there is insufficient dynamic memory, then the following functions throw
  121. // bad_alloc: the constructor, list_head_insert, list_insert, list_copy,
  122. // list_piece.
  123. #include <iostream>
  124. #ifndef MAIN_SAVITCH_NODE1_H
  125. #define MAIN_SAVITCH_NODE1_H
  126. #include <cstdlib> // Provides size_t and NULL
  127.  
  128. namespace main_savitch_5
  129. {
  130. class node
  131. {
  132. public:
  133. // TYPEDEF
  134. typedef char value_type;
  135.  
  136. // CONSTRUCTOR
  137. node(
  138. const value_type& init_data = value_type( ),
  139. node* init_link = NULL
  140. )
  141. { data_field = init_data; link_field = init_link; }
  142.  
  143. // Member functions to set the data and link fields:
  144. void set_data(const value_type& new_data) { data_field = new_data; }
  145. void set_link(node* new_link) { link_field = new_link; }
  146.  
  147. // Constant member function to retrieve the current data:
  148. value_type data( ) const { return data_field; }
  149.  
  150. // Two slightly different member functions to retreive
  151. // the current link:
  152. const node* link( ) const { return link_field; }
  153. node* link( ) { return link_field; }
  154.  
  155. private:
  156. value_type data_field;
  157. node* link_field;
  158. };
  159.  
  160. // FUNCTIONS for the linked list toolkit
  161. size_t list_length(const node* head_ptr);
  162. void list_head_insert(node*& head_ptr, const node::value_type& entry);
  163. void list_insert(node* previous_ptr, const node::value_type& entry);
  164. node* list_search(node* head_ptr, const node::value_type& target);
  165. const node* list_search
  166. (const node* head_ptr, const node::value_type& target);
  167. node* list_locate(node* head_ptr, size_t position);
  168. const node* list_locate(const node* head_ptr, size_t position);
  169. void list_head_remove(node*& head_ptr);
  170. void list_remove(node* previous_ptr);
  171. void list_clear(node*& head_ptr);
  172. void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);
  173. }
  174.  
  175. #endif

Thank you so much for your help!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Linked List Problem

 
1
  #2
Mar 30th, 2008
# void main()
main returns an int.

# node* head_ptr;
An uninitialised pointer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 48
Reputation: ayk-retail is an unknown quantity at this point 
Solved Threads: 0
ayk-retail ayk-retail is offline Offline
Light Poster

Re: Linked List Problem

 
0
  #3
Mar 30th, 2008
Thank you so much! It doesn't freeze anymore! However, it just displays the first...
A B C D E F
and then...
A
and then stops. I am not sure what to do about that. Also, can you recommend what I should initialize this to...
# node* head_ptr;

Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Linked List Problem

 
0
  #4
Mar 31st, 2008
your link list implementation is stack based implementation,
to reverse it you first need to know how to reverse the list.

there are three ways of doing this.
1. Recursive.
2. using explicity stack.
3. using iterative approach. ( the one you are using).

your code says the following
  1. node* reverse_list(node* head_ptr){
  2. cout << "Reversed..." << endl;
  3. node* result_ptr = NULL;
  4. node* curr_ptr = NULL;
  5. while (head_ptr)
  6. {
  7. curr_ptr = head_ptr;
  8. head_ptr = head_ptr->link( );
  9. curr_ptr->set_link(result_ptr);
  10. result_ptr = curr_ptr;
  11. }
  12. return result_ptr;
  13. }

what it does.
statement 1. do nothing but printing.
you got two extra pointers.
traversing the list on the basis of headPtr.
holding the headPtr in currentPtr.
incrementing the headPtr.
setting NULL in the currentPtr link.
setting resultPtr = CurrentPtr ( which is same as headPtr ).

this was your first iteration. but where you reversed the list.

always remember to check the preconditions and postconditions.
in the reverse case the precondition is to check if the list is null, or it contains only one element. reversing that would be nothing ...

lets do the reverse.
  1. void Reverse (Link* head)
  2. {
  3. //checking preconditions.
  4. if (!head && !head->next)
  5. return;
  6. //here we are sure that second node exists.
  7. Link* temp = head->next; // holding 2nd node reference.
  8. Link* iter = temp->next; // hold the reference of 3rd Node OR NULL.
  9. head->next = 0; // setting the first Node next = 0; (why? think)
  10. while (temp) // traversing from 2nd node to the end.
  11. {
  12. iter = temp->next; // saving the 3rd Node.
  13. temp->next = head; // reversing the 2nd to point to first.
  14. head = temp; // incrementing head.
  15. temp = iter; // incrementing 2nd to 3rd saved abov.
  16. }
  17. }

hope this help.
try the other two methods yourself.
Last edited by Laiq Ahmed; Mar 31st, 2008 at 4:49 am. Reason: Comments messed up
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC