1) Create the linked list node:
struct node{
int data;
struct node *next;
}
2) To do anything else you have to create a new node. In most languages you do this with new, but C does not know what new is. Instead:
node *newNode=malloc(sizeof(node));
NOTE: IT'S VERY VERY IMPORTANT THAT YOU USE THE NODE AS A POINTER
3) To access the actual data in the node:
newNode->data;
NOTE: NOTICE THE '->' YOU USE IT BECAUSE YOU HAVE A POINTER
4) To use in a loop:
while(newNode){
printf("%d",newNode->data);
newNode=newNode->next;
}
5) To insert into list:
void insertState(struct node **current,State a)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->data = a;
newNode->tail = *current; // The '*' to dereferences back to the real head
*current = newNode;
}
6) To go through list to print:
void generate(struct node **list)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=*list;
while(temp!=NULL)
{
printState(temp->data);
temp=temp->tail;
}
free(temp);
}
7) To delete from linked list:
void removeFromList(struct node *value, struct node **list)
{
struct node *currP,*prevP,*temp;
currP=(struct node*)malloc(sizeof(struct node));
prevP=(struct node*)malloc(sizeof(struct node));
temp=(struct node*)malloc(sizeof(struct node));
prevP = NULL;
currP=*list;
if(currP->tail==NULL)
{
*list=NULL;
}
else{
while(*list!=value)
{
prevP=*list;
currP=prevP->tail;
*list=currP;
}
}
}
struct node{
int data;
struct node *next;
}
2) To do anything else you have to create a new node. In most languages you do this with new, but C does not know what new is. Instead:
node *newNode=malloc(sizeof(node));
NOTE: IT'S VERY VERY IMPORTANT THAT YOU USE THE NODE AS A POINTER
3) To access the actual data in the node:
newNode->data;
NOTE: NOTICE THE '->' YOU USE IT BECAUSE YOU HAVE A POINTER
4) To use in a loop:
while(newNode){
printf("%d",newNode->data);
newNode=newNode->next;
}
5) To insert into list:
void insertState(struct node **current,State a)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->data = a;
newNode->tail = *current; // The '*' to dereferences back to the real head
*current = newNode;
}
6) To go through list to print:
void generate(struct node **list)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=*list;
while(temp!=NULL)
{
printState(temp->data);
temp=temp->tail;
}
free(temp);
}
7) To delete from linked list:
void removeFromList(struct node *value, struct node **list)
{
struct node *currP,*prevP,*temp;
currP=(struct node*)malloc(sizeof(struct node));
prevP=(struct node*)malloc(sizeof(struct node));
temp=(struct node*)malloc(sizeof(struct node));
prevP = NULL;
currP=*list;
if(currP->tail==NULL)
{
*list=NULL;
}
else{
while(*list!=value)
{
prevP=*list;
currP=prevP->tail;
*list=currP;
}
}
}