Linked list of data structures (common operations for single-linked lists)

Source: Internet
Author: User

Common operations for single-linked lists:

    1. Definition of a linked list
    2. The initialization of a linked list
    3. Creating a linked List
    4. Measuring meter length
    5. Linked list insertion
    6. Linked list Delete
    7. Print linked list
    8. Reverse linked List
//definition of a linked liststructlinklist {intvalue; Linklist*next;};//the initialization of a linked listvoidInitlist (linklist *L) {L=nullptr;}//creation of linked lists (head insertion method)linklist*createlist () {linklist* head =Newlinklist; Head->next =nullptr; if(Head = =nullptr) {Std::cout<<"failed to allocate memory! "<<Std::endl; returnnullptr; }    intinput; Std::cout<<"Enter the linked list value:"<<Std::endl;  while(CIN >> input && input! =-1) {linklist*tmp =Newlinklist; TMP->value =input; TMP->next = head->Next; Head->next =tmp; TMP=nullptr; }    returnhead;}//Measuring meter lengthintListlen (linklist *L) {        intLength =0;  while(L! =nullptr) {Length++; L= l->Next; }    //std::cout << "table length:" << length-1 << Std::endl;    returnlength-1;}//Linked list InsertionvoidInsertlist (linklist *l,intPosintvalue) {    intindex =1; Linklist*p =L; Linklist*tmp =Newlinklist; TMP->value =value; if(Pos <=0|| pos > Listlen (L) +1) {printf ("the input location is illegal and cannot be inserted!"); return; }    Else if(pos = = Listlen (L) +1)    {         while(P->next! =NULL) {P= p->Next; } tmp->next =nullptr; P->next =tmp; return; }    Else    {         while(Index <POS) {Index++; P= p->Next; } tmp->next = p->Next; P->next =tmp; }}//Linked list DeletevoidDeleteList (linklist *l,intPOS) {    intindex =1; if(Pos <=0|| POS >Listlen (L)) {printf ("the input location is illegal and cannot be deleted!\n"); return; }    Else    {         while(Index <POS) {Index++; L= l->Next; } L->next = l->next->Next; }}//Print linked listvoidPrint (Linklist *L) {    if(L->next = =nullptr) {printf ("There are no elements in the list!"); return; }    Else{linklist*tmp =Newlinklist; TMP= l->Next;  while(TMP! =NULL) {printf ("%d->", tmp->value); TMP= tmp->Next; } printf ("null\n"); }}//Reverse Linked listlinklist* reverselist (linklist *L) {    if(L = =nullptr)returnnullptr; Linklist*rev =Newlinklist; Rev->next =nullptr;  while(L->next! =nullptr) {L= l->Next; Linklist*tmp =Newlinklist; TMP->value = l->value; TMP->next = rev->Next; Rev->next =tmp; TMP=nullptr; }    returnRev;}intMainintargcint*argv[]) {linklist*l =Newlinklist;    Initlist (L); L=createlist ();    Print (L); Insertlist (L,4,Ten);    Print (L); DeleteList (L,5);    Print (L); Linklist*rev =Newlinklist; Rev->next =nullptr; Rev=reverselist (L);    Print (rev);  while(true) {GetChar (); }    return 0;}

Linked list of data structures (common operations for single-linked lists)

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.