Algorithm 2---List 1 simple implementation of---linked list

Source: Internet
Author: User
Tags stdin strcmp

1 basic knowledge of the linked list 1.1 basic definitions and advantages and disadvantages the various objects in the list are arranged in order, noting the difference between the arrays, the linear order of the array is determined by the array subscript, but the order of the list is determined by the pointers in each object. The list contains two aspects: 1 data Part, the actual data of the node is saved, 2 address part, the address of the next node is saved (single linked list). The advantages and disadvantages of the list: easy to understand, easy to operate; Cons: 1 when inserting and deleting operations, it is often necessary to move a large amount of data; 2 if the table is larger, it is sometimes difficult to allocate enough contiguous space, resulting in memory allocation failure and cannot be stored; 1.2 Categorized single linked list: Only one pointer per node ; Doubly linked list: Each node contains two pointers, one pointing to the next node, one pointing to the previous node, and a single-loop linked list: In a single-linked list, the pointer-domain null of the endpoint is changed to a table-head node or a starting node to form a single-loop linked list. Double loop linked list: The first node's pointer to the previous node points to the end of the chain, the tail node pointing to the next node pointer to the table head and where to form a double-loop linked list; 2 list Implementation C language version 2.1 Simple implementation First we do not consider the various adaptability, first of all, the form of fixed data ; contains two folders list_simple.h,list_simple.c list_simple.h
#ifndef _list_simple_htypedefstruct{    Charkey[Ten]; Charname[ -]; intAge ;} Data;typedefstructnode{Data Nodedata; structNode *NextNode;} Cltype; Cltype*claddend (Cltype *head,data Nodedata); Cltype*claddfirst (Cltype *head,data Nodedata); Cltype*clfindnode (Cltype *head,Char*key); Cltype*clinsertnode (Cltype *head,Char*findkey,data nodedata);intCldeletenode (Cltype *head,Char*key);intCllength (Cltype *head);voidClallnode (Cltype *head);#endif/*=======================================*///append nodes;Cltype *claddend (Cltype *head,data Nodedata) {Cltype*node,*htemp; if(! (Node= (Cltype)malloc(sizeof(Cltype))))//Allocate Space{printf ("memory allocation failed! \ n"); returnNULL; }    Else{node->nodedata=nodedata;//Save DataNode->nextnode=null//set the node pointer to null, which is the end of the table;        if(head==NULL) {Head=node; returnHead; } htemp=Head;  while(htemp->nextnode!=NULL) {Htemp=htemp->NextNode; } htemp->nextnode=node; returnHead; }}/*=======================================*///Inserting head nodesCltype *claddfirst (Cltype *head,data Nodedata) {Cltype*node; if(! (Node= (Cltype)malloc(sizeof(Cltype)))) {printf ("memory allocation failed \ n"); returnNULL; }    Else{node->nodedata=Nodedata; Node->nextnode=Head; Head=node; returnHead; }}/*=======================================*///Find NodesCltype *clfindnode (Cltype *head,Char*key) {Cltype*htemp; Htemp=Head;  while(htemp) {if(stcmp (htemp->nodedate.key,key) = =0)        {            returnhtemp; } htemp=htemp->NextNode; }    returnNULL;}/*=======================================*/Cltype*clinsertnode (Cltype *head,Char*findkey,data Nodedata) {Cltype*node,*nodetemp; if(! (Node= (Cltype)malloc(sizeof(Cltype)))) {printf ("memory allocation failed! \ n"); returnNULL; } node->nodedata=Nodedata; Nodetemp=Clfindnode (Head,findkey); if(nodetemp) {node->nextnode=nodetemp->NextNode; Nodetemp->nextnode=node; }    Else{printf ("did not find the insertion position for the fight \ n");  Free(node); }    returnhead;}/*=======================================*///Delete the node;intCldeletenode (Cltype *head,Char*key) {Cltype*node,*htemp; Htemp=Head; Node=Head;  while(htemp) {if(strcmp (htemp->nodedata.key,key) = =0) {node->nextnode=htemp->NextNode;  Free(htemp); return 1; }        Else{node=htemp; Htemp=htemp->NextNode; }    }    return 0;}/*=======================================*///calculate the length of a linked listintCllength (Cltype *head) {Cltype*htemp; intlen=0; Htemp=Head;  while(htemp) {len++; Htemp=htemp->NextNode; }    returnLen;}/*=======================================*///Show all the nodes .voidClallnode (Cltype *head) {Cltype*htemp;    Data Nodedata; Htemp=Head; printf ("There are%d nodes in the current list, and all the data for the linked list are as follows: \ n", Cllength (head));  while(htemp) {Nodedata=htemp->Nodedata; printf ("node: (%s,%s,%d) \ n", Nodedata.key,nodedata.name,nodedata.age); Htemp=htemp->NextNode; }}

List_simple.c
#include <stdlib.h>#include<stdio.h>#include<string.h>#include"list_simple.h"voidMain () {Cltype*node,*head=NULL;    Data Nodedata; Charkey[Ten]; Charfindkey[Ten]; printf ("link list test, enter the data in the list, enter the format: keyword name age \ n");  Do{fflush (stdin); scanf ("%s", Nodedata.key); if(strcmp (Nodedata.key,"0")==0)        {             Break;//Enter 0, exit        }        Else{scanf ("%s%d",nodedata.name,&nodedata.age); Head=claddend (Head,nodedata); }    } while(1);    Clallnode (head); printf ("Demo Insert node, enter the insertion position keyword: \ n"); scanf ("%s",&FindKey); printf ("Enter data for insertion node (keyword name age) \ n"); scanf ("%s%s%d",nodedata.key,nodedata.name,&nodedata.age); Head=Clinsertnode (Head,findkey,nodedata);    Clallnode (head); printf ("Demo Delete node, enter the keyword to delete: \ n");    Fflush (stdin); scanf ("%s", key);    Cldeletenode (Head,key);    Clallnode (head); printf ("demo Find in list, go to keyword to find: \ n");    Fflush (stdin); scanf ("%s", key); Node=Clfindnode (Head,key); if(node) {Nodedata=node->Nodedata; printf ("the node corresponding to the keyword is (%s%s%d) \ n", Key,nodedata.key,nodedata.name); }    Else{printf ("you didn't find your corresponding node in the list. \ n"); }}

The code has been compiled and passed! can be used directly

In the following we give an arbitrary list to store any type of data implementation method, please expect: Algorithm 2---List 2---linked list arbitrary storage implementation

Algorithm 2---List 1 simple implementation of---linked 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.