Multiple Queues in C - c

I have a basic queue design, but I want to have multiple queues. The way it looks right now is that I would need another queue.h file and replace head and tail with different names, but I am sure there is a better way?
queue.h *Edited
#include<stdlib.h> // malloc
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *head, *tail;
};
struct Queue *QueueInit() {
//allocate and initialize a queue
struct Queue *thisQueue = malloc(sizeof *thisQueue);
thisQueue->head = NULL;
thisQueue->tail = NULL;
return thisQueue;
}
void push(struct Queue *myQueue, int x) {
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
if(myQueue->head == NULL && myQueue->tail == NULL) { //empty
myQueue->head = myQueue->tail = temp;
return;
}
myQueue->tail->next = temp;
myQueue->tail = temp;
}
void pop(struct Queue *myQueue) {
struct Node* temp = myQueue->head;
if(myQueue->head == NULL) return; //empty
if(myQueue->head == myQueue->tail) {
myQueue->head = myQueue->tail = NULL;
}
else {
myQueue->head = myQueue->head->next;
}
free(temp);
}
How can I create multiple queues like this?
main.c
int main() {
struct Node iceCreamLine;
struct Node bathroomLine;
iceCreamLine.push(13);
bathroomLine.push(2);
//It looks like I will have to use this syntax then instead?
struct Queue *droneQueue; //(THIS IS LINE 5)
push(&droneQueue,1666);
push(&droneQueue,100);
printf("--> %d",&droneQueue->head->data);
printf("--> %d",&droneQueue->head->next->data);
}
The first printf works, but the second one gives me a segmentation dump. Also here are the warnings
main.c: In function ‘main’:
main.c:6:2: warning: passing argument 1 of ‘push’ from incompatible pointer type [enabled by default]
In file included from queue.c:2:0:
queue.h:21:6: note: expected ‘struct Queue *’ but argument is of type ‘struct Queue **’
main.c:7:2: warning: passing argument 1 of ‘push’ from incompatible pointer type [enabled by default]
In file included from queue.c:2:0:
queue.h:21:6: note: expected ‘struct Queue *’ but argument is of type ‘struct Queue **’
main.c:9:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
main.c:10:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]

struct Queue {
struct Node *head, *tail;
};
Add a QueueInit function to allocate and initialize a queue, returning a pointer to a struct Queue. Pass a pointer to struct Queue to push and pop, and get rid of your global head and tail.

Related

I am not able to print my polynomial expression using linked list

