|
-
Sep 19th, 2002, 12:01 PM
#1
Thread Starter
Ya ya Baby!!!Me is Back
Linked list
I want to delete a value that is in my linked list.
But I need something that can contain an adress. C++ have a variable that can hold an adress of a pointer without pointing on it?
-
Sep 19th, 2002, 01:15 PM
#2
Frenzied Member
Youve lost me =):
Code:
struct n
{
n* next;
n* prev;
int dat; // yeah, no templates here
};
remove_item(int data_to_remove,struct n*& head)
{
struct n* temp = head;
if(head->dat == data_to_remove)
{
head = head->next;
delete temp;
return 0;
}
while(temp->next)
{
if(temp->next->dat == data_to_remove)
{
struct n* del = temp->next;
temp->next = temp->next->next;
delete del;
return 0;
}
}
return 1;
}
Should do what you want.
Z.
-
Sep 20th, 2002, 09:18 AM
#3
Thread Starter
Ya ya Baby!!!Me is Back
Originally posted by Zaei
Youve lost me =):
Code:
struct n
{
n* next;
n* prev;
int dat; // yeah, no templates here
};
remove_item(int data_to_remove,struct n*& head)//I think you dont need to put struct ?
{
struct n* temp = head;
if(head->dat == data_to_remove) //Check if is the last n to remove, if yes then delete all
{
head = head->next;
delete temp;
return 0;
}
while(temp->next)//if it's not the last we remove only the one we wan to delete.
{
if(temp->next->dat == data_to_remove)
{
struct n* del = temp->next;
temp->next = temp->next->next;
delete del;
return 0;
}
}
return 1;
}
Should do what you want.
Z.
I added comment on your code, tell me if I am wrong or not?
-
Sep 20th, 2002, 01:08 PM
#4
Frenzied Member
Its C, you do need the struct (if I am remembering right =).
The second checks to see if the FIRST item is the one we are looking for, if so, simply chop it off.
Z.
-
Sep 20th, 2002, 03:21 PM
#5
Thread Starter
Ya ya Baby!!!Me is Back
-
Sep 24th, 2002, 07:20 AM
#6
C++ have a variable that can hold an adress of a pointer without pointing on it?
Huh?
Why not have a pointer, it's guaranteed to be the right size, and you don't HAVE to dereference it...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 24th, 2002, 09:34 AM
#7
Frenzied Member
CB is right. Plus, what he is saying:
Code:
char *ptr; when you: ptr++ is increments by one
long *ptr1; when you: ptr1++ it increments by 4 (length of long)
the compiler handles this automatically.
-
Sep 25th, 2002, 05:42 PM
#8
Fanatic Member
Yes, and u can do something along the lines of:
PHP Code:
// singly linked list
class Node {
public:
Node (int data = 0) : Data(data) {}
~Node () { if (pNext) delete pNext; pNext = 0; Data=0; }
Node * pNext;
int Data;
}
class List {
Node * m_pHead;
int m_count;
public:
List () : m_pHead(0) {}
~List () { if (m_pHead) delete m_pHead; m_pHead=0;m_count=0; }
bool Add(int dat) {
Node * pNew = new Node(dat);
if (pNew) {
m_pHead->pNext = pNew;
m_count++;
return true;
}
return false;
}
bool Remove (int dat) {
Node * pTemp = m_pHead;
Node * pDelete = 0;
// find it first
if (pTemp->Data == dat) {
pDelete = pTemp;
// disconect, delete, and reconnect
m_pHead = m_pHead->pNext;
pDelete->pNext = 0;
delete pDelete;
pDelete = 0;
m_count--;
return true;
}
while (pTemp->pNext) {
if (pTemp->pNext->pData == dat) {
m_count--;
pDelete = pTemp->pNext;
// disconnect and delete
pTemp->pNext = pDelete->pNext;
delete pDelete;
pDelete = 0;
return true;
}
return false;
}
Node * First () {
return m_pHead;
}
// Dont use this (BAD)
// cuz the list gets added to at the top, bottom, left, right,
// and everywhere!! so just use the first and iterate thru it
Node * Item (int index) {
int i = -1;
Node * pFound = m_pHead;
while (pFound) {
if (++i == index) {
return pFound;
}
}
return 0;
}
int Count () const {
return m_count;
}
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|