線性表的鏈式儲存方式
阿新 • • 發佈:2018-11-10
#include<bits/stdc++.h> #define ElemType int #define Status int #define INIt_SIZE 100 #define OK 1; #define ERROR 0; using namespace std; typedef struct lnode{ ElemType data; struct lnode *next; }lnode,*linklist;//*linklistΪָÏòlnodeÀàÐ͵ÄÖ¸Õ룬¶¨ÒåÁ´±íÍ·Ö¸Õë //linklist ÒѾ´ú±íÖ»Ö¸ÕëÁË¡£ Status initlist(linklist &L){ L = (lnode *)malloc(sizeof(lnode)); if(L == NULL) { printf("´æ´¢·ÖÅäʧ°Ü£¡\n"); return ERROR; } L->next = NULL; return OK; } /* µ¥Á´±íµÄ´´½¨£¬´Ë´¦Îªtail²å·¨ */ Status createlist(linklist &L) { if(L == NULL) // initlist(L); linklist p,tial; tial = L; printf("ÇëÊäÈëÏëÒª´´½¨Á´±íµÄ³¤¶È(ĬÈϳ¤¶ÈΪ5)£º\n"); //scanf("%d",&n); int n = 5; int a[]={0,1,2,3,4,5}; for(int i = 1;i <=n; ++i) { p = (linklist) malloc(sizeof(lnode)); //scanf("%d",&(p->data));//´Ë´¦Ä¬ÈÏElemTypeΪintÐÍ p->data = a[i]; p->next = NULL; tial->next = p; //²åÈëµ½±íÍ·¡£ tial = p; //β½áµãÖ¸Ïòд´½¨µÄ½áµã£» } return OK; } Status emptylist(linklist L) { if(L->next == NULL) { printf("This linked list is empty\n"); return OK; } else { printf("This linked list not is empty\n"); return ERROR; } } int getlength(linklist L) { linklist p = L->next; int length = 0; while(p) { p = p->next; ++length; } return length; } /* insert e at i-th position ; */ Status insert(linklist &L,int i,ElemType e) { linklist p = L; int it = 0;// while(p && it < i-1) //ÕÒµ½iµÄÇ°Ò»¸ö½áµã Õý³£Í˳öʱpÖ¸Ïòi-1 { p = p->next; ++it; } if(!p || it > i-1)//ͬʱÅжÏiÖµÊÇ·ñºÏ·¨£¬it > i-1 ¼´i <= 0 return ERROR; linklist nd = (linklist) malloc(sizeof(lnode)); nd->data = e; nd->next = p->next; p->next = nd; return OK; } /* ½«Á´±íÖû»Îª¿Õ±í ±£ÁôÍ·½áµã */ Status clearlist(linklist &L) { if(emptylist(L)) return ERROR; linklist p = L->next,s; while(p) { s = p->next; free(p); p = s; } L->next = NULL;//ÈÝÒ×Íü¼Ç return OK; } /* ´Ý»ÙÁ´±í */ Status destorylist(linklist &L) { if(!emptylist(L)) clearlist(L); free(L); return OK; } /* ɾ³ýÖ¸¶¨Î»ÖÃÔªËØ£¬²¢½«¸ÃÔªËØÖµ¸³¸øe´ø»Ø¡£ */ Status deletelist(linklist &L,int i,ElemType e) { if(i < 1 || i > getlength(L)) return ERROR; linklist p = L; int it = 0; while(p && it < i-1) { p = p->next; ++it; } if(!p || it > i-1) return ERROR; linklist s = p->next; //p->next = (p->next)->next; p->next = s->next; free(s);//ÊÍ·Å¿Õ¼ä } /* */ Status getelem(linklist L,int i,ElemType &e) { linklist p = L;//L±È½ÏºÃ Èç¹ûLÊÇ¿Õ int it = 0; //J count number; while(p && it < i){ p = p->next; ++it; } if(!p || it > i)//i==0 !p==1 return ERROR; e = p->data; return OK; } Status printlist(linklist L) { int length = getlength(L); linklist p = L->next; printf("Á´±íÄÚÔªËØΪ£º"); for(int i = 1;i <= length; ++i) { printf("%d ",p->data); p = p->next; } printf("\n"); } /* i ´ú±íλÐò */ Status priorelem(linklist L,int i,int &e){ linklist p = L; int cnt = 0; while(p && cnt < i-1) { p = p->next; ++cnt; } if(i <= 1 || !p) return ERROR; e = p->data; return OK; } /* Á´±í×îºóÒ»¸ö½áµãûÓÐÖ±½Óºó¼Ì Ö±½ÓÅпռ´¿É */ nextelem(linklist L,int i,int &e) { linklist p = L; int cnt = 0; while(p && cnt < i +1) // i 1 -n-1 { p = p->next; ++cnt; } if(!p || i < 1) return ERROR; e = p->data; return OK; } int main() { linklist L ; if(initlist(L)) printf("Á´±í´´½¨³É¹¦£¡\n"); else printf("Á´±í´´½¨Ê§°Ü!\n"); emptylist(L); printf("Ä¿Ç°Á´±íµÄ³¤¶ÈΪ %d\n",getlength(L)); createlist(L); printlist(L); int e = 6; insert(L,5,e); printf("insert length = %d\n",getlength(L)); printlist(L); emptylist(L); deletelist(L,2,e); printf("delete length = %d\n",getlength(L)); printlist(L); int pe1=-1000,pe2=-1000; priorelem(L,2,pe1); priorelem(L,5,pe2); printf("Prior of 2-th =%d Prior of 5-th =%d \n",pe1,pe2); int ne1=-1000,ne2=-1000; nextelem(L,1,ne1); nextelem(L,4,ne2); printf("next of 1-th=%d next of 4-th=%d \n",ne1,ne2); clearlist(L); emptylist(L); printf("clear length=%d\n",getlength(L)); destorylist(L); return 0; }