| | |
linked list deleteing problem
Thread Solved
![]() |
•
•
Join Date: Sep 2008
Posts: 20
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
C++ Syntax (Toggle Plain Text)
//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:
C++ Syntax (Toggle Plain Text)
//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 6:53 pm.
try this
C++ Syntax (Toggle Plain Text)
//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); }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Aug 2008
Posts: 13
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
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
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ cdialogbar char class classes code coding compile console conversion convert count delete deploy desktop developer directshow dissertations dll double-linkedlist download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loan loop looping loops map math matrix memory multiple news node number online output pagerank pointer problem program programming project python random read recursion recursive reference risk rpg string strings superclass temperature template test text text-file tree tutorial url validator variable vector video win32 windows winsock wordfrequency wxwidgets






