Chinaunix首页 | 论坛 | 博客
  • 博客访问: 651614
  • 博文数量: 128
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 1546
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-22 14:05
文章分类

全部博文(128)

文章存档

2012年(2)

2011年(51)

2010年(75)

分类: C/C++

2010-09-25 17:54:33

顺序表
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);


阅读(1484) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~