linked list deleteing problem
Please support our C++ advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 16
Reputation:
Solved Threads: 0
whenever the clearMemory function is called i get a segmentation fault and i can not figure out whats wrong. The function is supposed to delete all pointers. The function brings in the pointer to the linked list by reference. The nodes in the list are structs i named bus that contain another liked list i called bandmembers.
the function prototype and definition are:
thank you in advance for any help
//band member struct to hold band member info
struct bandMember
{
//first name holder
string firstName;
//last name holder
string lastName;
//instrument holder
string instrument;
//pointer to another bandmember
bandMember *memberPtr;
};
//struct to hold bus info
struct bus
{
//bus designation
int busNum;
//bus capasity, same for all buses
int busCap;
//list of bandmembers on each bus
bandMember *memListPtr;
//pointer to another bus struct
bus *busPtr;
};the function prototype and definition are:
//function prototype
void clearMemory(bus* &ptr);
//function to clear all pointers
void clearMemory(bus* &pointer)
{
bus *last, *next;
bandMember *current, *previous;
last = pointer;
next = last;
current = last->memListPtr;
previous = current;
while(last != NULL)
{
while(current != NULL)
{
previous = current;
current = current->memberPtr;
delete previous;
}
next = last;
last = last->busPtr;
delete next;
current = last->memListPtr;
previous = current;
}
pointer = NULL;
}thank you in advance for any help
Last edited by kyosuke0 : Nov 20th, 2008 at 5:53 pm.
try this
//function to clear all pointers
void clearMemory(bus* &pointer)
{
bus *next;
bandMember *current;
next = pointer;
while(next != NULL)
{
current = next->memListPtr;
while(current)
{
bandMember *hold = current;
current = current->memberPtr;
delete hold;
}
bus* hold = next;
next = next->busPtr;
delete hold;
}
pointer = NULL;
}
void Add(bus*& head)
{
bus* node = new bus;
node->busPtr = 0;
node->memListPtr = 0;
if(head == NULL)
head = node;
else
{
bus* next = head;
while(next->busPtr)
next = next->busPtr;
next->busPtr = node;
}
}
int main()
{
bus* head = 0;
for(int i = 0; i < 5; i++)
Add(head);
clearMemory(head);
}•
•
Posts: 9
Reputation:
Solved Threads: 1
struct node
{
int data;
struct node *link;
};
void delet(struct node **currNode,int location)
{
int i;
struct node *temp,*temp1,*temp2;
int cnt;
temp= *currNode;
cnt=count(currNode);
if(location>cnt)
{
printf("\nIndex should be <= %d",cnt);
}
for(i=1;i<=cnt;i++)
{
if(location==1&&i==1)
{
*currNode=temp->link;
free(temp);
}
if(i==location-1)
{
temp1=temp->link;
temp->link=temp->link->link;
free(temp1);
}
temp=temp->link;
}
}
int count(struct node **currNode)
{
int count;
struct node *temp;
temp= *currNode;
if(*currNode==NULL)
{
count=0;
}
else
count=1;
while(temp->link!=NULL)
{
temp=temp->link;
count++;
}
return count;
}
void main()
{
int value,index;
struct node *currNode;
currNode=NULL;
printf("Enter Index : ");
scanf("%d",&value);
//delete node in the given index of a link list
delet(&currNode,value);
}![]() |
Other Threads in the C++ Forum
- Previous Thread: help with c++ coursework - code so far included.
- Next Thread: Help... Link list problem
•
•
•
•
Views: 346 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode