From 1df9091936cf52e0324c080e8eea772d05f446aa Mon Sep 17 00:00:00 2001 From: zhuhao <1740087730@qq.com> Date: Sat, 28 Oct 2023 17:09:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 链表.cpp | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 链表.cpp diff --git a/链表.cpp b/链表.cpp new file mode 100644 index 0000000..324f8cc --- /dev/null +++ b/链表.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; +#include + +#define OK 1 +#define ERROR 0 + +typedef int status; +typedef int ElemType; + +//ÓýṹÌ嶨ÒåÒ»¸öµ¥Á´±í +typedef struct LNode +{ + ElemType data; //´æ´¢Á´±íµÄÊý¾ÝÔªËØ + struct LNode *next; //¶¨ÒåÒ»¸ö½á¹¹ÌåÖ¸ÕëÓÃÀ´Ö¸ÏòÏÂÒ»¸öÁ´±í½áµãµÄµØÖ· +}LNode,*Linklist; //ÕâÀﶨÒåµÄ Linklist ==LNode *; + +//---------µ¥Á´±íµÄ»ù±¾²Ù×÷------------ +//Ò»¡¢µ¥Á´±íµÄ³õʼ»¯ÒÔ¼°ÕýÐòÊäÈë +status Initlist(Linklist &L,int n) +{ + Linklist q,p; + L=(Linklist)malloc(sizeof(LNode)); //Ϊµ¥Á´±í·ÖÅäÍ·½áµã£¬µ«×¢ÒâÍ·½áµã²»ÊÇÁ´±íµÄµÚÒ»¸ö½áµã + L->next=NULL; //Í·½áµãµÄÖ¸ÕëÓòÖ¸ÏòµÚÒ»¸ö½áµãµÄµØÖ· + q=L; //ÈÃqÖ¸ÏòÁ´±íµÄβָÕ룻 + cout<<"ÇëÊäÈëµ¥Á´±íµÄÔªËØ£º"<>p->data; + q->next=p; //½«qµÄÖ¸ÕëÓòÖ¸Ïòp + q=p; //ÈÃp³ÉΪеÄq + } + q->next=NULL; + return OK; + } + +//¶þ¡¢µ¥Á´±íµÄ²åÈë +status Insertlist(Linklist &L,int i,ElemType e) +{ + Linklist p,s; + int j=0; + p=L; + while(p&&jnext; + j++; + } //´ËwhileÑ­»·ÓÃÓÚ²éÕÒ²åÈëλÖõÄÇ°Ò»¸ö½áµã + if(!p||j>i-1) + return ERROR; //´ËifÓï¾äÅжϲéÕÒÊÇ·ñʧ°Ü + s=(Linklist)malloc(sizeof(LNode)); //Ϊ²åÈëµÄÊý¾Ý·ÖÅäÒ»¸ö½áµã + if(!s) + return ERROR; + s->data=e; + s->next=p->next; //½«pµÄÖ¸ÕëÓò´«¸øsµÄÖ¸ÕëÓò + p->next=s; //È»ºó½«pµÄÖ¸ÕëÓòÖ¸Ïòs½áµã + return OK; + } + +//Èý¡¢µ¥Á´±íµÄɾ³ý +status Deletelist(Linklist &L,int i,ElemType &e) +{ + Linklist p,q; + int j=0; + p=L; + while(p&&jnext; + j++; + } + if(!(p->next)||j>i-1) //µ±Èç¹ûpµÄÏÂÒ»¸ö½áµãΪ¿Õʱ£¬Ôòɾ³ý´íÎó + return ERROR; + q=p->next; //½«ÒªÉ¾³ýµÄ½áµãÓÃqÖ¸Ïò + e=q->data; //ÓÃe·µ»Øɾ³ý½áµãµÄÔªËØ + p->next=q->next; //½«qµÄÖ¸ÕëÓò´«¸øp; + free(q); + return OK; +} + +//ËÄ¡¢µ¥Á´±íµÄ²éÕÒ +status GetElem(Linklist L,int e,int &i) +{ + Linklist p=L->next; + if(p==NULL) + return ERROR; + i=1; + while(p) + { + if(p->data==e) + { + return i; //Èç¹ûÔÚÁ´±íÖÐÕÒµ½¸ÃÔªËØ£¬ÔòÊä³öÆäλÐò + } + p=p->next; //·ñÔò¼ÌÐøÏÂÒƲéÕÒ + i++; + } + } + +//Îå¡¢µ¥Á´±íµÄÊä³ö +status Printf(Linklist L) +{ + Linklist p; + p=L->next; //ÈÃp±íʾµ¥Á´±íµÄµÚÒ»¸ö½áµã + while(p) + { + cout<data<<" "; + p=p->next; //Êä³öµÚÒ»¸ö½áµãÊý¾Ýºó£¬pºóÒÆһλ + } + cout<next; //¶øqµÄ×÷ÓÃÒ²ÊÇÓëpÀàËÆ¡£ + L->next=NULL; //½«Í·½áµãÓëµÚÒ»¸ö½áµãµÄÁ´½Ó¶Ï¿ª + while(p) + { + q=p->next; //qÊÇΪÁËÖ¸ÏòÊ£ÓàδÄæÖõÄÔªËصØÖ· + p->next=L->next; + L->next=p; //ÒÔÉÏÁ½ÌõÓï¾äÊǽ«µÚÒ»¸ö½áµãÄæÖòåÈë + p=q; //È»ºó½«q¸³¸øp; + } + return OK; +} + +//µ¥Á´±íµÄÇå¿Õ +status Clearlist(Linklist &L) +{ + Linklist p,q; + p=L->next; + while(p) + { + p=q; + q=p->next; //ÉèÁ¢Ò»¸öqÖ¸ÏòʣϵĽáµã£¬Ö®ºóÒ»¸öÒ»¸öÇå¿Õ¡£ + free(p); + } + L->next=NULL; + cout<<"´ËÁ´±íÒÑÇå¿Õ"<next; + free(p); + } + cout<<"´ËÁ´±íÒÑÏú»Ù¡£"<>n; + Initlist(L,n); + cout<<"µ¥Á´±íΪ£º"<>i>>e; + Insertlist(L,i,e); + cout<<"²åÈëÔªËغóµ¥Á´±íΪ£º"<>i; + Deletelist(L,i,e); + cout<<"ɾ³ýµÄÔªËØΪ£º"<>e; + GetElem(L,e,i); + cout<