#include<stdio.h>#include<malloc.h>typedef int Item;//¶¨ÒåÊý¾ÝÏîÀàÐÍtypedef struct node * PNode;//¶¨Òå½ÚµãÖ¸Õë//½ÚµãµÄ¶¨Òåtypedef struct node{ Item item;//Êý¾ÝÓò PNode next;//Á´Óò }Node,* SList;/*int SL_Creat(SList *p_list,int size)²ÎÊýp_list£ºÖ¸ÏòÒ»¸öÁ´±íÖ¸Õ룬´Ë´¦´«Èë±íÍ·µØÖ·size£ºÒª´´½¨µÄÁ´±í·ÖÅäµÄÊý¾ÝÔªËØ¿Õ¼ä¸öÊý£¬²»°üº¬Í·½Úµã·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø0¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇ´´½¨Ò»¸ö´óСΪsizeµÄÁ´±í²¢°ÑÁ´±íµÄÍ·Ö¸Õ븳¸øp_lsitËùÖ¸µÄÁ´±íÖ¸Õë¡£*/int SL_Creat(SList *p_list,int size){ PNode p=NULL; int i; *p_list = (SList)malloc(sizeof(Node)); if(*p_list==NULL) return -1; (*p_list)->next = NULL; for(i=size;i>0;i--) { p = (PNode)malloc(sizeof(Node)); if(p==NULL) return -1; p->item = 0; p->next = (*p_list)->next; (*p_list)->next = p; } return 1;}/*int SL_Insert(SList list,int pos,Item item)²ÎÊýlist£ºÒª²åÈëÊý¾ÝµÄµ¥Á´±íint£ºÖ¸¶¨Òª²åÈëÔªËصÄλÖ㬴Ó1¿ªÊ¼¼ÆÊýitem:Òª²åÈëµÄÊý¾ÝÏî·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø-1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇÔÚÁ´±ílistµÄposλÖòåÈëÐÂÔªËØ£¬ÆäֵΪitem¡£*/int SL_Insert(SList list,int pos,Item item){ PNode p,q; int i; p = list; i = 0; while(p!=NULL && i<pos-1)//½«Ö¸ÕëpÒƶ¯µ½Òª²åÈëÔªËØλÖÃ֮ǰ { p = p->next; i++;//i¼Ç¼pÖ¸ÏòµÄÊǵڼ¸¸öλÖà } if(p==NULL || i > pos-1) return -1; q = (Node *)malloc(sizeof(Node));//δ²åÈë½Úµã·ÖÅäÄÚ´æ if(q!=NULL)//ÈôÄÚ´æ·ÖÅä³É¹¦£¬½«½Úµã²åÈëÖ¸¶¨Î»Öà { q->item = item; q->next = p->next; p->next = q; return 1; } else { return -1; }}/*int SL_GetItem(SList list,int pos,Item *p_item)²ÎÊýlist£ºÒª»ñÈ¡Êý¾ÝÏîËùÔڵĵ¥Á´±íint£ºÖ¸¶¨Òª»ñÈ¡ÔªËØÔÚµ¥Á´±íÖеÄλÖÃp_item:Ö¸ÏòÒª½ÓÊÕÊý¾ÝÏîµÄ±äÁ¿·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø-1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇ»ñÈ¡ÔÚÁ´±ílistµÄposλÖõÄÔªËصÄÊý¾ÝÏÆäÖµ¸³¸øp_itemËùÖ¸µÄ±äÁ¿¡£*/int SL_GetItem(SList list,int pos,Item *p_item){ PNode p; int i; p = list; i = 0; while(p!=NULL && i<pos)//½«Ö¸ÕëpÒƶ¯µ½Òª·µ»ØµÄÔªËØλÖà { p = p->next; i++;//i¼Ç¼pÖ¸ÏòµÄÊǵڼ¸¸öλÖà } if((p==NULL)||(i>pos)) { return -1; } *p_item = p->item; return 1;}/*int SL_Delete(SList list,int pos,Item * p_item)²ÎÊýlist£ºÒªÉ¾³ýÔªËØËùÔڵĵ¥Á´±íint£ºÖ¸¶¨ÒªÉ¾³ýÔªËØÔÚµ¥Á´±íÖеÄλÖÃp_item:Ö¸Ïò½ÓÊÕɾ³ýÔªËصÄÊý¾ÝÏîµÄ±äÁ¿·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø-1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇɾ³ýÔÚÁ´±ílistµÄposλÖõÄÔªËØ£¬ÆäÖµ¸³¸øp_itemËùÖ¸µÄ±äÁ¿¡£*/int SL_Delete(SList list,int pos,Item * p_item){ PNode p,q; int i; p = list; i = 0; while(p!=NULL && i<pos-1)//½«Ö¸ÕëpÒƶ¯µ½Òª²åÈëÔªËØλÖÃ֮ǰ { p = p->next; i++;//i¼Ç¼pÖ¸ÏòµÄÊǵڼ¸¸öλÖà } if(p->next==NULL || i > pos-1) return -1; q = p->next; p->next = q->next; if(p_item != NULL) *p_item = q->item; free(q); return 1;}/*int SL_SetItem(SList list,int pos,Item item)²ÎÊýlist£ºÒªÉèÖÃÔªËØËùÔڵĵ¥Á´±íint£ºÖ¸¶¨ÒªÉèÖÃÔªËØÔÚµ¥Á´±íÖеÄλÖÃp_item:ÒªÉèÖÃÔªËصÄÊý¾ÝÏîµÄÖµ·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø-1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊǽ«Á´±ílistµÄposλÖõÄÔªËصÄÊý¾ÝÏîÉèÖÃΪitem¡£*/int SL_SetItem(SList list,int pos,Item item){ PNode p=NULL; int i; p = list; i = 0; while(p!=NULL && i<pos)//½«Ö¸ÕëpÒƶ¯µ½Òª²åÈëÔªËØλÖÃ֮ǰ { p = p->next; i++;//i¼Ç¼pÖ¸ÏòµÄÊǵڼ¸¸öλÖà } if(p==NULL || i > pos) return -1; p->item = item; return 1;}/*int SL_Find(SList list,int *pos,Item item)²ÎÊýlist£ºÒª²éÕÒÔªËØËùÔڵĵ¥Á´±íint£ºÖ¸ÏòÒª´æ´¢µÄ²éµÃµÄÔªËصÄλÖõıäÁ¿p_item:Òª²éÕÒÔªËصÄÊý¾ÝÏîµÄÖµ·µ»ØÖµÈô³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø-1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇÔÚÁ´±ílistÖвéÕÒÊý¾ÝÏîΪitemµÄÔªËØ£¬½«ÆäλÖÃÖµ¸³¸øposËùÖ¸µÄ±äÁ¿¡£*/int SL_Find(SList list,int *pos,Item item){ PNode p; int i; p = list; i = 0; while(p!=NULL && p->item!=item)//½«Ö¸ÕëpÒƶ¯µ½Òª²åÈëÔªËØλÖÃ֮ǰ { p = p->next; i++;//i¼Ç¼pÖ¸ÏòµÄÊǵڼ¸¸öλÖà if(p->item == item) { *pos = i; //·µ»Ø²éѯµ½µÄλÖà return 1; } } return -1; }/*int SL_Empty(SList list)²ÎÊýlist£ºÒªÅжϵĵ¥Á´±í·µ»ØÖµÈôΪ¿ÕÔò·µ»Ø1£¬·ñÔò·µ»Ø 0¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇÅжÏÁ´±ílistÊÇ·ñΪ¿Õ±í¡£*/int SL_Empty(SList list){ PNode p; p = list; if(p->next == NULL) return 1; return 0;}/*int SL_Size(SList list)²ÎÊýlist£ºÒª²éÕҵĵ¥Á´±í·µ»ØÖµ·µ»Ø°üº¬½ÚµãµÄ¸öÊý¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇ·µ»ØÁ´±ílistÖнڵãµÄ¸öÊý£¬°üº¬Í·½Úµã¡£*/int SL_Size(SList list){ PNode p; int i; p = list; i = 0; while(p!=NULL) { p = p->next; i++; } return i;}/*int SL_Clear(SList *p_list)²ÎÊýp_list£ºÖ¸ÏòÒªÇå³ýµÄµ¥Á´±í·µ»ØÖµ³É¹¦·µ»ØֵΪ1¡£¹¦Äܸú¯ÊýµÄ¹¦ÄÜÊÇÇå³ýÁ´±íµÄËùÓнڵ㣬°üº¬Í·½Úµã¡£*/int SL_Clear(SList *p_list){ PNode p,q; int i; p = *p_list; i = 0; while(p!=NULL) { q = p->next;//½èÖúÓÚq´æ´¢pµÄÁ´Óò£¬·ñÔòÊÍ·ÅpºóÎÞ·¨ÒýÓà free(p); p = q; } *p_list = NULL;//½«ËùÖ¸µÄÁ´±íÖ¸ÕëÉèΪNULL return 1;}