0

Did I do this right? I'm getting a strange error here...

linked.c: In function ‘push’:
linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default]
linked.c: In function ‘main’:
linked.c:146:9: error: incompatible type for argument 1 of ‘push’
linked.c:45:6: note: expected ‘struct node **’ but argument is of type ‘struct node’
~/swen250/CLinkedList$ gcc -o linked linked.c
linked.c: In function ‘push’:
linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default]
~/swen250/CLinkedList$ gcc -o linked linked.c
linked.c: In function ‘pop’:
linked.c:63:19: error: request for member ‘data’ in something not a structure or union
linked.c:64:20: error: request for member ‘next’ in something not a structure or union
linked.c: In function ‘copyList’:
linked.c:106:9: warning: passing argument 1 of ‘appendNode’ from incompatible pointer type [enabled by default]
linked.c:75:6: note: expected ‘struct node **’ but argument is of type ‘struct node *’
#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node* next;
};

static int length(struct node** head);
static void push(struct node** head, int data);
static int pop(struct node** head);
static void appendNode(struct node** head, int data);
static struct node *copyList(struct node** head);
static void printList(struct node** head);



/************************************************************
 length - return length of a list
 ************************************************************/
int length(struct node** head) {
    int count = 0;
    struct node* current = NULL;

    current = *head;
    while (current != NULL) {
        current = current->next;
        ++count;
    }

    return count;
}


/************************************************************
 push - add new node at beginning of list
 ************************************************************/
void push(struct node** head, int data) {
    struct node* new_ptr = NULL;

    new_ptr = (struct node*)malloc(sizeof(struct node));
    new_ptr->data = data;
    new_ptr->next = *head;

    *head = new_ptr;
}

/************************************************************
 pop - delete node at beginning of non-empty list and return its data
 ************************************************************/
int pop(struct node** head) {
    int val = 0;
    struct node* temp = NULL;

    if (*head != NULL) {
        val = head->data;
        temp = head->next;
        free(head);
        *head = temp;
    }

    return(val);
}

/************************************************************
 appendNode - add new node at end of list
 ************************************************************/
void appendNode(struct node** head, int data) {
    struct node* current = NULL;
    struct node* previous = NULL;
    struct node* new_ptr = NULL;

    current = *head;
    previous = current;
    while (current != NULL) {
        previous = current;
        current = current->next;
    }

    new_ptr = (struct node*)malloc(sizeof(struct node));
    new_ptr->data = data;
    new_ptr->next = NULL;

    previous = new_ptr;

}

/************************************************************
 copyList - return new copy of list
 ************************************************************/
struct node* copyList(struct node** head) {
    struct node* copy = NULL;
    struct node* current = NULL;
    struct node* new_ptr = NULL;

    /* Copy current head to copy */
    current = *head;
    while (current != NULL) {
        appendNode(copy, current->data);
        current = current->next;
    }

    return copy;
}


/************************************************************
 printList - print linked list as "List: < 2, 5, 6 >" (example)
 ************************************************************/
void printList(struct node** head) {
    struct node* current = NULL;

    printf("List: < ");

    current = *head;
    if (current == NULL)
        printf("none ");

    while (current != NULL) {
        printf("%d", current->data);
        current = current->next;
        if (current != NULL)
            printf(", ");
    }

    printf(" >\n");
}

void main() {
    int i;                      // index used for loops
    struct node *list_a;        // a new list
    struct node *list_a_copy;   // copy of list
    list_a = NULL;                // initialize empty list
    list_a_copy = NULL;           // initialize empy list


    // test push
    for (i = 0; i < 4; ++i)
        push(&list_a, i);

    // test length
    printf("Length of list = %d\n", length(&list_a));

    // test print head list
    printf("head:\n");
    printList(&list_a);

    // test append node
    for (i = 4; i < 8; ++i)
        appendNode(&list_a, i);

    // test print head list
    printf("head(append):\n");
    printList(&list_a);

    // make a copy of list
    list_a_copy = copyList(&list_a);

    // test pop head list
    for (i = 0; i < 4; ++i)
        printf("%d popped\n", pop(&list_a));

    // test print copy list
    printf("head copy:\n");
    printList(&list_a_copy);

    // test pop copy list
    for (i = 0; i < 4; ++i)
        printf("%d popped\n", pop(&list_a_copy));

}
1
  • One of the issues that I see is, your using a double pointer in the context of a single pointer. Commented Nov 25, 2013 at 3:36

