Segmentation fault with linked list homework

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Segmentation fault with linked list homework

Postby aminards » Fri Feb 26, 2010 2:32 pm

I have an assignment that requires me to have a user enter values into a linked list. Then I have to output the original list.
Next the user enters a threshold value. Then I create two new lists, smaller and greater with values either smaller than the threshold
or greater than the threshold. The result are three linked lists; one smaller, one greater, and one that contains any data that was equal to the threshold.
I print out the the smaller and greater lists, then sort the lists in ascending order and print the sorted ists out.

My problem is that I receive a segmentation fault error after entering the threshold value.

My guess is that the problem is with the get_input function but I just do not see the error.

What is wrong with my function or lists?

Thank you,
Angie

Code: Select all
#include <iostream>
#include <cstdlib>

using namespace std;

struct Node
{

    int data;

    Node *link;

};

void get_input(Node*& head_ptr);
//Gets list of integers from user.  Calls list_insert().

void list_insert(Node* ptr, const int& entry);
//Inserts data into the list at the end.

void display_and_count(Node*& head_ptr);
//Prints out the integers in the list.

void split_them_up(Node*& head_ptr, Node*& smaller, Node*& greater, int threshold);
//Takes original list and moves data into three lists; one with values lower than the threshold,
//one with only values equal to the threshold(the original list),
//and one with values greater than the threshold.

void remove_after(Node* head_ptr);
//removes the node after the one pointed to by head_ptr.

void place(Node*& head_ptr, int x);
//Sorts the items in the list in increasing order.


int main()
{
Node* head_ptr, *smaller, *greater;

head_ptr = new Node;

head_ptr = NULL;

smaller = new Node;

smaller = NULL;

greater = new Node;

greater = NULL;


int threshold = 0;


get_input(head_ptr);

display_and_count(head_ptr);

cout << "What threshold value would you like to use?\n";

cin >> threshold;

cout << "Threshold value: " << threshold;

split_them_up(head_ptr, smaller, greater, threshold);

display_and_count(head_ptr);

cout << "SMALLER: ";

display_and_count(smaller);

cout << "GREATER: ";

display_and_count(greater);

cout << endl;

place(smaller, x);

cout << "Sorted smaller list: ";

display_and_count(smaller);

place(greater, x);

cout << "Sorted greater list: ";

display_and_count(greater);



return 0;

}


void get_input(Node *&head_ptr)

{

    Node* temp, *temp2;

    temp = new Node;

    int input;

    cout << "Enter a list of positive integers.  Enter a zero at the "

         << "end of the list.\n";

    cin >> input;

    if(input == 0)

        {

       cout << "Empty list.\n";

            exit(1);

   }

    temp->data = input;

    temp->link = head_ptr;

    head_ptr = temp;

    while(input != 0)

       {

           temp2 = temp;

      cin >> input;

      if(input == 0)

           break;

      temp = new Node;

      temp->data = input;

           temp2->link = temp;

        }

    temp->link = NULL;

}




void list_insert(Node* ptr, const int& entry)

{

    Node* temp;

    if(ptr == NULL)

        {

            cout << "Invalid insertion";

           exit(1);

   }

    temp = new Node;

    temp->data = entry;

    temp->link = ptr->link;

    ptr->link = temp;

}



void display_and_count(Node*& head_ptr)

{

    int count = 0;

    Node* temp;

    temp = head_ptr;

   
    cout << "Original list: ";

    do

        {

       if(temp == NULL)

      break;

            else

          {

          cout << temp -> data << " ";

          temp = temp -> link;

          count++;

      }

       }

    while(temp != NULL);

    if(count > 0)

        cout << " has " << count << " nodes." << endl;

    else

        cout << "is empty.\n";

}





void split_them_up(Node*& head_ptr, Node*& smaller, Node*& greater, int threshold)

{

    Node* temp = head_ptr, *temp_small = NULL, *temp_great = NULL, *temp_head = NULL;


    for(temp = head_ptr; temp != NULL; temp = temp->link)

        if(temp->data < threshold)

       {

      temp_small - new Node;

      temp_small->data = temp->data;

      smaller = temp_small;

      remove_after(head_ptr);

       }

   else if(temp->data > threshold)

       {

      temp_great = new Node;

      temp_great->data = temp->data;

      greater = temp_great;

      remove_after(head_ptr);

       }

}



void remove_after(Node* head_ptr)

{

    Node* temp;

    int info;

    if(head_ptr == NULL)

        {

            cout << "End.\n";

            exit(1);

        }

    else

   {

       temp = head_ptr->link;

       if(temp == NULL)

      {

          cout << "End.\n";

          exit(1);

              }

       else

      {

          info = temp->data;

          head_ptr->link = temp->link;

          delete temp;

      }

   }


}



void place(Node*& head_ptr, int x)

{

    Node* pointer, *shadow, *r;

    bool found = false;

    pointer = head_ptr;

    shadow == NULL;

    while((pointer != NULL) && (!found))

        {

       if(x < pointer->data)

       found = true;

       else

      {

          shadow = pointer;

          pointer = pointer->link;

         }

   }

    if(shadow == NULL)

   {

       r = new Node;

       r->data = x;

       r->link = head_ptr;

       head_ptr = r;

   }

    else

   {

       r = new Node;

       r->data = x;

       r->link = shadow->link;

       shadow->link = r;

   }

    return;
}
aminards
 
Posts: 3
Joined: Fri Feb 26, 2010 2:12 pm

Re: Segmentation fault with linked list homework

Postby Alvaro » Fri Feb 26, 2010 2:47 pm

Code: Select all
head_ptr = new Node;

head_ptr = NULL;


I have a hard time getting past this. What do you think happens with the memory that was allocated in the first of those two lines?
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Segmentation fault with linked list homework

Postby Alvaro » Fri Feb 26, 2010 2:50 pm

Code: Select all
place(smaller, x);


Does that even compile? What is `x'?
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Segmentation fault with linked list homework

Postby aminards » Fri Feb 26, 2010 3:20 pm

Alvaro wrote:
Code: Select all
head_ptr = new Node;

head_ptr = NULL;


I have a hard time getting past this. What do you think happens with the memory that was allocated in the first of those two lines?


I think the first line, head_ptr = new Node; makes the pointer head_ptr point to an empty node. Then head+ptr = NULL; means that the node that head_ptr is pointing to is not pointing to anything/it is the end of the list.
aminards
 
Posts: 3
Joined: Fri Feb 26, 2010 2:12 pm

Re: Segmentation fault with linked list homework

Postby aminards » Fri Feb 26, 2010 3:22 pm

Alvaro wrote:
Code: Select all
place(smaller, x);


Does that even compile? What is `x'?



Everything compiles with no errors. x is an integer value that is associated with a node.
aminards
 
Posts: 3
Joined: Fri Feb 26, 2010 2:12 pm


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests