Linked List

Line 75 it breaks. I believe while(temp!=NULL) works there but have no clue what to do after that. Maybe use bool. I also believe it will break in the delete section loop aswell. Any help? Yes I know I already posted this 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
#include<iostream>
#include <cstdlib>

using namespace std;

char name[20];

class room
{
 public:
    int data;
    room* next;
};

class Pointers
{
	public:
	Pointers()
	{
		top = NULL;
		count=0;
	}

	bool empty()
	{
		if(count==0)
			return true;
		else
			return false;
	}
//ap is after_posistion
	void insert(int ap, int element)
	{
		room *newelement = new room;

		if(top == NULL)
		{       
			newelement->data = element;
			newelement->next = NULL;
			top = newelement;
			count++;
		}
		else
		{
			room *temp;
			temp = top;
			for(int i=0;i<ap; i++)
				temp=temp->next;

			newelement->data = element;
			newelement->next = temp->next;
			temp->next = newelement;
			count++;
		}
	}

	void remove(int ap)
	{
		room *temp;
		temp = top;
		for(int i=0;i<ap;i++)
			temp=temp->next;

		room * old = temp->next;
		temp->next = old->next;
		count--;
		delete(old);
	}


	void print()
	{
		room *temp;
		temp = top;
		while(temp!=NULL)
		{
			cout<<temp->data<<",";
			temp=temp->next;
		}
	}
	private:
		room *top;
		int count; //head
		int stackData;      
};

int main() {   
    Pointers *sl = new Pointers();  
    sl->insert(0,10);
    sl->insert(10,20);
    sl->insert(20,30);
    sl->insert(20,40);
    sl->insert(30,50);
    sl->insert(50,60);
    sl->insert(5,70);
    sl->remove(30);
    sl->remove(10);
    sl->remove(50);
    sl->print();

    cin>>name;
    return 0;
}
[code]
[/code]
Last edited on
I'm trying to make my linked list from imputing and deleting from location to imputing and deleting from the data alone.
I've really no idea what that could possibly mean. What is location? What data?

This is certainly wrong:
1
2
			for(int i=0;i<ap; i++)
				temp=temp->next;
line 90 will crash. temp becomes NULL but you ignore it.

chang it to:
1
2
3
4
5
			room *temp1=top; // use temp1 on line 51/52
			for(temp=top;temp!=NULL; temp=temp->next)
				temp1 = temp;
				if(ap > temp->data)
				  break;
> This is certainly wrong
It's not your fault that your function is used incorrectly.
`ap' should be a valid position, between 0 and size-1

> chang it to
that has a different meaning, now you are doing a search on the value passed.


> from imputing and deleting from location to imputing and deleting from the data alone.
>> I've really no idea what that could possibly mean
weird, your suggestion responded to that problem.
Sorry for the confusion I set up this to hold addresses and there value. I'm trying to store value to the next value so forth.
Also this works perfectly with it because the first num is ap then it's finding where to put it. The part I'm stuck on is linking 10 to 20 20 to 30 so forth in the code above. Basically getting rid of where to put the numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {   
    PointerList *sl = new PointerList();  
    sl->insert(0,10);
    sl->insert(0,20);
    sl->insert(1,30);
    sl->insert(1,40);
    sl->insert(0,50);
    sl->insert(3,60);
	sl->insert(5,70);
    sl->remove(3);
    sl->print();


	cin>>name;
    return 0;
}
Last edited on
Are you saying that you want to insert the items so that the list is sorted? I hope so, because that's what I'm going to talk about :)

There's a trick to inserting/deleting items in a singly linked list. Rather than using a pointer to the previous item, use a pointer to the pointer to the item. This will always point to top or the next pointer of an item. Now you can insert an item like this:
// Insert element into list. pp points to top, or a room's next pointer
1
2
3
4
5
6
7
8
void insert(room **pp, int element)
{
	room *newelement = new room;
	newelement->data = element;
	newelement->next = *pp;
	*pp = newelement;
	++count;
}

See how easy that is?

Now to insert X into the list in sorted order, you'll want to find the link to the first element that is greater than X:
1
2
3
4
5
6
7
8
room **findGT(int X)
{
	room **pp, *p;
	for (pp = &top; p=*pp; pp = &p->next) {
		if (p->element > X) break;
	}
	return pp;
}


Now inserting in sorted order is easy:
1
2
3
4
void insert(int X)
{
	insert(findGT(X), X);
}


removing is the same:

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
// Return pointer to pointer to element equal to X, or the last link.
room **find(int X)
{
	room **pp, *p;
	for (pp = &top; p=*pp; pp = &p->next) {
		if (p->element == X) break;
	}
	return pp;
}

// remove the item pointed to by *pp.  Handles the case where
// pp points to NULL. Returns true if item removed, false otherwise
bool remove(room **pp)
{
	room *item = *pp;
	if (item) {
		*pp = item->next;
		item->next = nullptr;
		delete item;
		return true;
	}
	return false;
}

bool remove(int X)
{
	return remove(find(X));
}


Putting it all together:

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 <cstdlib>

using namespace std;

char name[20];

class room
{
public:
    int data;
    room* next;
};

class Pointers
{
public:
    Pointers()
    {
        top = NULL;
        count=0;
    }

    bool empty()
    {
        return (count == 0);
    }

    // Return pointer to pointer to element equal to X, or the last
    // link.
    room **find(int X)
    {
        room **pp, *p;
        for (pp = &top; p=*pp; pp = &p->next) {
            if (p->data == X) break;
        }
        return pp;
    }

    room **findGT(int X)
    {
        room **pp, *p;
        for (pp = &top; p=*pp; pp = &p->next) {
            if (p->data > X) break;
        }
        return pp;
    }

    void insert(room **pp, int element)
    {
        room *newelement = new room;
        newelement->data = element;
        newelement->next = *pp;
        *pp = newelement;
        ++count;
    }

    void insert(int X)
    {
        insert(findGT(X), X);
    }


    // remove the item pointed to by *pp.  Handles the case where pp
    // points to NULL. Returns true if item removed, false otherwise
    bool remove(room **pp)
    {
        room *item = *pp;
        if (item) {
            *pp = item->next;
            item->next = NULL;
            delete item;
            return true;
        }
        return false;
    }

    bool remove(int X)
    {
        return remove(find(X));
    }

    void print()
    {
        room *temp;
        temp = top;
        while(temp!=NULL)
            {
                cout<<temp->data<<",";
                temp=temp->next;
            }
        cout << endl;
    }
private:
    room *top;
    int count; //head
    int stackData;
};

int main() {
    Pointers *sl = new Pointers();
    sl->insert(10);
    sl->insert(20);
    sl->insert(30);
    sl->insert(40);
    sl->insert(50);
    sl->insert(60);
    sl->insert(70);
    sl->remove(30);
    sl->remove(10);
    sl->remove(50);
    sl->print();

    cin>>name;
    return 0;
}

Yeas thank you this will help me a lot thanks again.
I know I had this the other day but I figured out what I'm trying to do now. My for loop (line75)in the insert is breaking because of null as coder777 said. Can anyone help me there? I believe while(temp!=NULL) works but I'm lost after that.
Last edited on
Topic archived. No new replies allowed.