C + + unidirectional linked list

Source: Internet
Author: User

Because of the time rush, the author did not carry out any inspection, in short, 165 lines of bare hands, debugging without bugs, the basic function of the implementation of no major problems, may be some of the details are ill-considered (this is also a C + + criticism, The car does not just push forward), but also forget Forgive.

The code is written in the C + + environment and can be run on the C-compiler platform, but it needs to be modified (the statement of the declaration variable is written before the execution of the statement).

#include <stdio.h>#include<stdlib.h>//define nodes, All nodes must use malloc () application in the heap, in the program execution phase to ensure the existence of space;//-1 indicates an abnormal exittypedefstructnode{intdata; Node*next;} Node;//used to indicate the length of the list, 1 represents an empty table of headless nodesintLen =-1;//define a global header nodeNode *head =NULL;//Create an empty list with a head nodevoidcreatenodelist () {if(head! = NULL)//represented as a non-empty tableExit (-1);//Non-normal ExitHead = (Node *)malloc(sizeof(Node));//Application Heap Space    if(head = = NULL)//if head==null the application failsExit (-1); //Header node has a data field of 0 and the pointer field is NULLHead->data =0; Head->next =NULL; Len=0;//0 represents an empty table with a head node}//Add a node (added after a node that is null for the pointer field of the current Node)voidAddNode (intData) {    if(head = = NULL)//represented as empty tableExit (-1); Node*newnode = (Node *)malloc((sizeof(Node));//Create a new node    if(newNode = = NULL)//Application FailureExit (-1); //initialize data fields and pointer fields for new nodesNewnode->data =data; NewNode->next =NULL; //Connection WorkNode *p = head;//Current NodeNode *q = head->next;//Next Node     while(q! =NULL) {        //loop-formed Pointer field representing the current node is not NULLp = q;//Update current nodeQ = p->next;//Update the current Node's    }    //Loop end indicates that the current Node's pointer field is NULL//Update the current node pointer field to a new junctionP->next =newNode; Len++;//table length plus one}//get node Data (0<index<len)intGetNode (intIndex) {    intData =-1; if(head = = NULL)//represented as empty tableExit (-1); if(index <0)//subscript to start from 0Exit (-1); if(index >= Len)//Subscript out of BoundsExit (-1); Node*p = head->next; Data= p->data;  for(inti =0; I < index;i++) {p=p->next; } Data= p->data; returndata;}//Element LookupintGetData (intData) {    if(head = = NULL)//represented as empty tableExit (-1); Node*p = head->next;//represents the first node     for(inti =0;p!=null;i++)    {        if(p->data==Data)returnI//return subscriptp = p->next; }}//print a linked list to the screenvoidgetlinks () {if(head = = NULL)//represented as empty tableExit (-1); //point to first nodeNode *p = head->next; //if the nodule is not null, the node is present     while(p! =NULL) {printf ("%d\n", p->data); P= p->next; }}//Delete a nodevoidRemoveNode (intIndex) {    if(head = = NULL)//represented as empty tableExit (-1); if(index <0)//subscript to start from 0Exit (-1); if(index >= Len)//Subscript out of BoundsExit (-1); //p represents the current node, Q indicates the current Node's pointer fieldNode *p = head;//the previous node to be deletedNode *q = head->next;//the node to be deleted     for(inti =0; I < index;i++) {p=q; Q= p->next; }    //re-connectP->next = q->next;  free(q);//Delete q nodelen--;//table length minus one}//Empty listvoidCleanlinks ()//represented as empty table{    if(head = =NULL) exit (-1); Node*p = head;//Current NodeNode *q=head->next;//Next Node     free(p);//Delete current node     while(q!=NULL) {p=q; Q= p->next;  free(p);//Delete current node} len= -1;//indicates that the table is empty}voidmain () {//Create a tablecreatenodelist (); //adding elementsAddNode (1); AddNode (2); AddNode (3); AddNode (4); //Print table, and table lengthgetlinks (); printf ("len:%d\n", len); //View Third node "4"printf"Index 3:%d\n", GetNode (3)); //index of the node where data 4 is locatedprintf"Data 4:%d\n", GetData (4)); //Remove the third element "3"RemoveNode (2); //Separatorsprintf"=============\n"); //Print table and table length againgetlinks (); printf ("len:%d\n", len);    Cleanlinks (); System ("Pause");}

A/C + + one-way list

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.