ll_fo.c

来自「cygwin下的包含各种c基本操作的demo程序」· C语言 代码 · 共 104 行

C
104
字号
//----------------------------------------------------------------//            the link_list operation function//----------------------------------------------------------------#include "my_common.h"int LinkListInit(link_list ** head){    if (!(*head = (link_list *)malloc(sizeof(link_list))))        return -1;    (*head)->data = NULL;    (*head)->next = NULL;         return 0; 		}int add_node(int data,int k,link_list * head){	 if ((head == NULL)||( k <=0))	 { return -1; }	 	 link_list *tmp_node = (link_list *)malloc(sizeof(link_list));	 tmp_node->data = data;	 tmp_node->next = NULL;	 	 if ((head->next == NULL)&&(k == 1))	 {	     head->next = tmp_node;	     //tmp->next = NULL;	     return 0;		 }	 	 while((k--)&&(head->next != NULL))	 { 	    head = head->next;    }      if (k>0)   { return -1;}      tmp_node->next = head->next;   head->next = tmp_node;   	 return 0;}int del_node(int n,link_list *head){    if ( n <= 0)    {  return -1; }        while(((n--) >= 1)&&(head->next != NULL))    {         head = head->next;                      }	    if (head->next == NULL)    {  return 0; }        head->next = ((link_list *)(head->next))->next; //NOTE:the force covert is important         return 0;	}void ll_print(link_list *head){   	  int ii = 0;	  	  printf("the value of the linklist is ");    while(head != NULL)    {         printf("node %d : %d   ",ii,head->data);	         head = head->next;    	   ii++;    }    printf("\n");    	}void ll_demo(void){    link_list * head = NULL;    int ii = 10;    int jj = 0;        LinkListInit(&head);        while( ii > 0)    {    add_node(ii,jj,head);    ii--,jj++;    }        add_node(999,3,head);    ll_print(head);        del_node(7,head);    ll_print(head);    	    return;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?