RSS Forums RSS

linked list deleteing problem

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 16
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

linked list deleteing problem

  #1  
Nov 20th, 2008
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.

//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.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,883
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: linked list deleteing problem

  #2  
Nov 20th, 2008
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);
}
Reply With Quote  
Posts: 16
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: linked list deleteing problem

  #3  
Nov 20th, 2008
that did it, thanks
Reply With Quote  
Posts: 9
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: linked list deleteing problem

  #4  
Nov 21st, 2008
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);
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Views: 346 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:06 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC