½ñÌìÎÒÃÇÒªÌÖÂÛµÄÊǵ¥Á´±íµÄ´´½¨²åÈëÓëɾ³ý /*×¢ÒâÒòΪ½¨±íʱÓÃÁË×Ö·ûÐÍÊý¾ÝËùÒÔÊäÈëÊý×ÖʱֻÄÜÊä0~9*/ #include<stdio.h> #include<stdlib.h> typedef struct node //½áµãµÄ½á¹¹Ìå { int data; //Êý¾ÝÓò struct node *next; //Ö¸ÕëÓò }node, *list; list HeadCreat£¨£©£» //Í·²å·¨´´½¨Á´±í list TailCreat£¨£©£» //β²å·¨´´½¨Á´±í void Insert£¨list head, int x, char value, int length£©£»//ÏòÁ´±íÖÐÌØ¶¨Î»ÖòåÈëÒ»¸öÔªËØ void Delete£¨list head, int x, int length£©£» //ɾ³ýÒ»¸ö½áµã int Length£¨listhead£©£» //ͳ¼ÆÁ´±íµÄ³¤¶È void print£¨head£©£» //Êä³öÁ´±í int main£¨void£© { list head; int x, length; char value; // head = HeadCreat£¨£©£» //Í·²å·¨´´½¨Á´±í head = TailCreat£¨£©£» //β²å·¨´´½¨Á´±í length = Length£¨head£©£» print£¨head£©£» printf£¨"\n"£©£» printf£¨"ÇëÊäÈëÒª²åÈëµÄÊýÖµ£º"£©£» scanf£¨"%c",&value£©£» printf£¨"%c\n",value£©£» printf£¨"ÇëÊäÈëÒª²åÈëµÄλÖãº"£©£» scanf£¨"%d",&x£©£» printf£¨"%d\n",x£©£» Insert£¨head, x, value, length£©£» //ÏòÁ´±íÖÐÌØ¶¨Î»ÖòåÈëÒ»¸öÔªËØ print£¨head£©£» printf£¨"\n"£©£» printf£¨"ÇëÊäÈëҪɾ³ýµÄ½áµã£º"£©£» scanf£¨"%d",&x£©£» Delete£¨head, x, length£©£» //ɾ³ýÒ»¸ö½áµã print£¨head£©£» return 0; } list HeadCreat£¨£© //Í·²å·¨´´½¨Á´±í { list head; char c; int flag = 1; head = £¨list£©malloc£¨sizeof£¨node£©£©£» //Í·½áµã£¬ÀïÃæÃ»ÓÐÊý¾Ý if£¨£¡head£© printf£¨"Á´±í´´½¨´íÎó£¡"£©£» head->next = NULL; while£¨flag == 1£© { printf£¨"ÇëÊäÈëÊý¾Ý£º"£©£» c = getchar£¨£©£» flushall£¨£©£» //Çå³ýenterËù²úÉúµÄ»º´æÊý¾Ý if£¨c != '$'£© { node *w; w = £¨node *£©malloc£¨sizeof£¨node£©£©£» w->data = c; w->next = head->next; head->next = w; } else flag = 0; } return head; } list TailCreat£¨£© //β²å·¨´´½¨Á´±í { list head; node *p; char c; int flag = 1; head = £¨list£©malloc£¨sizeof£¨node£©£©£» //Í·½áµã£¬ÀïÃæÃ»ÓÐÊý¾Ý if£¨£¡head£© printf£¨"Á´±í´´½¨´íÎó£¡"£©£» p = head; p->next = NULL; head->next = NULL; while£¨flag == 1£© { printf£¨"ÇëÊäÈëÊý¾Ý£º"£©£» c = getchar£¨£©£» flushall£¨£©£» //Çå³ýenterËù²úÉúµÄ»º´æÊý¾Ý if£¨c != '$'£© { node *w; w = £¨node *£©malloc£¨sizeof£¨node£©£©£» w->data = c; p->next = w; p = w; } else flag = 0; p->next = NULL; } return head; } int Length£¨list head£© //ͳ¼ÆÁ´±íµÄ³¤¶È { node *p; int i = 0; p = head->next; while£¨p£© { p = p->next; i++; } return i; } void Insert£¨list head, int x, char value, int length£©//ÏòÁ´±íÖÐÌØ¶¨Î»ÖòåÈëÒ»¸öÔªËØ { node *pre, *p; //*preΪҪ²åÈëλÖõÄǰÇý½áµã int i = 1; pre = head; p = £¨node *£©malloc£¨sizeof£¨node£©£©£» while£¨i < x£© { pre = pre->next; i++; } if£¨i > length£© { printf£¨"²åÈë´íÎó£¡\n"£©£» } else { p->data = value; p->next = pre->next; pre->next = p; } } void Delete£¨list head, int x, int length£© //ɾ³ýÒ»¸ö½áµã { node *pre, *p; //*perΪҪɾ³ý½áµãµÄǰÇý½áµã£¬*hidΪҪɾ³ýµÄ½áµã int i = 1; p = head; if£¨x > length£© printf£¨"ÊäÈë½áµãλÖôíÎó£¡\n"£©£» else { while£¨i <= x£© { pre = p; p = p->next; i++; } pre->next = p->next; } } void print£¨list head£© //Êä³öÁ´±í { node *p; p = head->next; for£¨£» p != NULL; p = p->next£© printf£¨"%c ",p->data£©£» } |