| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1788 人关注过本帖
标题:线性表的运用,求大神讲解讲解
取消只看楼主 加入收藏
liyue6822532
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-2-28
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:2 
线性表的运用,求大神讲解讲解
请你定义一个线性表,可以对表进行“在某个位置之前插入一个元素”、“删除某个位置的元素”、“清除所有元素”、“获取某个位置的元素”等操作。键盘输入一些命令,可以执行上述操作。本题中,线性表元素为整数,线性表的第一个元素位置为1。线性表的最大长度为1000。
输入

各个命令以及相关数据,它们对应的格式如下:

在某个位置之前插入操作:insert,接下来的一行是插入的组数n,下面是n行数据,每行数据有两个值,分别代表位置与插入的元素值

清除线性表:clear

获取某个位置的元素:getelem,接下来一行是需要获取的元素位置

删除某个位置的元素:delete,接下来一行是被删除的元素位置

当输入的命令为exit时,程序结束
输出

当输入的命令为getelem时,请输出获取的元素值,

当输入的命令是delete时,请输出被删除的那个元素值

注意,所有的元素均占一行
样例输入

insert
2
1 1
2 2
delete
1
clear
insert
2
1 3
2 4
getelem
2
exit

样例输出

1
4
搜索更多相关主题的帖子: insert 线性表 键盘 元素 
2016-03-27 15:20
liyue6822532
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-2-28
收藏
得分:0 
回复 3楼 TonyDeng
我都是一点一点学过来的 自己写了下 但是没写正确
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
int main()
{
    struct node *head, *p, *q, *t,*t1,*t2,*temp;
    int i, j, n, a,b;
    char x[10] = "insert";
    char y[10] = "delete";
    char k[10] = "clear";
    char z[15] = "getelem";
    char z1[10] = "exit";
    char h[100];
    head = NULL;
    while (gets_s(h))
    {
        if(strcmp(h, x) == 0)
        {
            scanf_s("%d", &n);
            for (i = 1; i <= n; i++)
            {
                scanf_s("%d %d", &a, &b);
                p = (struct node*)malloc(sizeof(struct node*));
                p->data = b;
                p->next = NULL;
                if (head == NULL)
                {
                    head = p;
                }
                else
                {
                    q->next = p;
                }
                q = p;
            }
        }
        if (strcmp(h, y) == 0)
        {
            i = 1;
            t = head;
            scanf_s("%d", &n);
            while (t != NULL)
            {
                t = t->next;
                i++;
                if (i+1 == n)
                {
                    b = t->next->data;
                    temp = t->next;
                    t = temp;
                    free(temp);
                    printf("%d\n", b);
                }
            }
        }
        if (strcmp(h, k) == 0)
        {
            head = NULL;
        }
        if (strcmp(h, z) == 0)
        {
            scanf_s("%d", &n);
            t = head;
            i = 1;
            while (head != NULL)
            {
                t = t->next;
                if (i == n)
                {
                    printf("%d\n", t->data);
                }
            }
        }
        if (strcmp(h, z1) == 0)
        {
            break;
        }
    }
}
2016-03-27 20:28
liyue6822532
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-2-28
收藏
得分:0 
回复 5楼 TonyDeng
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
int main()
{
    struct node *head, *p, *q, *t,*t1,*t2,*temp;
    int i, j, n, a,b,flag=1,j1;
    char x[10] = "insert";
    char y[10] = "delete";
    char k[10] = "clear";
    char z[15] = "getelem";
    char z1[10] = "exit";
    char h[1000];
    head = NULL;
    while (gets_s(h))
    {
        if(strcmp(h, x) == 0)
        {
            scanf_s("%d", &n);
            if (head == NULL)
            {
                for (i = 1; i <= n; i++)
                {
                    scanf_s("%d %d", &a, &b);
                    p = (struct node*)malloc(sizeof(struct node*));
                    p->data = b;
                    p->next = NULL;
                    if (head == NULL)
                    {
                        head = p;
                    }
                    else
                    {
                        q->next = p;
                    }
                    q = p;
                }
            }
            else
            {
                for (j = 1; j <= n;j++)
                {
                    scanf_s("%d %d", &a, &b);
                    t = head;
                    for (j1 = 1; j1 <= a; j1++)
                    {
                        if (j1 == a)
                        {
                            p = (struct node*)malloc(sizeof(struct node*));
                            p->data = b;
                            p->next = t->next;
                            t->next = p;
                            break;
                        }
                        t = t->next;
                    }
                }
            }
        }
        if (strcmp(h, y) == 0)
        {
            i = 1;
            t = head;
            scanf_s("%d", &n);
            if (n == 1)
            {
                printf("%d\n", t->data);
                flag = 0;
            }
            if (flag)
            {
                while (t != NULL)
                {
                    t = t->next;
                    i++;
                    if (i + 1 == n)
                    {
                        b = t->next->data;
                        temp = t->next;
                        t = temp;
                        free(temp);
                        printf("%d\n", b);
                    }
                }
            }
        }
        if (strcmp(h, k) == 0)
        {
            head = NULL;
        }
        if (strcmp(h, z) == 0)
        {
            scanf_s("%d", &n);
            t = head;
            i = 1;
            while (t != NULL)
            {
                t = t->next;
                i++;
                if (i == n)
                {
                    printf("%d\n", t->data);
                }
            }
        }
        if (strcmp(h, z1) == 0)
        {
            break;
        }
    }
}


这是改完之后的
2016-03-27 21:29
快速回复:线性表的运用,求大神讲解讲解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050504 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved