#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;
}