3 Answers 3

2

The issue is with the way how double pointers are used.

Here is the full working code: I have made some changes on the way how double pointers were used. You can see the changes in pop function and copyList function.

#include <stdio.h>
#include <stdlib.h>

struct node {
  int data;
  struct node* next;
};

static int length(struct node** head);
static void push(struct node** head, int data);
static int pop(struct node** head);
static void appendNode(struct node** head, int data);
static struct node *copyList(struct node** head);
static void printList(struct node** head);



/************************************************************
 length - return length of a list
************************************************************/
int length(struct node** head) {
  int count = 0;
  struct node* current = NULL;

  current = *head;
  while (current != NULL) {
    current = current->next;
    ++count;
  }

  return count;
}


/************************************************************
 push - add new node at beginning of list
************************************************************/
void push(struct node** head, int data) {
  struct node* new_ptr = NULL;

  new_ptr = (struct node*)malloc(sizeof(struct node));
  new_ptr->data = data;
  new_ptr->next = *head;

  *head = new_ptr;
}

/************************************************************
 pop - delete node at beginning of non-empty list and return its data
************************************************************/
int pop(struct node** head) {
  int val = 0;
  struct node* temp = NULL;

  if (*head != NULL) {
    val = (*head)->data;
    temp = (*head)->next;
    free(*head);
    *head = temp;
  }

  return(val);
}

/************************************************************
 appendNode - add new node at end of list
************************************************************/
void appendNode(struct node** head, int data) {
  struct node* current = NULL;
  struct node* previous = NULL;
  struct node* new_ptr = NULL;

  current = *head;
  previous = current;
  while (current != NULL) {
    previous = current;
    current = current->next;
  }

  new_ptr = (struct node*)malloc(sizeof(struct node));
  new_ptr->data = data;
  new_ptr->next = NULL;

  previous = new_ptr;

}

/************************************************************
 copyList - return new copy of list
************************************************************/
struct node* copyList(struct node** head) {
  struct node* copy = NULL;
  struct node* current = NULL;
  struct node* new_ptr = NULL;

  /* Copy current head to copy */
  current = *head;
  while (current != NULL) {
    appendNode(&copy, current->data);
    current = current->next;
  }

  return copy;
}


/************************************************************
 printList - print linked list as "List: < 2, 5, 6 >" (example)
************************************************************/
void printList(struct node** head) {
  struct node* current = NULL;

  printf("List: < ");

  current = *head;
  if (current == NULL)
    printf("none ");

  while (current != NULL) {
    printf("%d", current->data);
    current = current->next;
    if (current != NULL)
      printf(", ");
  }

  printf(" >\n");
}

void main() {
  int i;                      // index used for loops
  struct node *list_a;        // a new list
  struct node *list_a_copy;   // copy of list
  list_a = NULL;                // initialize empty list
  list_a_copy = NULL;           // initialize empy list


  // test push
  for (i = 0; i < 4; ++i)
    push(&list_a, i);

  // test length
  printf("Length of list = %d\n", length(&list_a));

  // test print head list
  printf("head:\n");
  printList(&list_a);

  // test append node
  for (i = 4; i < 8; ++i)
    appendNode(&list_a, i);

  // test print head list
  printf("head(append):\n");
  printList(&list_a);

  // make a copy of list
  list_a_copy = copyList(&list_a);

  // test pop head list
  for (i = 0; i < 4; ++i)
    printf("%d popped\n", pop(&list_a));

  // test print copy list
  printf("head copy:\n");
  printList(&list_a_copy);

  // test pop copy list
  for (i = 0; i < 4; ++i)
    printf("%d popped\n", pop(&list_a_copy));
}
Sign up to request clarification or add additional context in comments.

Comments

0

From the error log, I notice that you have incompatible types multiples time. It would help if you post your main.cpp as well.

p.s Like Mike G said, double pointer is really excessive. You can implement a linked list using a single pointer.

Comments

0

Problem is in your pop() function...take a look. you are assigning

val=head->data;
temp = head->next;

also you are freeing the head and then reassigning.

you should do like this

val=(*head)->data;
temp=(*head)->next;

and while freeing memory...free the temp and reassign head to its next.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.