C Single-linked list operation

Source: Internet
Author: User

The head pointer holds the number of linked list elements, the head node begins to hold the data, and the tail node points to null

List.h

#ifndef _list_h#define_list_h#include<stdio.h>#include<stdlib.h>#defineDEBUG 0typedefstructnode{intVal; structNode *Next;} Node; Node* L_malloc ();//Allocating MemoryNode * init ();//InitializevoidShow (Node *list);//show linked list elementsintLsearch_pos (Node *list,intn);//Left SearchintRsearch_pos (Node *list,intn);//Right SearchintFind_val (Node *list,intVal);//Value SearchNode * INSERT (node *list,intPosintn);//inserting NodesNode * DEL_NODE (node *list,intn);//Specify location Delete nodeNode * DEL_VAL (node *list,intVal);//Specify a value to delete a node#endif

List.c

#include"list.h"/************************************* * Allocate memory, retry 1 times when failed *************************************/Node*L_malloc () {Node*p =NULL; intCNT =3;  Do{p= (Node *)malloc(sizeof(Node)); } while(NULL = = P &&--cnt);//try again when you fail    if(0>CNT) {Perror ("malloc"); Exit (-1);//memory allocation failed, exit    }    returnp;}/************************************* * Initialize *************************************/Node*init () {Const intnum =8; intCNT =0; Node*head =NULL; Node*pcur =NULL; Node*pnew =L_malloc (); Head= Pcur = Pnew;//Head->val =0;//head pointer, value record number of linked list elements     for(; CNT < num; cnt++) {pnew=L_malloc (); Pnew->val =CNT; Pcur->next =pnew; Pcur=pnew; Head->val++; } pcur->next =NULL; returnHead; }/************************************* * Show *************************************/voidShow (Node *list) {     while(NULL! =list) {printf ("%d->",list->val); List= list->Next; } printf ("end\n\n");}/************************************* * Start from left to search *************************************/intLsearch_pos (Node *list,intN) {    if(N <1|| n > List->val) {//number of linked list values exceededprintf"exceed length of list\n"); return-1; }     for(intCNT =0; CNT < n; cnt++) {List= list->Next; }    returnList->Val; }/************************************* * Start from right to search *************************************/intRsearch_pos (Node *list,intN) {    if(N <1|| n > list->val) {printf ("exceed length of list\n"); return-1; } Node*pcur = list->next;//define 2 pointers: Current positionNode *ptag = list->next;//Target Location     for(intCNT =0; CNT < n; cnt++) {Pcur= pcur->Next; }                                //interval n between the current position and the target position     while(NULL! = pcur) {//The current position is first moved to the end of the footer, where PTag is the location to searchPcur = pcur->next;//Move 2 pointers at a timePTag = ptag->next;//    }    returnPtag->Val;}/************************************* * Search from left to specified value * has value return position, no value return-1 *************************************/intFind_val (Node *list,intval) {    if(DEBUG) printf ("In find_val:\n"); intCNT =1; intLen = list->Val;  for(; CNT <= Len; cnt++) {List= list->Next; if(DEBUG) printf ("list->val:%d\n",list->val); if(val = = list->val) {            returnCNT; }    }    return-1;}/************************************* * Inserts a new value at the specified position *************************************/Node* Insert (Node *list,intPosintN) {    if(DEBUG) printf ("In insert:\n"); Node*pcur =list; Node*pnew =L_malloc (); Pnew->val =N;  for(intCNT =1; CNT < POS && Pcur->next; cnt++) {//exits at the specified position or at the end of the listPcur = pcur->Next; if(DEBUG) printf ("pcur->val:%d\n",pcur->val); }    if(NULL = = Pcur->next) {//to reach the end of the list, test the empty list or run normalization of negative numbersPnew->next = NULL;//Insert Footer When POS is larger than the linked list lengthPcur->next =pnew; }    Else{pnew->next = pcur->Next; Pcur->next =pnew; } list->val++; returnlist;}/************************************* * Specify location Delete value *************************************/Node* Del_node (Node *list,intN) {    if(N <1|| n > list->val) {printf ("exceed length of list\n"); returnlist; } Node*pcur =NULL; Node*ptag =list;  for(intCNT =0; CNT < n; cnt++) {Pcur=PTag; PTag= ptag->Next; } pcur->next = ptag->Next;  Free(PTag); List->val--; returnlist;}/************************************* * Specify a value to delete a node *************************************/Node* Del_val (Node *list,intval) {    intn =Find_val (List,val); if( -1= = N) {//No value specified, no delete        returnlist; }    returnDel_node (list,n);}

Main.c

#include"list.h"intMain () {Node*list =init ();    Show (list); intnum = Lsearch_pos (list,2 ); printf ("The 2th value is%d\n", num);        Show (list); Num= Rsearch_pos (list,2 ); printf ("The value from right 2th is%d\n", num);        Show (list); Num= Find_val (list,5 ); printf ("The value 5 is in%dth\n", num);    Show (list); List= Del_node (list,1 ); printf ("After delete 1th value,the list is:\n");            Show (list); List= Del_val (list,3 ); printf ("After Delete value 3,the list is:\n");            Show (list); List= Del_val (list, - ); printf ("After Delete value 100,the list is:\n");    Show (list);  for(intCNT =Ten; CNT >0; CNT--) {List=Del_node (list, CNT); printf ("After delete%dth value,the list is:\n", CNT);    Show (list); }     for(intCNT =0; CNT < -; CNT + +) {List= insert (list, cnt%5, Rand ()); printf ("After insert a value at%dth,the list is:\n", CNT);    Show (list); } list= Insert (list,6, About ); printf ("After insert a value at 6th,the list is:\n");        Show (list); List= insert (list,-6, About ); printf ("After insert a value at-6th,the list is:\n"); Show (list);}

C Single-linked list operation

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.