小米笔记 - 坏记性不如烂笔头

C - 顺序表常见操作

代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100

typedef struct {
    int data[MAXLEN]; //数据 
    int length; //表长 
}SeqList;

//创建与初始化-置空表    检索   插入  删除  求表长
// ArrayList  LinkedList 
SeqList * createList(SeqList *);
SeqList * initList(SeqList  *);
int searchList(SeqList *,int );
SeqList * insertList(SeqList*,int,int);
SeqList * deleteList(SeqList *,int);
int getLength(SeqList *);
void displayList(SeqList *);

//删除所有值为value的元素 
//创建 
 SeqList * createList(SeqList    *list){
        list = (SeqList *)malloc(sizeof(SeqList));
        printf("\n创建完毕");
        return list;
}
//初始化
SeqList * initList(SeqList  *list){
    int i;
    if(list == NULL)
    {
        printf("\n表无空间,请先创建表");
        return NULL;
    }
    for(i = 0;i < MAXLEN;i++) 
    {
        list->data[i] =  -99999;
    }
    list->length = 0;
    printf("\n初始化完毕");
    return list;
} 

//输出
 void displayList(SeqList *list){
     int i;
     if(list == NULL)
     {
        printf("\n表无空间,请先创建并初始化");
         return ;
     }
     for(i = 0;i < list->length;i++)
     {
        printf("%d\t",list->data[i]);         
     }
     printf("\n输出完毕");
     return ;
}
//求表长
int getLength(SeqList *list){
    if(list == NULL)
    {
        printf("\n表无空间,请先创建并初始化");
        return -1;
    }
    return list->length;        
} 

//插入
SeqList * insertList(SeqList *list,int pos,int value)
{
    int i;
     //先判断List是否有空间?
     if(list == NULL) 
     {
         printf("\n无空间,请先创建并初始化表");
         return NULL;
     }
     //判断List满?
     if(list->length == MAXLEN)
     {
         printf("\n空间已满,无法插入");
         return list;
     }
     //判断插入位置是否合法?[]
     if(pos < 1 || pos > list->length + 1)
     {
         printf("\n插入位置不合法");    
         return list;
     }
     //移动元素【pos,length】 ,从后往前 
    for(i = list->length ;i >= pos;i--) 
    {
        list->data[i] = list->data[i-1];
    }
     //插入()赋值 
     list->data[pos-1] = value;
     //长度+1 
     list->length++;
         
     printf("\n插入成功");    
    return list;        
} 
//删除 
SeqList * deleteList(SeqList *list ,int pos){
    int i;
    if(list == NULL)    
    {
        printf("\n表无空间,请先创建并初始化");
        return NULL;
    }
    if(list->length == 0)
    {
        printf("\n表数据为空,不能删除");
        return list;
    }
    if(pos < 1 || pos > list->length)
    {
        printf("\n删除位置不合法");
        return list;
    }
    for(i = pos;i < list->length;i++)
    {
        list->data[i-1]     = list->data[i];
    }
    list->length--;
    
    printf("\n删除成功");
    return list;
} 
//查找 
int searchList(SeqList *list,int value)
{
    int i;
    if(list == NULL)    
    {
        printf("\n表无空间,请先创建并初始化");
        return -1;
    }
    if(list->length == 0)
    {
        printf("\n表为空,无结果");
        return -1;
    }
    for(i = 0;i < list->length;i++){
        if(list->data[i] == value)
        {
            return i;
        }
    }
    return -1;
}    
int main(int argc, char *argv[])
{
    int choice;
    int length;
    int pos;
    int index;
    int value;
    SeqList *list = NULL;
        
    while(1)
    {
        printf("\n-------顺序表--------\n");    
        printf("\n-------1.创建表------\n");    
         printf("\n-------2.初始化------\n");    
        printf("\n-------3.求表长------\n");    
        printf("\n-------4.插入--------\n");    
        printf("\n-------5.删除--------\n");    
        printf("\n-------6.查找--------\n");    
        printf("\n-------7.输出表------\n");    
        printf("\n-------0.退出--------\n");    
        
        printf("\n您的选择是:");
        scanf("%d",&choice);        
        switch(choice)
        {
            case 1:
                    list = createList(list);
                    break;    
            case 2:    list = initList(list);
                    break;
            case 3:    length = getLength(list);
                    if(length == -1)
                    {
                        printf("\n表不存在");
                    }
                    else{
                        printf("\n表长为:%d",length);
                    }
                    break;
            case 4:    
                    if(list != NULL)
                    {
                        printf("\n插入位置范围是:");
                        if(list->length == 0)
                            printf("1~1");
                        else
                            printf("%d~%d",1,list->length+1);    
                    }
                    printf("\n请输入插入的位置:");    
                    scanf("%d",&pos);
                    printf("\n请输入插入的值:");
                    scanf("%d",&value);
                    
                    list = insertList(list,pos,value);
                
                    break;
            case 5:    
                    if(list !=NULL)
                    {
                        printf("\n可删除的元素范围为:");
                        printf("1~%d",list->length);
                    }
                    printf("\n请输入要删除的元素位置:");
                    scanf("%d",&pos); 
                    
                    list = deleteList(list,pos);                    
                    break;
            case 6:    
                    printf("\n请输入要查找的值:");
                    scanf("%d",&value); 
                    index = searchList(list,value);
                    if(index == -1)
                    {
                        printf("\n查找失败。");
                    }
                    else
                    {
                        printf("\n查找成功,该元素在下标为%d的位置",index);
                    }
                    break;
            case 7:    displayList(list);
                    break;
            case 0:    printf("\n感谢使用.\n");
                    return 0;
            default:
                printf("\n您的选择有误,请重新选择");
        }
    }
    
    return 0;
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »