Sorted List deleteItem & insertItem not working

Hello. My deleteItem and insertItem functions are not working: when you try to delete an item from the array it says "the item is not in the list"...and when you try to insert an item, it does nothing but spit back 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 back to you when the display function is called. Any help would be appreciated in figuring this out. I believe the linear and binary search functions are OK, for what it's worth. Thank you.

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
#pragma once
#include <iostream>
#include <string>
#include "SortedList.h"
using namespace std;

SortedList::SortedList()
{
	length = 0;
}

SortedList::~SortedList() {}

int SortedList::linearSearch(int item)
{
	int location = 0;
	while ((item >= numbers[length]) && (location < length))
	location++;
	return location;
}

void SortedList::insertItem(int item)
{
	int location = 0;
	location = linearSearch(item);

	numbers[length] = location;
	length++;
}

int SortedList::binarySearch(int item)
{
	int first = 0;
	int midpoint = 0;
	int last = length - 1;

	while (true)
	{
		if (first > last)
		{
			return first;
		}

		midpoint = (first + last) / 2;

		if (item == numbers[midpoint])
		{
			return midpoint;
		}

		else if (item > numbers[midpoint])
		{
			first = (midpoint + 1);
		}

		else if (item < numbers[midpoint])
		{
			last = (midpoint - 1);
		}
	}
}

void SortedList::deleteItem(int item)
{

	int location = 0;
	location = binarySearch(item);

	while (item != numbers[location] && location < length)
	location++;
	if (location < length)
	{
		numbers[location] = numbers[length - 1];
		length--;
		cout << "The item has been deleted." << endl;
	}
	else
	{
		cout << "The number is not in the list." << endl;
	}
}

bool SortedList::isFull() const
{
	return (length == MAX_SIZE);
}

bool SortedList::isEmpty() const
{
	return (length == 0);
}

void SortedList::displayList() const
{
	int location = 0;
	if (location < length)
	{
		while (location < length)
		{
			cout << numbers[location] << endl;
			location++;
		}
	}
	else
		cout << "List is empty." << endl;
}

void SortedList::showMenu()
{
	cout << "a. Insert a number into the list." << endl;
	cout << "b. Delete a number from the list." << endl;
}
Last edited on
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
void SortedList::insertItem(int item)
{
	int location = 0;
	location = linearSearch(item);

	numbers[length] = location; //¿? ¿why do you store `location'?
	length++;
}

void SortedList::deleteItem(int item)
{

	int location = 0;
	location = binarySearch(item);

	while (item != numbers[location] && location < length) //¿what for?
		location++;
	if (location < length)
	{
		numbers[location] = numbers[length - 1]; //good bye, sorted property
		length--;
		cout << "The item has been deleted." << endl;
	}
	else
	{
		cout << "The number is not in the list." << endl;
	}
}
Hey I think my code right now is not executing from line 18 and skipping to the else statement. What should I do so that I don't say "good bye" to the sorted property?

Also I deleted the while loop and the location++ without any problems so I think you were right.

What should I store instead of location?
Last edited on
I updated my code, but it's not displaying the right deleted number and not putting things in ascending order. How can I fix that? Thanks for the help...anyone?

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
#include <iostream>
#include <string>
#include "SortedList.h"
using namespace std;

SortedList::SortedList()
{
	length = 0;
}

SortedList::~SortedList() {}

int SortedList::linearSearch(int item)
{
	int location = 0;
	while ((item >= numbers[length]) && (location < length))
	location++;
	return location;
}

void SortedList::insertItem(int item)
{
	int location = 0;
	location = linearSearch(item);

	for (int i = length; i > location; i--)
	{
		numbers[i] = numbers[i - 1];
	}
	numbers[location] = item;
	length++;
}

int SortedList::binarySearch(int item)
{
	int first = 0;
	int midpoint = 0;
	int last = length - 1;

	while (true)
	{
		if (first > last)
		{
			return first;
		}

		midpoint = (first + last) / 2;

		if (item == numbers[midpoint])
		{
			return midpoint;
		}

		else if (item > numbers[midpoint])
		{
			first = (midpoint + 1);
		}

		else if (item < numbers[midpoint])
		{
			last = (midpoint - 1);
		}
	}
}

void SortedList::deleteItem(int item)
{

	int location = 0;
	location = binarySearch(item);

	if (location < length) {
		for (int i = location + 1; i < length; i++)
		{
			numbers[i - 1] = numbers[i];
		}
		length--;
		cout << "The item has been deleted." << endl;
	}
	else {
		cout << "Item is not in the list." << endl;
	}
	
}


bool SortedList::isFull() const
{
	return (length == MAX_SIZE);
}

bool SortedList::isEmpty() const
{
	return (length == 0);
}

void SortedList::displayList() const
{
	int location = 0;
	if (location < length)
	{
		while (location < length)
		{
			cout << numbers[location] << endl;
			location++;
		}
	}
	else
		cout << "List is empty." << endl;
}

void SortedList::showMenu()
{
	cout << "a. Insert a number into the list." << endl;
	cout << "b. Delete a number from the list." << endl;
}
1
2
3
4
5
6
7
int SortedList::linearSearch(int item)
{
	int location = 0;
	while ((item >= numbers[length]) && (location < length))
	location++;
	return location;
}
test your functions
you check `item' against the same element several times. Moreover, that element (numbers[lenght]) is invalid, as it is outside the "used" range

if the item is not present, `binarySearch()' returns a value that is indistinguishable of a found case.
Last edited on
Topic archived. No new replies allowed.