当前位置: 首页 > >

ÈëÃÅС°×ѧÊý¾Ý½á¹¹ÓëËã·¨£¨2£©??ÏßÐÔ±í

发布时间:

Êý¾Ý½á¹¹ÓëËã·¨£¨2£©??ÏßÐÔ±í
Ò»¡¢¸ÅÄÓÉÁã¸ö»ò¶à¸öÊý¾ÝÔªËØ×é³ÉµÄÓÐÏÞÐòÁÐ

×¢Ò⣺1.ÐòÁÐÐÔ£¬ÔªËؼäÓÐÏÈÀ´ºóµ½
2.µÚÒ»¸öÔªËØÎÞÇ°Çý£¬×îºóÒ»¸öÔªËØÎÞºó¼Ì£¬ÆäËûÔªËØÓÐÇÒÖ»ÓÐÒ»¸öÇ°ÇýºÍºó¼Ì£¬Ò»¶ÔÒ»µÄ¹Øϵ£¬¾ÍÏñÒ»ÌõÏß¡£
3.ÏßÐÔ±íÊÇÓÐÏÞµÄ
4.ÏßÐÔ±íÔªËصĸöÊýn¶¨ÒåΪÏßÐÔ±íµÄ³¤¶È£¬µ±n=0ʱ£¬³ÆΪ¿Õ±í


°´´æ´¢½á¹¹·ÖÀࣺ˳Ðò´æ´¢½á¹¹£¨Êý×飩ºÍÁ´Ê½´æ´¢½á¹¹£¨Á´±í£©
˳Ðò´æ´¢£ºÓÃÒ»¶ÎµØÖ·Á¬ÐøµÄ´æ´¢µ¥ÔªÒÀ´Î´æ·ÅÏßÐÔ±íµÄÊý¾Ý½á¹¹


¶þ¡¢²Ù×÷

1.InitList(*L)£º³õʼ»¯ÏßÐÔ±í£¬½¨Á¢Ò»¸öеĿյÄÏßÐÔÁ´±í
2.ListEmpty(L)£ºÅжÏÏßÐÔ±íÊÇ·ñΪ¿Õ±í£¬ÈôÊÇÔò·µ»Øtrue£¬·ñÔò·µ»Øfalse
3.ClearList(*L)£ºÇå¿ÕÏßÐÔ±í
4.GetElem(L, i, *e)£º½«ÏßÐÔ±íLÖеĵÚi¸öλÖõÄÔªËØÖµ·µ»Ø¸øe
5.LocateElem(L, e)£ºÔÚÏßÐÔ±íLÖвéÕÒÓë¸ø¶¨ÖµeÏàµÈµÄÔªËØ£¬Èç¹û²éÕҳɹ¦£¬·µ»Ø¸ÃÔªËØÔÚ±íÖеÄÐòºÅ£¬·ñÔò·µ»Ø0±íʾʧ°Ü
6.ListInsert(*L, i, e)£ºÔÚÏßÐÔ±íLÖеÚi¸öλÖòåÈëÐÂÔªËØe
7.ListDelte(*L, i, *e)£ºÉ¾³ýÏßÐÔ±íLÖеÚi¸öλÖÃÔªËØ£¬²¢ÓÃe·µ»*äÖµ
8.ListLength(L)£º·µ»ØÏßÐÔ±íLµÄÔªËصĸöÊý


Èý¡¢Ë³Ðò´æ´¢½á¹¹ £¨Êý×飩

ÏßÐÔ±í˳Ðò´æ´¢½á¹¹´úÂë


#define MAXSIZE 20
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int length;
}Sqlist;

ÏßÐÔ±íµÄ³¤¶È²»Ó¦¸Ã³¬¹ýÊý×éµÄ³¤¶È£¬ÏßÐÔ±í´æ´¢Ê±¼äÐÔÄÜΪO(1)£¬Í¨³£³ÆÓÐÕâÖÖÌØÐԵĴ洢½á¹¹ÎªËæ»ú´æ´¢½á¹¹


»ñÈ¡Êý×éÔªËزÙ×÷


#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
Status GetElem(Sqlist L, int i, ElemType *e)
{
if(L.length==0||i<1||i>L.length)
{
return ERROR;
}
*e = L.data[i-1];
return OK;
}

Êý×éÔªËزåÈë²Ù×÷


