hi guys... i am having some trouble implementing an insert function for a linked list...
this is my code so far..
Code:
//header file
typedef struct{
char *comName;
int comValue;
}fcommand;
typedef struct{
fcommand* command;
struct node *next;
}node;
Code:
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
//insertNode Function
void insertNode(void){
node *newNode = (node*)malloc(sizeof(node));
char* string = "Hello";
//char* name;
//initialize node
newNode->next = NULL;
//instantiate command
newNode->command = (fcommand*)malloc(sizeof(fcommand));
if (newNode->command == NULL){
free(newNode);
return;
}
//initialize command (TESTING)
newNode->command->comName = string;
//check for empty list
if (head == NULL){
head = newNode;
}
//check if the command belongs at the lists head.
else if (head->next == NULL){
newNode->next = head->next;
head->next = newNode; //assignment from incompatible pointer type
printf("added at the beginning.. \n");
}
//loop through the list and add the command at the end of the list
else{
node *current =head;
while(current->next != NULL){
current = current->next; //assignment from incompatible pointer type
}
current->next = newNode; //assignment from incompatible pointer type
printf("Added at the end.!\n");
}
}
i know it is not the best code.. butt your help will be appreciated.. !
thanks guys.