데이터 구조 실험 1

4132 단어 데이터 구조
실험 1:
#include         //   :      
#include
#define MAXSIZE 9
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct Sqlist{
    ElemType data[MAXSIZE];
    int length;
    Sqlist(){
        length=0;
    }
}Sqlist;
Status ListShow(Sqlist &L);
/*      ——  */
Status ListClear(Sqlist &L){
    memset(L.data,0,sizeof(L.data));
    L.length=0;
    return OK;
}
/*            ——  */
Status GetElem(const Sqlist &L,const int &i,ElemType &e){
    if(L.length==0||i<1||i>L.length){
        printf("   :   %d,   。
",i); return ERROR; } e=L.data[i-1]; return OK; } /* —— */ Status ListInsert(Sqlist &L,int i,ElemType e){ int k,len=L.length,temp; if(len>=MAXSIZE){ printf(" :

"); return ERROR; }else if(i<1||i>len+1){ printf(" : %d ,

",i); return ERROR; }else if(i<=len){ for(k=len-1;k>=i-1;k--){ L.data[k+1]=L.data[k]; } } L.data[i-1]=e; L.length++; printf(" :%d , :%d ,

",i,e); /* */ ListShow(L); printf("
"); return TRUE; } /* */ Status ListDelete(Sqlist &L,int i,ElemType &e){ int k; if(L.length==0){ printf("
"); return ERROR; } if(i<1||i>L.length){ printf(" : :%d
",i); return ERROR; } e=L.data[i-1]; if(i
#include
#include
#include
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType ;
typedef struct Node {
    ElemType data;
    struct Node *next;
}Node;
typedef struct Node * LinkList;
/*
    ,                  。
         。
*/
Status GetElem(LinkList head,int i,ElemType &e)
{
    int j=1;
    LinkList p;
    p=head->next;
    while(p&&jnext;
        j++;
    }
    if(!p||idata;
    printf("    !!!
"); return OK; } /* : , 。 。 */ Status ListInsert(LinkList *head,int i,ElemType e){ int j=1; LinkList p=*head; while(p&&jnext; j++; } if(!p||idata=e; s->next=p->next; p->next=s; printf(" !!!
"); return OK; } Status ListDelete(LinkList &L,int i,ElemType &e){ int j=0; LinkList p=L; while( (p->next)&&(jnext; j++; } if( (!p) || (j>i-1) ){ printf(" !!!
:%d
",i); return ERROR; } LinkList q=p->next; p->next=q->next; e=q->data; free(q); printf(" : :%d :%d
",i,e); return OK; } Status ListShow(LinkList &L){ LinkList p=L->next; if(!(p->next)){ printf("
"); return ERROR; } while(p->next){ printf("%d ",p->data); p=p->next; } printf("
"); return OK; } int main() { int e; LinkList head =(LinkList)malloc(sizeof(Node)); LinkList s1=(LinkList)malloc(sizeof(Node)); s1->data=4; s1->next=NULL; head->next=s1; ListInsert(&head,1,2); ListInsert(&head,2,3); ListInsert(&head,2,7); ListInsert(&head,3,5); GetElem(head,3,e); printf(" : :%d :%d
",3,e); ListDelete(head,2,e); return 0; }

좋은 웹페이지 즐겨찾기