Status ListInsert(Sqlist *L, int i, ElemType e)
{
int k;
if(L->length>=MAXSIZE)
{
return ERROR;
}
if(i<1||i>L->length+1)//²åÈëλÖò»ºÏÀí
{
return ERROR;
}
if(ilength)
{
for(k=L->length-1;k>=i-1;k--)
{
L->data[k+1]=L->data[k];
}
}
L->data[i-1]=e;
L->length+=1;
return OK;
}

Êý×éÔªËØɾ³ý²Ù×÷


Status ListDelte(Sqlist *L, int i, ElemType *e)
{
int k;
if(L->length==0)
{
return ERROR;
}
if(i<1||i>L->length)
{
return ERROR;
}
*e=L->data[i-1];
if(ilength)
{
for(k=i-1;k<=L->length-1;k++)
{
L->data[k]=L->data[k+1];
}
}
L->length-=1;
return OK;
}

˳Ðò´æ´¢½á¹¹µÄÓÅȱµã£º


ËÄ¡¢Á´Ê½´æ´¢½á¹¹ £¨Á´±í£©??±íÖеÄÿ¸öÔªËØ°üº¬Á½¸öÄÚÈÝ£º±¾ÉíÊý¾Ý£¨´æ´¢Õâ¸öÊý¾ÝµÄµØ·½½ÐÊý¾ÝÓò£©ºÍÏÂÒ»¸öÔªËصĵØÖ·£¨´æ´¢Õâ¸öÖ¸ÕëµÄµØ·½½ÐÖ¸ÕëÓò£©£¬Ã¿¸öÔªËØÓÖ½Ð×öÁ´±íµÄ½áµã£¨Node£©¡£ÓÉÓÚÕâÑùµÄÁ´±íÀïÃæÿ¸ö½ÚµãÖ»°üº¬Ò»¸öÖ¸ÕëÓò£¬ËùÒÔÓֽе¥Á´±í¡£

Á´±íÖеÚÒ»¸ö½áµãµÄ´æ´¢Î»Ö㨲»ÊǵÚÒ»¸ö½áµãÖÐÖ¸ÕëÓòµÄÖ¸Õ룬ÊÇÖ¸ÏòµÚÒ»¸ö½áµãµÄÖ¸Õ룩½Ð×öÍ·Ö¸Õ룬×îºóÒ»¸ö½áµãÀïÃæµÄÖ¸ÕëΪ¿Õ£¨NULL£©
ÓÐʱºòΪÁËÊý¾Ý²Ù×÷·½±ã£¬»áÔÚµÚÒ»¸ö½áµã֮ǰÔÙÉèÖÃÒ»¸ö½áµã£¬Õâ¸ö½áµã½Ð×öÍ·½áµã£¬Õâ¸öÍ·½áµãµÄÊý¾ÝÓò¿ÉÒÔ²»´æ·ÅÈκÎÊý¾Ý£¬ÆäÖ¸ÕëÓò´æ·ÅÖ¸ÏòµÚÒ»¸ö½áµãµÄÖ¸Õ루ͷָÕ룩


CÓïÑÔÖпÉÓýṹÌåÖ¸ÕëÀ´±íʾµ¥Á´±í


typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef struct Node *LinkList

µ¥Á´±íµÄ¶ÁÈ¡


