C--c language simulation of Java LinkedList

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.