Demo entry 6683056

LT

   

Submitted by anonymous on Dec 12, 2017 at 04:49
Language: C. Code size: 17.1 kB.

/* Linear Table On Linked list */
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

/*---------page 10 on textbook ---------*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASTABLE -1
#define OVERFLOW -2

typedef int status; 
typedef int ElemType; //数据元素类型定义

/*-------page 22 on textbook -------*/
#define LIST_INIT_SIZE 100
#define LISTINCREMENT  10
typedef struct Node{  //链表(有表头节点的单向链表)结点的定义
    ElemType elem;
    struct Node * next;
}SqListNode;
struct ListChain{  //用于存放线性表的链表
    SqListNode * L;
    struct ListChain * next;
};
struct ListChain * head;
int indexOfList; // 现在选择的线性表
int amountOfList;   //线性表数量
ElemType separate=98756412; //用于在储存时分隔两个线性表
/*-----page 19 on textbook ---------*/
status IntiaList(SqListNode ** L);
status SwitchList(SqListNode ** L);
status DestroyList(SqListNode ** L);
status ClearList(SqListNode *L);
status ListEmpty(SqListNode *L);
int ListLength(SqListNode *L);
status GetElem(SqListNode *L,int i,ElemType * e);
status LocateElem(SqListNode *L,ElemType e); 
status PriorElem(SqListNode *L,ElemType cur,ElemType * pre_e);
status NextElem(SqListNode *L,ElemType cur,ElemType * next_e);
status ListInsert(SqListNode * L,int i,ElemType e);
status ListDelete(SqListNode * L,int i,ElemType * e);
status ListTrabverse(SqListNode *L);  
status LoadData(SqListNode ** L);
status SaveData(); 
/*--------------------------------------------*/
void main(void){
	head=(struct ListChain *)(malloc(sizeof(struct ListChain)));
    head->next=NULL;
  SqListNode * L=NULL;  int op=1;
  ElemType e;
  int i;
  while(op){
    system("cls");  printf("\n\n");
    printf("      Menu for Linear Table On Sequence Structure \n");
    printf("-------------------------------------------------\n");
    printf("          1. IntiaList       	 9. NextElem \n");
    printf("          2. DestroyList     	10. ListInsert\n");
    printf("          3. ClearList       	11. ListDelete\n");
    printf("          4. ListEmpty       	12. ListTrabverse\n");
    printf("          5. ListLength      	13. LoadData\n");
    printf("          6. GetElem        	14. SaveData\n");
    printf("          7. LocateElem         15. SwitchList\n");             
    printf("          8. PriorElem           0. Exit\n");                                                                   
    printf("-------------------------------------------------\n");
    printf("    请选择你的操作[0~15]:");
    scanf("%d",&op);
    switch(op){
        case 1:
            switch(IntiaList(&L)){
                case OK:
                printf("线性表创建成功!\n");
                break;
                default:
                printf("线性表创建失败!\n");
            }
         system("pause");
         break;
        case 2:
            switch(DestroyList(&L)){
                case OK:
                printf("销毁成功!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
                break;
        case 3:
            switch(ClearList(L)){
                case OK:
                printf("清空成功!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 4:
            switch(ListEmpty(L)){
                case TRUE:
                printf("是空表!\n");
                break;
                case FALSE:
                printf("不是空表!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 5:
            switch(ListLength(L)){
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
                default:
                printf("Length---%d\n",ListLength(L));
                break;
            }
            system("pause");
            break;
         case 6:
            system("cls");
            printf("请输入i:");
            scanf("%d",&i);
            switch(GetElem(L,i,&e)){
                case OK:
                printf("获取元素成功!\n");
                printf("e----%d\n",e);
                break;
                case OVERFLOW:
                printf("i超出范围! \n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 7:
            system("cls");
            printf("请输入e:");
            scanf("%d",&e);
            switch(i=LocateElem(L,e)){
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
                case 0:
                printf("未找到!\n");
                break;
                default:
                printf("位序----%d\n",i);
            }
            system("pause");
            break;
        case 8:
            system("cls");
            printf("请输入e:");
            ElemType pre_e;
            scanf("%d",&e);
            switch(PriorElem(L,e,&pre_e)){
                case OK:
                printf("已找到----%d\n",pre_e);
                break;
                case ERROR:
                printf("未找到!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 9:
            system("cls");
            printf("请输入e:");
            ElemType next_e;
            scanf("%d",&e);
            switch(NextElem(L,e,&next_e)){
                case OK:
                printf("已找到----%d\n",next_e);
                break;
                case ERROR:
                printf("未找到!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 10:
        	system("cls");
        	ListTrabverse(L);
            printf("请输入i:");
            scanf("%d",&i);
            printf("请输入e:");
            scanf("%d",&e);
            switch(ListInsert(L,i,e)){
                case OK:
                printf("插入成功");
                break;
                case OVERFLOW:
                printf("i超出范围\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 11:
        	system("cls");
        	ListTrabverse(L);
            printf("请输入i:");
            scanf("%d",&i);
            switch(ListDelete(L,i,&e)){
                case OK:
                printf("e----%d删除成功",e);
                break;
                case OVERFLOW:
                printf("i超出范围\n");
                break;
                case ERROR:
                printf("表为空!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }
            system("pause");
            break;
        case 12:
            switch(ListTrabverse(L)){
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
                case 0:
                printf("表为空!\n");
            }    
            system("pause");
            break;
        case 13:
            switch(LoadData(&L)){
                case OK:
                printf("读取成功!\n");
                break;
                case ERROR:
                printf("读取失败!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }    
            system("pause");
            break;
        case 14:
            switch(SaveData(L)){
                case OK:
                printf("储存成功!\n");
                break;
                case ERROR:
                printf("储存失败!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }    
            system("pause");
            break; 
        case 15:
        	system("cls");
            switch(SwitchList(&L)){
                case OK:
                printf("切换成功!\n");
                break;
                case ERROR:
                printf("切换失败!\n");
                break;
                case INFEASTABLE:
                printf("表不存在!\n");
                break;
            }    
            system("pause");
            break; 
        case 0:
            break;
    }//end of switch
  }//end of while
  printf("欢迎下次再使用本系统!\n");
}//end of main()
/*--------page 23 on textbook --------------------*/
status IntiaList(SqListNode *(*L)){
	struct ListChain * p=head->next;
	if(p==NULL){   //如果一个线性表都没有就创建第一个线性表
		head->next=(struct ListChain *)(malloc(sizeof(struct ListChain)));
		p=head->next;
	}
	else{  //否则移动到最后一个创建新的线性表
		while(p->next!=NULL){
			p=p->next;
		}
		p->next=(struct ListChain *)(malloc(sizeof(struct ListChain)));
		p=p->next;
	}
	p->L=(SqListNode *)(malloc(sizeof(SqListNode)));   //为线性表创建头节点
	p->next=NULL;
    (*L)=(p->L);
    (*L)->next=NULL;
    if(!*L) exit(OVERFLOW);
    amountOfList++;
    indexOfList=amountOfList;
    return OK;
}

status SwitchList(SqListNode *(*L)){
	if (amountOfList==0)
	{
		return INFEASTABLE;
	}
	struct ListChain * p=head;
	int i;
	for (i = 0; i < amountOfList; ++i)    //打印现有线性表信息
	{
		p=p->next;
		printf("%d----Length:%d\n",i+1,ListLength(p->L));
	}
	int currentindex=indexOfList;
	while(1){
		if(indexOfList>0){
			printf("现在是表%d,",currentindex);
		}
		printf("请选择表[1~%d]:",amountOfList);
		scanf("%d",&indexOfList);     //读取输入
		if (indexOfList>=1&&indexOfList<=amountOfList)
		{
			break;
		}
	}
	p=head;
	for (i = 0; i < indexOfList; ++i)  //让L指向选择的线性表
        {
        	p=p->next;
        }
    *L=(p->L);
    if(!*L) exit(ERROR);
    return OK;
}

status DestroyList(SqListNode ** L){
	struct ListChain * p=head,* temp;
	int i;
    if (*L){
        SqListNode * q;  
        q=(*L)->next;
        while(q!=NULL){ //释放表头节点后的第一个结点,并将表头节点的next指向下一个
            (*L)->next=q->next;
            free(q);
            q=(*L)->next;
        }
        free(*L);   //释放表头节点
        for (i = 0; i < indexOfList-1; ++i)
        {
        	p=p->next;
        }
        temp=p->next;
        p->next=p->next->next;
        free(temp); //释放存放这个线性表的节点
        *L=NULL;
        amountOfList--;
        indexOfList=0; 
        return OK;
    }
    else{
        return INFEASTABLE;
    }
}

status ClearList(SqListNode *L){
    if (L){
        SqListNode * q;  //释放除表头节点外的所有存储空间
        q=L->next;
        while(q!=NULL){
            L->next=q->next;
            free(q);
            q=L->next;
        }
        return OK;
    }
    else{
        return INFEASTABLE;
    }
}

status ListEmpty(SqListNode *L){
    if (L){
        if (L->next==NULL)
        {
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
    else{
        return INFEASTABLE;
    }
}
int ListLength(SqListNode *L){
    if (L){
        //遍历表的结点计算表长
        SqListNode * q;
        int cnt=0;
        q=L->next;
        while(q!=NULL){
            q=q->next;
            cnt++;
        }
        return cnt;   //返回表长
    }
    else{
        return INFEASTABLE;
    }
}
status GetElem(SqListNode *L,int i,ElemType * e){
    if (L){
        if (i>=1&&i<=ListLength(L))
        {
            SqListNode * q=L->next;
            while(--i){
                q=q->next;
            }
            *e=(q->elem);
            return OK;
        }
        else{
            return OVERFLOW;
        }
    }
    else{
        return INFEASTABLE;
    }
}
status LocateElem(SqListNode *L,ElemType e){
    if (L){
        int i;
        SqListNode * q=L->next;
        for (i = 1; q!=NULL; ++i)
        {
            if (q->elem==e)
            {
                return i;
            }
            q=q->next;
        }
        return 0;
    }
    else{
        return INFEASTABLE;
    }
    
}
status PriorElem(SqListNode *L,ElemType cur,ElemType * pre_e){
    if (L){
        SqListNode * q,* pre;
        pre=L;
        q=pre->next;
        while(q!=NULL){
            if (q->elem==cur&&pre!=L)
            {
                *pre_e=pre->elem;
                return OK;
            }
            pre=pre->next;
            q=q->next;
        }
        return ERROR;
    }
    else{
        return INFEASTABLE;
    }
}
status NextElem(SqListNode *L,ElemType cur,ElemType * next_e){
    if (L){
        SqListNode * q;
        q=L->next;
        while(q!=NULL){
            if (q->elem==cur&&q->next!=NULL)
            {
                *next_e=q->next->elem;
                return OK;
            }
            q=q->next;
        }
        return ERROR;
    }
    else{
        return INFEASTABLE;
    }
}
status ListInsert(SqListNode * L,int i,ElemType e){
    if (L){
        SqListNode * q=L,* temp;
        while(i-->0){       //判断索引是否越界(下界)
            if (q==NULL)    //判断索引是否越界(上界)
            {
                break;
            }
            if (i==0)   //到达索引制定的元素
            {
                temp=q->next;
                q->next=(SqListNode *)(malloc(sizeof(SqListNode)));
                q->next->elem=e;
                q->next->next=temp;
                return OK;
            }
            q=q->next;
        }
        return OVERFLOW;
    }
    else{
        return INFEASTABLE;
    }
}
status ListDelete(SqListNode * L,int i,ElemType * e){
    if (L){
        if (L->next!=NULL)
        {
            SqListNode * q=L,* temp;
            while(i-->0){   //判断索引是否越界(下界)
                if (q->next==NULL)  //判断索引是否越界(上界)
                {
                    break;
                }
                if (i==0)   //到达索引制定元素
                {
                    temp=q->next->next;
                    free(q->next);
                    q->next=temp;
                    return OK;
                }
                q=q->next;
             }
            return OVERFLOW;
        }
        else{
            return ERROR;
        }
    }
    else{
        return INFEASTABLE;
    }
}
status ListTrabverse(SqListNode *L){
    if (L){
    	int i=1;
        SqListNode * q=L->next;//遍历链表
        while(q!=NULL)
        {
            printf("%d----%d\n",i,q->elem);
            i++;
            q=q->next;
        }
        return ListLength(L);
    }
    else{
        return INFEASTABLE;
    }
   
}

status SaveData(){
	if (head->next!=NULL)
	{
		char filename[20]="mydata";   
		struct ListChain * p=head->next;
		FILE  *fp;  
		if ((fp=fopen(filename,"wb"))==NULL)    //创建文件
    	    {
    	        printf("File open error\n ");
    	        return ERROR;
    	    }
        SqListNode * q;
		while(p!=NULL){
            q=p->L->next;
            while(q!=NULL){ //写入一个线性表的数据
               fwrite(&(q->elem),sizeof(ElemType),1,fp);
               q=q->next; 
            }
			fwrite(&separate,sizeof(ElemType),1,fp);	//标记一个顺序表的结尾
			p=p->next;
		}
		fclose(fp);
		return OK;
	}
    else{
        return INFEASTABLE;
    }
}  

status LoadData(SqListNode ** L){
	amountOfList=0;    //重置线性表表总数
	FILE  *fp;
	char filename[20]="mydata";  
    int i;   
	if ((fp=fopen(filename,"rb"))==NULL)   //打开文件
    {
        printf("File open error\n ");
        return ERROR;
    }
	struct ListChain * p=head; //创建盛装线性表的链表
	p->next=(struct ListChain *)(malloc(sizeof(struct ListChain)));
	p=p->next;
    p->L=(SqListNode *)(malloc(sizeof(SqListNode)));
    p->L->next=NULL;
	amountOfList++;
	(*L)=(p->L);
    SqListNode * q=(*L); //q指向一个线性表的头指针
    while(1){
        q->next=(SqListNode *)(malloc(sizeof(SqListNode)));     
        if(!fread(&(q->next->elem),sizeof(ElemType),1,fp)){             //读取一个数据写到q->next
            break;
        }   
    	if (q->next->elem==separate)       //判断读到的是不是分隔标志
    	{
    		q->next=NULL;
    		p->next=(struct ListChain *)(malloc(sizeof(struct ListChain)));
    		p=p->next;
            p->L=(SqListNode *)(malloc(sizeof(SqListNode)));
            p->L->next=NULL;
    		amountOfList++;
    		(*L)=(p->L);
            q=(*L);
    	}
    	else{      
            q=q->next;
    	}   
    }
    amountOfList--; //在读到最后一个分隔符后多创建了一个节点,这里要删除这个节点
    indexOfList=amountOfList;
    p=head;
    for (i = 1; i <= amountOfList; ++i)
    {
    	p=p->next;
    }
    free(p->next->L);
    free(p->next);
    p->next=NULL;
    (*L)=(p->L);
    fclose(fp);
    return OK;
}  

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).