My last Bool Function

I do not Understand how to make a function that checks to see if the linked lists are in Ascending order, especially since you can not index in Linked Lists. That is what makes it even harder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Node1.cpp
#include <iostream>
#include <string>
#include <cassert>
#include <cstdlib>
#include "node1.h"

using namespace std;

namespace main_savitch_5
{
	std::size_t list_length(const node* head_ptr)
	{
		const node *cursor;
		size_t answer;
		answer = 0;
		
		for(cursor = head_ptr; cursor != NULL; cursor = cursor ->link())
		{
			++answer;
		}
		return answer;
	}
    void list_head_insert(node*& head_ptr, const node::value_type& entry)
    {
    	head_ptr = new node(entry, head_ptr);
    }
    void list_insert(node* previous_ptr, const node::value_type& entry)
    {
    	node *insert_ptr;

    	insert_ptr = new node;
    	insert_ptr->set_data(entry);
    	insert_ptr->set_link(previous_ptr->link());
    	insert_ptr->set_link(insert_ptr);
    }
    node* list_search(node* head_ptr, const node::value_type& target)
    {
    	node *cursor;

    	for(cursor = head_ptr; cursor != NULL; cursor = cursor ->link())
    	{
    		if(target == cursor->data())
    		{
    			return cursor;
    		}
    	return(NULL);
    	}
    }
    const node* list_search(const node* head_ptr, const node::value_type& target)
    {
    	const node *cursor;

    	for(cursor = head_ptr; cursor != NULL; cursor = cursor->link())
    	{
    		if(target == cursor->data())
    		{
    			return(cursor);
    		}
    	}
    	return(NULL);
    }
    node* list_locate(node* head_ptr, std::size_t position)
    {
    	return(NULL);
    }
    const node* list_locate(const node* head_ptr, std::size_t position)
    {
    	return(NULL);
    }
    void list_head_remove(node*& head_ptr)
    {
    	node *remove_ptr;

    	remove_ptr = head_ptr;
    	head_ptr = head_ptr->link();
    	delete remove_ptr;
    }
    void list_remove(node* previous_ptr)
    {
    	node *remove_ptr;

    	remove_ptr = previous_ptr->link();
    	previous_ptr->set_link(remove_ptr->link());

    }
    void list_clear(node*& head_ptr)
    {
    	while(head_ptr != NULL)
    	{
    		list_head_remove(head_ptr);
    	}
    }
    void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
    {
    	head_ptr = NULL;
    	tail_ptr = NULL;

    	if(source_ptr ==NULL)
    	{
    		return;
    	}

    	list_head_insert(head_ptr, source_ptr->data());
    	tail_ptr = head_ptr;

    	source_ptr = source_ptr->link();
    	while(source_ptr != NULL)
    	{
    		list_insert(tail_ptr, source_ptr->data());
    		tail_ptr = tail_ptr->link();
    		source_ptr = source_ptr->link();
    	
       }
    }
//       Precondition: head_ptr is the head pointer of a linked list
//     Precondition: value_type has implemented the operator "<"
//     Postcondition: return value is `true` iff the list items are currently in
//        ascending order.  If there are fewer than two items in the list, return
//        value is `true`.
    bool list_is_ordered(const node* head_ptr)
    {
       int accum;
       while(head_ptr != Null)
          if(head_ptr != head_ptr->link[accum];
             return(false)
          else()
             ++accum
    }

    }
} 





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Node1.h
//   bool list_is_ordered(const node* head_ptr)
//     Precondition: head_ptr is the head pointer of a linked list
//     Precondition: value_type has implemented the operator "<"
//     Postcondition: return value is `true` iff the list items are currently in
//        ascending order.  If there are fewer than two items in the list, return
//        value is `true`.
//     NOTE: The implementation uses the "<" operator for comparing list items.
//
// DYNAMIC MEMORY usage by the toolkit:
//   If there is insufficient dynamic memory, then the following functions throw
//   bad_alloc:
//      - node constructor
//      - list_head_insert
//      - list_insert
//      - list_copy

#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <cstdlib>    // Provides size_t and NULL
#include "Person.h"   // so we can store Person data

namespace main_savitch_5
{
    class node
    {
        public:
        // TYPEDEF
        typedef Person value_type;  // NOTE CHANGE from textbook version

        // CONSTRUCTOR
        node(const value_type& init_data = value_type( ),
             node* init_link = NULL)
        { data_field = init_data; link_field = init_link; }

        // Member functions to set the data and link fields:
            void set_data(const value_type& new_data) { data_field = new_data; }
            void set_link(node* new_link)             { link_field = new_link; }

        // Constant member function to retrieve the current data:
        value_type data( ) const { return data_field; }

        // Two slightly different member functions to retrieve the current link:
        const node* link( ) const { return link_field; }
              node* link( )       { return link_field; }

        private:
            value_type data_field;
            node* link_field;
    };  // end node class

    // FUNCTIONS for the linked list toolkit
    std::size_t list_length(const node* head_ptr);
    void list_head_insert(node*& head_ptr, const node::value_type& entry);
    void list_insert(node* previous_ptr, const node::value_type& entry);
    node* list_search(node* head_ptr, const node::value_type& target);
    const node* list_search(const node* head_ptr, const node::value_type& target);
    node* list_locate(node* head_ptr, std::size_t position);
    const node* list_locate(const node* head_ptr, std::size_t position);
    void list_head_remove(node*& head_ptr);
    void list_remove(node* previous_ptr);
    void list_clear(node*& head_ptr);
    void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);
    bool list_is_ordered(const node* head_ptr);
}

#endif 
Last edited on
Topic archived. No new replies allowed.