void create(struct node *head); //declaring functions
void display(struct node *head);
struct node //creating a struct datatype for nodes
{
int coeff;
int power;
struct node* next;
};
struct node* poly1=NULL; //head pointer for a sample polynomial
void main()
{
int coeff,power,deg,i;
clrscr(); //main function
printf("\n enter polynomial 1 ");
create(poly1);
display(poly1);
getch();
}
void create(struct node *head)
{
struct node *newnode,*temp;
int exp,num,n,i;
printf("\n enter no. of terms in your expression=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=(struct node*)malloc(sizeof(newnode));
newnode->next=NULL;
printf("\n enter power=");
scanf("%d",&exp);
newnode->power=exp;
printf("\n enter coefficient=");
scanf("%d",&num);
newnode->coeff=num;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
printf("%dx^%d",temp->coeff,temp->power);
temp=temp->next;
}
}
My code compilation shows no errors or warnings but I am not able to print my polynomial function. The display function is printing just 0 x^ 0 or no values at all, I have tried many things but it is not working, can someone point out what should I do to correct it. I am using C language.
My code compilation shows no errors or warnings
That's very strange. After adding missing includes and commenting out getch and clrscr this is the compiler output and it's without even enabling the (almost mandatory) flags -Wall -Wextra:
$ gcc main.c
main.c:4:20: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
4 | void create(struct node *head); //declaring functions
| ^~~~
main.c:5:21: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
5 | void display(struct node *head);
| ^~~~
main.c: In function ‘main’:
main.c:19:11: warning: passing argument 1 of ‘create’ from incompatible pointer type [-Wincompatible-pointer-types]
19 | create(poly1);
| ^~~~~
| |
| struct node *
main.c:4:26: note: expected ‘struct node *’ but argument is of type ‘struct node *’
4 | void create(struct node *head); //declaring functions
| ~~~~~~~~~~~~~^~~~
main.c:20:12: warning: passing argument 1 of ‘display’ from incompatible pointer type [-Wincompatible-pointer-types]
20 | display(poly1);
| ^~~~~
| |
| struct node *
main.c:5:27: note: expected ‘struct node *’ but argument is of type ‘struct node *’
5 | void display(struct node *head);
| ~~~~~~~~~~~~~^~~~
main.c: At top level:
main.c:24:8: error: conflicting types for ‘create’
24 | void create(struct node *head)
| ^~~~~~
main.c:4:6: note: previous declaration of ‘create’ was here
4 | void create(struct node *head); //declaring functions
| ^~~~~~
main.c:53:7: error: conflicting types for ‘display’
53 | void display(struct node *head)
| ^~~~~~~
main.c:5:6: note: previous declaration of ‘display’ was here
5 | void display(struct node *head);
Your compiler seems to be seriously outdated.
There seems to be many issues with your code, but one obvious is this:
newnode=(struct node*)malloc(sizeof(newnode));
The casting is bad practice, but should not cause any actual problems, but the argument to sizeof is wrong. It should be:
newnode=malloc(sizeof(*newnode));
Here you pass the pointer by value
create(poly1);
to your function
void create(struct node *head)
then you set head
head=newnode;
but are only modifying the local variable head, while you want to modify poly1.
One possibility is to pass a double pointer like this:
create(&poly1);
...
void create(struct node **head)
...
if (*head == NULL)
*head = newnode;
Now you really modify the global variable poly1, which is what you want.

I have no error showing up in my code but when my program reaches the "chooseCard" function it terminates

