The LinkedList collection of Java is a single-chain set, so just know the head and know the whole behind.
First step: Write your head file well linkedList.h
//This is a header file .structperson{intvalue; structPerson *next;}; typedefstructPerson p;/*AddLast is added from the last place. First judge in the absence, if the head in the words, on the loop to know the last one to find, create an instance, add to the last position at the back*/voidAddLast (p* Head,intval);//send a head in, and you'll know what's behind it.voidPrint (p*head);//Removefirst Remove the first one, first to determine the first in the absence, in the words first removed, and then the back of the cover over,//Reason for pointers with Pointers 1 because if a single * passed parameter is only a value copy, cannot modify the original header, if the pointer can be used to modify the original headervoidRemovefirst (p** head);
Step Two: linkedlist.c
#include <stdio.h>#include<stdlib.h>#include"linkedList.h"voidAddLast (p* Head,intval) { //Judging head if(Head==null)return; P* current =Head; //Loops while(Current->next! =NULL) { Current= current->Next; } //Create an instancep* Newperson =malloc(sizeof(p));//Create instance, specify memory spaceNewperson->value =Val; Newperson->next =NULL; Current->next =Newperson;}voidPrint (p*head) { if(Head==null)return; P* current =Head; while(Current! =NULL) {printf ("%d\n",current->value); Current= current->Next; }}voidRemovefirst (p**head) { if(*head==null)return; P*Current ; if((*head)->next! =NULL) { Current= (*head)Next; } Free((*head)); *head =Current ;}intMain () {p* head =malloc(sizeof(p)); Head->value =1; Head->next =NULL; P* last =malloc(sizeof(p)); AddLast (Head,2); printf ("addlast......\n"); Print (head);}
C--c language simulates Java's linkedlist