Status GetElem(LinkList L, int i, ElemType * e)
{
LinkList p;
p=L->next;//ÈÃpÖ¸ÏòÁ´±íLµÄµÚÒ»¸ö½áµã£¬²»ÊÇÍ·½áµã£¬Á´±íÃû³ÆL´ú±íÍ·½áµã
j=1;
while(p&&j {
p=p->next;
++j;
}
if(!p||j>i)
{
return ERROR;
}
*e=p->data;
return OK;
}

µ¥Á´±íÔªËصIJåÈë


Status ListInsert(LinkList *L, int i, ElemType e)
{
LinkList p, s;
p=(*L);//ÈÃPµÈÓÚÁ´±íLµÄÍ·½áµã
int j=1;
while(p&&j {
p=p->next;
++j;
}
if(!p||j>i)
{
return ERROR;
}
s=(LinkList)malloc(sizeof(Node))//ÔÚÄÚ´æÖпª±ÙÒ»¿éºÍNodeÊý¾ÝÀàÐÍ´óСһÑùµÄµØ·½£¬ÓÃÀ´´æ·ÅLinkListÀàÐ͵ÄÊý¾Ý£¬sΪָÏò¸ÃµØ·½µÄÖ¸Õë
s->data=e;
s->next=p->next;
p->next=s;
return Ok;
}

µ¥Á´±íÔªËصÄɾ³ý


Status ListDelte(LinkList *L, int i, ElemType *e)
{
int j=1;
LinkList p, q;
p=*L;
while(p->next&&j {
p=p->next;
++j;
}
if(!(p->next)||j>i)
{
return ERROR;
}
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;

}

µ¥Á´±íÕû±íµÄ´´½¨£¨Í·²å·¨£©


void CreateListHead(LinkList* L, int n)//ÆäÖÐnΪ´´½¨µÄÁ´±íµÄ³¤¶È
{
LinkList p;
int i;
srand(time(0))
*L=(LinkList)malloc(sizeof(Node))//½¨Á¢Ò»¸öÍ·½áµã
*L->next=NULL//Í·½áµãÊý¾ÝÓòûÓÐÊý¾Ý£¬Ö¸ÕëÓòÖ¸Ïò¿Õ,Õâ¸ö¿ÕÔÚºóÀ´²åÈëÊý¾ÝµÄʱºò£¬¸øÁ˵ÚÒ»¸öµ«×îÖÕ±äΪ×îºóÒ»¸ö½áµãµÄÖ¸ÕëÓò
for(i=0;i {
p=(LinkList)malloc(sizeof(Node));
p->data=rand()%100+1;
p->next=(*L)->next;
(*L)->next=p;
}
}

µ¥Á´±íÕû±íµÄ´´½¨£¨Î²²å·¨£©


void CreateListTail(LinkList* L, int n)
{
int i;
LinkList q,p;
srand(time(0))
*L=(LinkList)malloc(sizeof(Node));
q=*L;
for(i=0;i {
p=(LinkList)malloc(sizeof(Node))
q->next=p;
q=p;
}
q->next=NULL;
}

µ¥Á´±íÕû±íµÄɾ³ý


Status ClearList(LinkList* L)
{
LinkList p,q;
q=*L->next;//qÖ¸ÏòLµÄµÚÒ»¸ö½áµã
while(p)
{
p=q->next;
free(q);
q=p;
}
*L->next=NULL;//LµÄÍ·½áµãÖ¸ÕëÓòÇå¿Õ
return OK;
}

Á½ÖÖÏßÐÔ±íÓÅȱµã±È½Ï£º
1.Èç¹û²éÕÒ³ÌÐò¸ü¶à£¬Ó¦Ê¹ÓÃ˳Ðò´æ´¢½á¹¹£¬Èô²åÈëɾ³ý½Ï¶à£¬ÔòӦʹÓÃÁ´Ê½´æ´¢½á¹¹
2.˳Ðò´æ´¢½á¹¹ÐèÒªÔ¤ÏÈ·ÖÅäÄڴ棬ÔÚ´æ´¢Êý¾Ýʱ£¬ÔªËظöÊý»áÊÜÏÞÖÆ£¬Á´Ê½´æ´¢½á¹¹²»ÐèÒªÔ¤ÏÈ·ÖÅäÄڴ棬ԪËظöÊýÒ²²»ÊÜÏÞÖÆ


Îå¡¢¾²Ì¬Á´±í??ÓÃÊý×éÃèÊöµÄÁ´±í£¬Êý×éÔªËØͬʱ°üº¬Êý¾Ý(data)ºÍºó¼ÌÔªËصÄϱê(cur)

¾²Ì¬Á´±íµÄ´´½¨£¨½á¹¹ÌåÊý×飩


#define MAXSIZE 1000
typedef struct
{
ElemType data;
int cur;
}Component, StaticLinkList[MAXSIZE];//ºóÃæµÄ³ÌÐò¿ÉÖ±½ÓÓÃStaticLinkList¶¨Òå´óСΪ1000µÄ½á¹¹ÌåÊý×é

¾²Ì¬Á´±íµÄ³õʼ»¯£ºÊý×éµÄµÚÒ»¸öÔªËØ£¬Ò²¾ÍÊÇϱêΪ0µÄÔªËصÄcur´æ·Å±¸ÓÃÁ´±í£¨Î´±»Ê¹ÓõÄÊý×飩µÄµÚÒ»¸ö½áµãµÄϱ꣬¶øÊý×éµÄ×îºóÒ»¸öÔªËصÄcurÔò´æ·ÅµÚÒ»¸öÓÐÊý×éÔªËصÄϱê


Status InitList(StaticLinkList space)
{
int i;
for(i=0;i<=MAXSIZE-1;i++)
{
space[i].cur=i+1;
}
space[MAXSIZE-1].cur=0;
return OK;
}

¾²Ì¬Á´±íµÄmalloc¹¦ÄÜʵÏÖº¯Êý£¬Ë¼Â·ÊÇÕÒµ½µÚÒ»¸ö±¸ÓÃÁ´±í£¨¼ÙÉèΪA£©µÄλÖã¬È»ºó½«Õâ¸ö±¸ÓÃÁ´±í(A)ËùÖ¸ÏòµÄÏÂÒ»¸ö±¸ÓÃÁ´±í(B)¸ø[0].cur£¬ÕâÑùÒ»À´£¬¾ÍÏ൱ÓÚB±ä³ÉÁËA£¬¶øA¾Í±ä³ÉÁ˷DZ¸ÓÃÁ´±í£¬±íʾ±»Õ÷ÓÃÁË


int Malloc_SLL(StaticLinkList space)
{
int i=space[0].cur;
if(space[0].cur)
space[0].cur=space[i].cur;
return i;
}

¾²Ì¬Á´±í²åÈëÊý¾Ý??ÕÒµ½µÚi-1¸öÊý¾ÝËùÔÚµÄϱ꣬¸Ä±äcur¼´¿É


Status ListInsert(StaticLinkList L, int i, ElemType e)//½«e²åÔÚµÚi¸öÊý¾Ý£¬¶ø·ÇϱêiµÄµØ·½
{
int j,k,l;
k=MAXSIZE-1;//Ê×ÏȽ«kµÈÓÚ×îºóÒ»¸öÔªËصÄϱ꣬ÄÇôL[k].curµÄÖµ¾ÍÊǵÚÒ»¸öÊý¾ÝÔªËصÄϱê
if(i<1||i>ListLength(L)+1)
return ERROR;
int j=Malloc_SLL(L);//½«µÚÒ»¸ö±¸ÓÃÁ´±íµÄ¿Õ¼äÌÚ³öÀ´ÓÃÀ´´æ·ÅеÄÊý¾Ý
if(j)
{
L[j].data=e;//дÈëÊý¾Ý
for(l=1;l<=i-1;l++)//ÕÒµ½µÚi-1¸öÊý¾ÝËùÔÚµÄϱêk
k=L[k].cur;
L[j].cur=L[k].cur;//½«µÚi-1¸öÊý¾ÝÖеÄcur£¨Ö¸ÏòÔ­µÚi¸öÊý¾ÝµÄϱ꣩¸øеÄÊý¾ÝµÄcur£¬ÐµÄÊý¾Ý²åÔÚÁ˵Úi¸öÊý¾ÝµÄλÖã¬ÆäcurÖ¸ÏòÔ­µÚi¸öÊý¾Ý£¨²åÍêºó±äΪµÚi+1¸öÊý¾Ý£©µÄϱê
L[k].cur=j;//еÄÊý¾Ý±äΪµÚi¸öÊý¾Ýºó£¬°ÑÆäϱêÖµ¸øµÚi-1¸öÊý¾ÝµÄcur
return OK;
}
return ERROR;
}

¾²Ì¬Á´±íµÄfreeº¯ÊýʵÏÖ


void Free_SLL(StaticLinkList space, int k)
{
space[k].cur=space[0].cur;
space[0].cur=k
}

¾²Ì¬Á´±íµÄɾ³ý²Ù×÷£¨É¾³ýLÖеÚi¸öÔªËØ£©


Status ListDelete(StaticLinkList L, int i)
{
int j,k;
K=MAXSIZE-1;
for(j=1;j<=i-1;j++)
{
k=L[k].cur;
}
j=L[k].cur;
L[k].cur=L[j].cur;
Free_SLL(L, j);
return Ok;

}

Áù¡¢Ñ­»·Á´±í??°Ñµ¥Á´±íÖÐÖÕ½á½áµãµÄÖ¸Õë¶ËÓÉ¿ÕÖ¸Õë¸ÄΪָÏòÍ·½áµã£¬¾ÍʹÕû¸öµ¥Á´±íÐγÉÒ»¸ö»·£¬ÕâÖÖͷβÏàÁ¬µÄµ¥Á´±í³ÉΪѭ»·Á´±í¡£
Æß¡¢Ë«ÏòÁ´±í??ÔÚµ¥Á´±íµÄÿ¸ö½áµãÖУ¬ÔÙÉèÖÃÒ»¸öÖ¸ÏòÇ°Çý½áµãµÄÖ¸ÕëÓò
°Ë¡¢×ܽá


相关推荐


友情链接: