1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include<stdio.h> #include<stdlib.h> typedef struct _NodeList{ int data; struct _NodeList * nlp; }NodeList;
int getsize(); NodeList * get(int index); void insert(NodeList * nlp,int index); NodeList * new_node_list(int data); void add(NodeList *nlp); void print_of_range(int range); NodeList * remove_o(int index); void clear(); NodeList * HEAD=NULL; int NODE_SIZE=0; int main(int argc, char const *argv[]){ for(short i=0;i<10;i++){ add(new_node_list(i+100)); } insert(new_node_list(999),1); print_of_range(6); remove_o(2); print_of_range(6); clear(); return 0; } NodeList * new_node_list(int data){ NodeList * nlp=(NodeList *)malloc(sizeof(NodeList)); nlp->data=data; nlp->nlp=NULL; return nlp; } void insert(NodeList *nlp,int index){ if(index>NODE_SIZE){ printf("insert index:%d > NODE_SIZE:%d\n",index,NODE_SIZE); exit(0); } if(index==0){ NodeList * temp=HEAD; nlp->nlp=temp; HEAD=nlp; NODE_SIZE++; } else if(index==NODE_SIZE) add(nlp); else{ NodeList * prep=HEAD; NodeList * suffp=NULL; for(int i=0;i<index-1;i++){ prep=prep->nlp; } suffp=prep->nlp; prep->nlp=nlp; nlp->nlp=suffp; NODE_SIZE++; } } void add(NodeList *nlp){ if(HEAD==NULL){ HEAD=nlp; NODE_SIZE++; return; } NodeList * n=HEAD; for(int i=0;i<NODE_SIZE-1;i++){ n=n->nlp; } NODE_SIZE++; n->nlp=nlp; } NodeList * get(int index){ if(index<0||index>NODE_SIZE-1){ printf("Out of range :index > %d\n",NODE_SIZE-1); exit(1); } NodeList * getnlp=HEAD; for(int i=0;i<index;i++){ getnlp=getnlp->nlp; } return getnlp;
} int getsize(){ return NODE_SIZE; } void print_of_range(int range){ if(range>NODE_SIZE||range<0){ printf("Out of range :range > %d\n",NODE_SIZE); exit(1); } NodeList * nlp=HEAD; for(int i=0;i<range;i++){ printf("%d :value : %d\n",i,nlp->data); nlp=nlp->nlp; } } NodeList * remove_o(int index){ if(index<0||index>NODE_SIZE-1){ printf("Out of range :range > %d\n",NODE_SIZE); exit(1); } NodeList * removep=HEAD; if(index==0){ HEAD=HEAD->nlp; } else if(index==NODE_SIZE-1){ NodeList * c=HEAD; for(int i=0;i<NODE_SIZE-2;i++){ c=c->nlp; } removep=c->nlp; c->nlp=NULL; }else{ NodeList * reprefix=HEAD; for(int i=0;i<index-1;i++){ reprefix=reprefix->nlp; } removep=reprefix->nlp; reprefix->nlp=removep->nlp; } NODE_SIZE--; return removep; } void clear(){ NodeList * wait=NULL; NodeList * temp=HEAD; for(int i=0;i<NODE_SIZE;i++){ wait=temp; temp=temp->nlp; free(wait); } NODE_SIZE=0; }
|