user2225940
user2225940

Reputation: 83

Creating a singly linked list with 2 structures in C

I am working with these two structures, the first holds the employee information while the second holds the list information:

typedef struct ListNodeTag{
  int idNumber;
  struct ListNodeTag *next;
} Employee;

typedef Employee Item;

typedef struct {
  int size;
  Item *head;
} List;

When the program starts, the list is initialized with this function: The list is declared in my main as List L; and is called like this: Initialize (&L);

void Initialize (List *L) {
  L->size = 0;
  L->head = NULL;
}

From here, I am able to get the list size set to 0 correctly.

I then proceed to add Employee's to the list with these two functions. The first (EmployeeCreation) creates the employee, while the second (Insert) takes it and inserts it into the list. The employee is declared in my main as Employee E; and called like this EmployeeCreation(XXX, &E);

void EmployeeCreation (int idNumber, Employee *E) {   
  E->idNuber = idNumber;
  E->next = NULL;  
}

void Insert (Item X, int position, List *L) {
  int i;
  Item *currentPtr,*previousPtr;
  if(L->head == NULL)
  L->head = &X;
  else{
    currentPtr = L->head;
    for(i=0;i<position;i++){
      previousPtr = currentPtr;
      currentPtr = currentPtr->next;
    }
  previousPtr->next = &X;
  X.next = currentPtr; 
  L->size = L->size + 1;
  }  
}

When run with a test file of 4 ID's. the first is read in correctly and saved to the list. The second, replaces the first, and the length of the list increases by 1 (I understand this part). After this, the program Seg Faults. Where would this error be occurring in my program? Even if the Insert function wants to override a position already occupied I think it should make room just fine and stitch in the new location.

Upvotes: 1

Views: 1650

Answers (3)

Ed Heal
Ed Heal

Reputation: 60037

Here you go:

void Insert (Item X, int position, List *L) {
  int i;
  Item *currentPtr,*previousPtr;
  Item *newX = malloc(sizeof(Item);
  newX->idNunmber - X.idNumber;
  newX->next = NULL;

  if (L->head == NULL) {
      L->head = newX;
  } else {
    currentPtr = L->head;
    previousPtr = NULL;
    for(i=0;currentPtr!= NULL && i<position;i++) {
      previousPtr = currentPtr;
      currentPtr = currentPtr->next;
    }

    newX->next = currentPtr;
    previousPtr->next = newX; 
    L->size +=1;
  }  
}

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41045

You are assigning the address of a local variable (item X) to L->head

L->head = &X;

When you exit from Insert, L->head points to garbage because X is no longer accesible, to fix this you can:

void Insert (Item *X, int position, List *L) {
  ...
  L->head = X;
  ...  
}

Item *temp = malloc(sizeof(Item));

Insert(temp, ..., ...);
...
free(temp);

Upvotes: 2

hrv
hrv

Reputation: 955

In the Insert function, you could simplify this part and check --

else{
  currentPtr = L->head;
  for(i=0;i<position;i++){
    currentPtr = currentPtr->next;
  }
  Item *nextItm = currentPtr.next;
  currentPtr.next = &X;
  X .next = nextItm ; 
  L->size = L->size + 1;
}

Upvotes: 0

Related Questions