I need to let the player choose what cards they want to keep from their hand of 5 cards. I am trying to do this in my chooseCards function but when the user chooses to replace a card it terminates with no error.
I have tried changing the types of pointers in the function hoping that was the issue but nothing changed. When the user enters "n" (to replace a card) the program quits. However, the passCard function works flawlessly earlier in the program along with the shuffle function, so I am confused on where the issue is. I know this would be far easier using arrays but the assignment requires linked lists.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct card_s {
char suit[20];
int face;
struct card_s *next;
}card;
void makeDeck(card** currentCard) {
int i, j;
char cardsuit[20];
card *tempCard = '-', *tail = NULL, *lastcard = NULL;
for (i = 0; i < 13; i += 1) { // 13 cards per suit
for (j = 0; j < 4; j += 1) { // 4 suits
tempCard = (card*)malloc(sizeof(card));
if (j == 0) {
strcpy(cardsuit, "of diamonds");
}
else if (j == 1) {
strcpy(cardsuit, "of hearts");
}
else if (j == 2) {
strcpy(cardsuit, "of clubs");
}
else if (j == 3) {
strcpy(cardsuit, "of spades");
}
tempCard->face = i + 1;
strcpy(tempCard->suit, cardsuit);
tempCard->next = NULL;
lastcard = tempCard;
if (*currentCard == NULL) {
*currentCard = tempCard;
}
else {
tail->next = tempCard;
}
tail = tempCard;
tail->next = NULL; //sets final card place in list to null
}
}
return;
}
int FindLength(card* currentcard) {
int i = 0;
while (currentcard != NULL) {
i += 1; //increment i evry time current card has value
currentcard = currentcard->next;
}
return i;
}
void shuffleDeck(card** currentCard, int deckLength) {
int cardcount, place, i, rng;
int j = 0;
card *shuffled = NULL;
card *unshuffled = NULL;
card *tempCard = NULL;
srand(time(NULL));
for (place = 0; place < deckLength; place += 1) {
shuffled = *currentCard;
unshuffled = *currentCard;
tempCard = (card*)malloc(sizeof(card));
rng = rand() % deckLength;
for (cardcount = 0; cardcount < rng; cardcount += 1) {
unshuffled = unshuffled->next;
}
for (cardcount = 0; cardcount < place; cardcount += 1) {
shuffled = shuffled->next;
}
strcpy(tempCard->suit, unshuffled->suit); //swap the suits of each card
strcpy(unshuffled->suit, shuffled->suit);
strcpy(shuffled->suit, tempCard->suit);
tempCard->face = unshuffled->face; //swap value of the cards
unshuffled->face = shuffled->face;
shuffled->face = tempCard->face;
}
return;
}
void pushFront(card** head, char* suit, int face) {
card* temp = (card*)malloc(sizeof(card));
temp->face = face;
strcpy(temp->suit, suit);
temp->next = *head;
*head = temp; //new card put at head of list
}
void removeFront(card **head) {
card* temp = NULL;
temp = *head; //point temp to first card
*head = (*head)->next; //set head to the next card
free(temp); //free the first
}
void passCard(card** giver, card** taker) {
pushFront(taker, (*giver)->suit, (*giver)->face);
removeFront(giver);
return;
}
void dealCards(card** deck, card** p1, card** p2) {
card* current = NULL;
current = *deck;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
passCard(deck, p1);
}
else {
passCard(deck, p2);
}
}
return;
}
void chooseCards(card*hand, card*deck) {
int i, chosenCard = 0;
char answer;
for (i = 1; i < 6; i += 1) {
printf("Keep card %d? (y or n): ", i);
scanf(" %c", &answer);
if (answer == 'n') {
passCard(hand, deck);
shuffleDeck(deck, FindLength(deck));
passCard(deck, hand);
}
hand = hand->next;
}
return;
}
int main() {
card *cards = NULL; //deck of cards
card *player1 = NULL; //p1 hand
card *dealer = NULL; //dealer hand
makeDeck(&cards); //create deck of 52 cards
int deckLength = Findlength(cards);
shuffleDeck(&cards, deckLength); //shuffle deck of cards
dealCards(&cards, &player1, &dealer); //deal to player 1 and dealer
//print player 1 hand
chooseCards(&player1, &cards);
//print player 1 hand, chosen cards have been replaced by cards in the deck
return 0;
}
The output is supposed to replace the chosen card with a card from the deck.
When I compile your code I get:
joshua#nova:/tmpϟ gcc testshuffle.c
testshuffle.c: In function ‘makeDeck’:
testshuffle.c:18:22: warning: initialization of ‘card *’ {aka ‘struct card_s *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
card *tempCard = '-', *tail = NULL, *lastcard = NULL;
^~~
testshuffle.c: In function ‘chooseCards’:
testshuffle.c:182:22: warning: passing argument 1 of ‘passCard’ from incompatible pointer type [-Wincompatible-pointer-types]
passCard(hand, deck);
^~~~
testshuffle.c:143:22: note: expected ‘card **’ {aka ‘struct card_s **’} but argument is of type ‘card *’ {aka ‘struct card_s *’}
void passCard(card** giver, card** taker) {
~~~~~~~^~~~~
testshuffle.c:182:28: warning: passing argument 2 of ‘passCard’ from incompatible pointer type [-Wincompatible-pointer-types]
passCard(hand, deck);
^~~~
testshuffle.c:143:36: note: expected ‘card **’ {aka ‘struct card_s **’} but argument is of type ‘card *’ {aka ‘struct card_s *’}
void passCard(card** giver, card** taker) {
~~~~~~~^~~~~
testshuffle.c:183:25: warning: passing argument 1 of ‘shuffleDeck’ from incompatible pointer type [-Wincompatible-pointer-types]
shuffleDeck(deck, FindLength(deck));
^~~~
testshuffle.c:77:25: note: expected ‘card **’ {aka ‘struct card_s **’} but argument is of type ‘card *’ {aka ‘struct card_s *’}
void shuffleDeck(card** currentCard, int deckLength) {
~~~~~~~^~~~~~~~~~~
testshuffle.c:184:22: warning: passing argument 1 of ‘passCard’ from incompatible pointer type [-Wincompatible-pointer-types]
passCard(deck, hand);
^~~~
testshuffle.c:143:22: note: expected ‘card **’ {aka ‘struct card_s **’} but argument is of type ‘card *’ {aka ‘struct card_s *’}
void passCard(card** giver, card** taker) {
~~~~~~~^~~~~
testshuffle.c:184:28: warning: passing argument 2 of ‘passCard’ from incompatible pointer type [-Wincompatible-pointer-types]
passCard(deck, hand);
^~~~
testshuffle.c:143:36: note: expected ‘card **’ {aka ‘struct card_s **’} but argument is of type ‘card *’ {aka ‘struct card_s *’}
void passCard(card** giver, card** taker) {
~~~~~~~^~~~~
testshuffle.c: In function ‘main’:
testshuffle.c:207:17: warning: passing argument 1 of ‘chooseCards’ from incompatible pointer type [-Wincompatible-pointer-types]
chooseCards(&player1, &cards);
^~~~~~~~
testshuffle.c:170:23: note: expected ‘card *’ {aka ‘struct card_s *’} but argument is of type ‘card **’ {aka ‘struct card_s **’}
void chooseCards(card*hand, card*deck) {
~~~~~^~~~
testshuffle.c:207:27: warning: passing argument 2 of ‘chooseCards’ from incompatible pointer type [-Wincompatible-pointer-types]
chooseCards(&player1, &cards);
^~~~~~
testshuffle.c:170:34: note: expected ‘card *’ {aka ‘struct card_s *’} but argument is of type ‘card **’ {aka ‘struct card_s **’}
void chooseCards(card*hand, card*deck) {
~~~~~^~~~
When I run it I get:
Program received signal SIGSEGV, Segmentation fault.
0x000055555555530d in FindLength ()
(gdb) bt
#0 0x000055555555530d in FindLength ()
#1 0x0000555555555618 in chooseCards ()
#2 0x00005555555556ce in main ()
(gdb)
Fix all your warnings. You will find your problem therein. You have bad type conversions between card* and card** that's causing FindLength to crash. In particular, FindLength wants a card* but got passed a card** in a variable declared to contain card* from chooseCards() and proceed to interpret the middle of the suit string as the next pointer and crash accessing unallocated memory.

Error while printing a string in a reverse order using stack

Hello friends this is my first post, I may have done something wrong while posting, sorry for that.
I am trying to print the String in a reverse order using stack. The point where I am trying to get the top value into the character array I am getting error at that part, please help.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct StackNode
{
char data;
struct StackNode* next;
};
struct StackNode* createNode(char data){
struct StackNode* stack=(struct StackNode*)malloc(sizeof(struct StackNode));
stack->data=data;
stack->next=NULL;
return stack;
}
void push(struct StackNode** root,char data)
{
struct StackNode* stack=createNode(data);
stack->next=*root;
*root=stack;
}
char top(struct StackNode** root)
{
return (*root)->data;
}
void pop(struct StackNode** root, char c[],int k)
{
int i;
for(i=0;i<=k;i++)
{
c[i]=top(&root);
*root=(*root)->next;
}
}
void print(struct StackNode* root)
{
while(root!=NULL)
{
printf("%c",root->data);
root=root->next;
printf(" ");
}
printf("\n");
}
int main()
{
struct StackNode* root=NULL;
char c[]="Sherry";
int k=strlen(c);
int i;
for(i=0;i<=k;i++)
{
push(&root,c[i]);
}
pop(&root,c,k);
for(i=0;i<=k;i++)
{
printf("%c",c[i]);
printf(" ");
}
return 0;
}
stackreviseLinkedList.c: In function ‘pop’:
stackreviseLinkedList.c:36:1: warning: passing argument 1 of ‘top’ from incompatible pointer type [enabled by default]
c[i]=top(&root);
^
stackreviseLinkedList.c:26:6: note: expected ‘struct StackNode **’ but argument is of type ‘struct StackNode ***’
char top(struct StackNode** root)
The compiler is clearly pointing out the error here (Disclaimer: Apart from compiler error there could be other issues, I didn't try running the program!)
c[i]=top(&root); is the culprit
should be
c[i]=top(root); // no & required
Look at line# 36
stackreviseLinkedList.c: In function ‘pop’:
stackreviseLinkedList.c:36:1: warning: passing argument 1 of ‘top’ from incompatible pointer type [enabled by default]
c[i]=top(&root);
^
stackreviseLinkedList.c:26:6: note: expected ‘struct StackNode **’ but argument is of type ‘struct StackNode ***’
char top(struct StackNode** root)
The main issue that fails you is:
the line:
c[i]=top(&root);
should be:
c[i]=top(root);
Other issues that don't hurt you right now but are just bad programming:
pop() should handle only one character, should be symmetric to push
you have a memory leak. You should put the StackNode in a temporary pointer, change the root pointer, then release the StackNode
as I commented, indentation. It help to spot coding issues.

C compilation warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

/*Implementation of Binary Tree*/
#include <stdio.h>
#include <ncurses.h>
#include <malloc.h>
#include <stdlib.h>
struct bin_tree
{
int INFO;
struct node *LEFT, *RIGHT;
};
typedef struct bin_tree node;
node *insert(node *,int); /*Function prototype for insering a new node*/
void display(node *); /*Function prototype fpr displaying the tree nodes*/
int count=1; /*counter for ascertaining left or right position for the new node*/
int main()
{
struct node *root = NULL;
int element, choice;
clear();
/*Displaying a menu of choices*/
while(1)
{
clear();
printf("\n Select an option\n");
printf("\n1 - Insert");
printf("\n2 - Display");
printf("\n3 - Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n\n Enter the node value: ");
scanf("%d", &element);
root = insert(root, element); /*calling the insert function for inserting a new element into the tree*/
getch();
break;
}
case 2:
{
display(root); /*calling the display function for printing the node values*/
getch();
break;
}
case 3:
{
exit(1);
break;
}
default:
{
printf("\n incorrect choice.Please try again.");
getch();
break;
}
}
}
}
node *insert(node *r, int n)
{
if(r==NULL)
{
r=(node*)malloc(sizeof(node));
r->LEFT = r->RIGHT = NULL;
r->INFO = n;
count = count+1;
}
else
{
if(count%2==0)
r->LEFT = insert(r->LEFT,n);
else
r->RIGHT = insert(r->RIGHT,n);
}
return(r);
}
void display(node *r)
{
if(r->LEFT!=NULL)
display(r->LEFT);
printf("%d\n",r->INFO);
if(r->RIGHT!=NULL)
display(r->RIGHT);
}
gcc -Wall -c "BinaryTree.c" (in directory: /home/dere/IGNOUPROGRAMS/C)
BinaryTree.c: In function ‘main’:
BinaryTree.c:46:5: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:16:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:46:10: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:53:5: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:17:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c: In function ‘insert’:
BinaryTree.c:86:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:86:11: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:88:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:88:12: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c: In function ‘display’:
BinaryTree.c:96:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:99:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
Compilation finished successfully.
Would appreciate any help on this error.
Declare root as type of node * itself in main() function. No need to declare as struct node *.
node *root = NULL;
As root is typedef for struct bin_tree.
Warning is due to the declaration mismatch. As insert() function is defined as node *insert(node *,int); but root variable is defined as struct node * and its passed as argument to insert() function.
seems your code is wrong,probably try like this.you need to pass the adress of root to insert function pointer like below:
root = insert(&root, element);
node *insert(node **r, int n)
{
struct node* temp = NULL;
struct node *q = NULL;
q = *r;
if(q==NULL)
{
temp=(node*)malloc(sizeof(node));
temp->LEFT = temp->RIGHT = NULL;
temp->INFO = n;
count = count+1;
}
else
{
if(count%2==0)
temp->LEFT = insert(temp->LEFT,n);
else
temp->RIGHT = insert(temp->RIGHT,n);
}
return(temp);
}

C implementation of skew heap

I'm trying to implement a skew heap in C, but my code doesn't compile. I'm not that experienced in C and never created any type of heap in C. That is why I don't know how to fix it, I'm hoping someone can point me the right direction. I have been reading articles about the skew heap and this is what I got so far using the algorithms I have found online. Thanks in Advance.
typedef struct node
{
int value;
struct node * root;
struct node * leftchild;
struct node * rightchild;
} Node;
struct skewHeap
{
struct node * root;
};
void skewHeapInit (struct skewHeap * sk)
{
sk->root = 0;
}
void skewHeapAdd (struct skewHeap *sk)
{
struct node *n = (struct node *) malloc(sizeof(struct node));
assert(n != 0);
n->value = 0;
n->leftchild = 0;
n->rightchild = 0;
line 185. s->root = skewHeapMerge(s->root, n);
}
void skewHeapRemoveFirst (struct skewHeap *sk)
{
struct node * n = sk->root;
free(n);
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
}
line 196. struct node * skewHeapMerge(struct node *left, struct node *right)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (left == NULL)
return *right;
if (right == NULL)
return *left;
if (left->value < right-> value)
{
temp = left->leftchild;
left->leftchild = skewHeapMerge(left->rightchild, right);
left->rightchild = temp;
return left;
}
else
{
temp = right->rightchild;
right->rightchild = skewHeapMerge(right->leftchild, left);
right->leftchild = temp;
return right;
}
}
These are the compilations errors I'm getting at the moment:
program.c: In function ‘skewHeapAdd’:
program.c:185: warning: implicit declaration of function ‘skewHeapMerge’
program.c:185: warning: assignment makes pointer from integer without a cast
program.c: In function ‘skewHeapRemoveFirst’:
program.c:191: warning: assignment makes pointer from integer without a cast
program.c: At top level:
program.c:196: error: conflicting types for ‘skewHeapMerge’
program.c:185: note: previous implicit declaration of ‘skewHeapMerge’ was here
program.c: In function ‘skewHeapMerge’:
program.c:202: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
program.c:205: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
Regarding the compiler errors,
program.c: In function ‘skewHeapAdd’:
program.c:185: warning: implicit declaration of function ‘skewHeapMerge’
program.c:185: warning: assignment makes pointer from integer without a cast
tells you that no prototype of skewHeapMerge is in scope where skewHeapAdd is defined, hence (the compiler apparently operates in C89 mode, but thankfully warns about it), the compiler supposes an implicit declaration with return type int for skewHeapMerge.
Add a header file with prototypes for all your functions, and #include that in all *.c files where these functions are used or defined, so that the compiler knows the types of the functions.
program.c: In function ‘skewHeapRemoveFirst’:
program.c:191: warning: assignment makes pointer from integer without a cast
that should be the line
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
where sk->root is a struct node*, but due to the implicit declaration of skewHeapMerge, that is assumed to return an int.
program.c: At top level:
program.c:196: error: conflicting types for ‘skewHeapMerge’
program.c:185: note: previous implicit declaration of ‘skewHeapMerge’ was here
here the compiler finds that the definition of skewHeapMerge gives a type conflicting with the one from the implicit declaration.
program.c: In function ‘skewHeapMerge’:
program.c:202: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
program.c:205: error: incompatible types when returning type ‘struct node’ but ‘struct node *’ was expected
That is for the lines
if (left == NULL)
return *right;
if (right == NULL)
return *left;
where you ought to return right resp. left instead of *right resp. *left (I overlooked that at first).
You have a mistake in skewHeapRemoveFirst
void skewHeapRemoveFirst (struct skewHeap *sk)
{
struct node * n = sk->root;
free(n);
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
}
where you use n after you freed it. You have to exchange the last two lines in that function.
And in skewHeapMerge
struct node * skewHeapMerge(struct node *left, struct node *right)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (left == NULL)
return *right;
if (right == NULL)
return *left;
you are leaking memory. Remove the allocation, since if temp is used at all, you assign either left->leftchild or right->rightchild to it.

Resources