EnQueued
EnQueued

Reputation: 1

How to solve incompatible types in assignment using node pointer to set up a Linked List

Okay so this is what I have so far...

typedef struct node{
int *next;
int val;
}node;
void pqPrint(){
node *current=front;
printf("Queue Contains:");
while(current->next!=NULL){
printf(" %d ", current->val);
node temp;
temp.next=current->next;
current->next=temp;
}
printf("\n");
}

I keep getting the error stated above with current->next=temp;

Upvotes: 0

Views: 1018

Answers (2)

Josip Dorvak
Josip Dorvak

Reputation: 131

First off...your next should not be an int pointer, most compilers will warn you about that. It should be a struct node*. Second, your trying to assign a complete node object TO A POINTER. They are 2 completely different data types. Make your temp a Node * and it should work

Upvotes: 0

user4129403
user4129403

Reputation:

I didn't find any error in your description, but anyway, there are a few things to do here. I wrote a complete program based on your listing above:

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

typedef struct node{
    struct node *next;
    int val;
}node;

node *front;

void pqPrint() {
    node *current=front;
    printf("Queue Contains:");
    while(current != NULL){
        printf(" %d ", current->val);
        current = current->next;
    }
    printf("\n");
}

void    main(void)
{
    front = malloc(sizeof(front));
    front->val = 10;
    front->next = NULL;
    pqPrint();
}

A few comments: 1. The next element in your struct shouldn't be of type int, most compilers would complain. 2. You need to declare a list start as front, and initialize it. 3. You need to allocate memory for each element in the list, typically by using malloc(), but note that my example doesn't validate the result (in real life, you must check against NULL return from malloc()). 4. If you have a pointer to the left, you need to use -> (not .) to refer to separate elements in the struct which the pointer points to. 5. I guess your temp variable just confused you so I removed it. Note that in your example, you would always miss the first element.

Upvotes: 1

Related Questions