文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>《入式系统软件设计中的数据结构》学习笔记---1...

《入式系统软件设计中的数据结构》学习笔记---1...

时间:2010-09-25  来源:huazaicola

顺序表 1.申请表空间 2.输入数据 3.插入数据 4.删除数据  

//在程序没有调通时,不要复制,否则复制的地方都出错
#include <stdio.h>
#include <stdlib.h>
#include "link.h"

void main(void)
{
    sequentlist *L;
    int ch,n,i;
    while(1)
    {
        printf("\n please choise:\n");
        printf("1.input list\n");
        printf("2.insert list\n");
        printf("3.del list\n");
        printf("4.end\n");
        
        ch = getch();
        switch(ch)
        {
        case '1':
            printf("input the num of data:");
            scanf("%d", &n);
            printf("\n");
            L = (sequentlist *)malloc(sizeof(sequentlist));    
            for(i = 0; i < n; i++)
            {
                printf("输入第%d个数:\n",i);
                scanf("%d", &L->data[i]);
            }
            L->last = n -1;
            printf("输入的数据表为:\n");
            for(i = 0; i <= L->last ; i++)
            {
                printf("data[%d] = %d\n", i, L->data[i]);
            }
            break;
        case '2':
            printf("\n输入要插入数据的位置:");
            scanf("%d", &i);
            printf("\n输入要插入的数据:");
            scanf("%d", &n);
            printf("\n");
            if(insert(L,n,i))
            {
                printf("\n插入后的顺序表为:\n");
                for(i = 0; i <= L->last; i++)
                {
                    printf("data[%d] = %d\n", i, L->data[i]);
                }
            }
            break;
        case '3':
            printf("\n输入要删除数据的位置:");
            scanf("%d", &i);
            printf("\n");
            if(del(L,i))
            {
                printf("\n删除后的顺序表为:\n");
                for(i = 0; i <= L->last; i++)
                {
                    printf("data[%d] = %d\n", i, L->data[i]);
                }
            }
            break;
        default:
            printf("no\n");break;

        }
    }
    return;
}
//插入数到位置i

int insert(sequentlist *L, datatype x, int i)
{
    int j;
    printf("%d",x);
    if(L->last > maxsize - 1)
        printf("no enough size\n");
    else if(i < 1 || i >L->last + 2)    //分清楚下标i-1和位置i

        printf("illeague insert\n");
    else
    {
        for(j = L->last; j >= i - 1; j--)
        {
            L->data[j + 1] = L->data[j];
        }
        L->data[i - 1] = x;
        printf("\n%d\n",L->data[i-1]);
        L->last += 1;
    }
    return 1;
}
//将位置i处的数删除

int del(sequentlist *L, int i)
{
    int j;
    if(i < 1 || i > L->last+1)
    {
        printf("illeague del\n");
        return 0;
    }
    else
    {
        for(j = i; j <= L->last; j++)
        {
            L->data[j-1] = L->data[j];
        }
        L->last = L->last - 1;
    }
    return 1;
}

头文件


 

#define maxsize 1024
typedef int datatype;

typedef struct
{
    datatype data[maxsize];
    
    int last;
}sequentlist;

int insert(sequentlist *L, datatype x, int i);
int del(sequentlist *L, int i);


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载