Pan.J Pan.J - 1 month ago 17
C Question

How to solve seg fault?

When my code do searching. If the data is found, terminal says Segmentation fault 11. If the data is not found, it would be no Segmentation fault.
#include
#include
#include "list.h"

typedef struct list {
int data;
struct list *next;
} List;

List *head, *tail;


void prtError( char *errMsg )
{
printf( "%s \n", errMsg );
}

void prtList()
{
List *p;
for ( p = head->next; p != NULL; p = p->next )
printf( "%4d", p->data );
printf( "\n");
}

List *init()
{
head = ( List * ) malloc( sizeof( List ) );
if ( head == NULL ) {
prtError( "Insufficient memory!" );
return( NULL );
}
head->data = -1;
head->next = NULL;
tail = head;
return ( head );
}

List *insertFirst( int d )
{
List *current;

current = (List *) malloc(sizeof(List));

if (current == NULL)
{
prtError("Insufficient Memory!");
return NULL;
}

current->data = d;

if (head->next == NULL)
{
current->next = NULL;
head->next = current;
tail = current;
}
else
{
current->next = head->next;
head->next = current;
}

return current;
}

List *insertLast( int d )
{
List *current;

current = (List *) malloc(sizeof(List));

if (current == NULL)
{
prtError("Insufficient Memory");
return NULL;
}

current->data = d;
current->next = NULL;

if (head->next == NULL)
head->next = current;
else
tail->next = current;

tail = current;

return current;
}

int removeFirst()
{
List *nodeToRemove;
int dataToRemove;

if (head->next == NULL)
{
prtError("Empty list!");
return -1;
}
else
{
nodeToRemove = head->next;
dataToRemove = nodeToRemove->data;
head->next = nodeToRemove->next;
free(nodeToRemove);
return dataToRemove;
}
}


Search the list for an element containing integer k.
If found, return the pointer to that element. Otherwise, return NULL.
If there is more than one element containing k, return the pointer to the first encountered element.

List *search( int k )
{
List *current = head->next;

while (current != NULL && current->data != k)
current = current->next;

if (current != NULL && current->data == k)
{
printf("data is found!%d\n", current->data);
return current;
}

return NULL;
}

main()
{
List *p;
int i;

if ( (p = (List *) init()) == NULL )
exit( 1 );

for ( i = 1; i <= 10; i++ ) {
if ( (p = (List *) insertLast( i*5 )) == NULL )
exit( 1 );
prtList();
}

while ( removeFirst() > 0 )
prtList();

for ( i = 1; i <= 10; i++ ) {
if ( (p = (List *) insertFirst( i*5 )) == NULL )
exit( 1 );
prtList();
}

for ( i = 1; i <= 10; i++ ) {
if ( (p = (List *) search( i*4 )) )
printf( "search %d found %d\n", i*4, p->data );
else
printf( "search %d not found\n", i*4 );
}
}

Answer

In the remove function, you must update tailif the list becomes empty.

int removeFirst()
{
   List *nodeToRemove;
   int dataToRemove;

   if (head->next == NULL)
   {
      prtError("Empty list!");
      return -1;
   }
   else
   {
      nodeToRemove = head->next;
      dataToRemove = nodeToRemove->data;
      head->next = nodeToRemove->next;
      free(nodeToRemove);

      // Update tail if list is empty
      if (head->next == NULL)
      {
          tail = head;
      }

      return dataToRemove;
   }
}
Comments