标题:线性表的运用,求大神讲解讲解
只看楼主
liyue6822532
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2016-2-28
结帖率:57.14%
已结贴  问题点数:20 回复次数:7 
线性表的运用,求大神讲解讲解
请你定义一个线性表,可以对表进行“在某个位置之前插入一个元素”、“删除某个位置的元素”、“清除所有元素”、“获取某个位置的元素”等操作。键盘输入一些命令,可以执行上述操作。本题中,线性表元素为整数,线性表的第一个元素位置为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
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
很好的鍛煉題目,最好自己先思考一下。

授人以渔,不授人以鱼。
2016-03-27 17:32
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
看樓主的發帖歷史,都是數據結構和算法的,這個題目應該可以應付吧?

授人以渔,不授人以鱼。
2016-03-27 18:04
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
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
你好像没明白题目要求做出来的效果

授人以渔,不授人以鱼。
2016-03-27 20:35
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
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:0 
你再仔细琢磨一下程序需求,不急写代码。没那么简单的代码,而且有至少两种以上的实现方法。

授人以渔,不授人以鱼。
2016-03-27 22:13
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:10 
你写的不好看懂,起码有的地方是不对的。最好用函数的方法,清晰明白。
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10

struct node
{
    int data;
    struct node* next;
};

//创建一个线性表
struct node* create()
{
    struct node* p;
    struct node* q;
    struct node* head;
    
    q=(struct node*)malloc(sizeof(struct node));
    q->data=1;
    head=q;
    
    int i=1;
    for(;i<N;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=i+1;
        
        q->next=p;
        q=p;
    }

    /*****/
    p=(struct node*)malloc(sizeof(struct node));
    p->data=-1;
    p->next=NULL;
    q->next=p;
    /*****/
    
    return head;
}

void insertItem(struct node* head,int position,int i)
{
    struct node* p;
    struct node* q;
    struct node* tmp=(struct node*)malloc(sizeof(struct node));
    tmp->data=i;
    //插入位置在头节点
    if(position==1)
    {
        tmp->next=head;
        head=tmp;  //重新设定头节点
        return;
    }
    //找到插入位置
    int j=1;
    for(p=head;j<position;j++)
    {
        q=p;  //新节点前一个节点
        p=p->next;  //新节点的后一个节点
    }
    
    //插入,注意前后连接
    q->next=tmp;
    tmp->next=p;
}

void deleteItem(struct node* head,int position)
{
    struct node* p;
    struct node* q;
    //删除头节点
    if(position==1)
    {
        printf("%d\n",head->data);
        head=head->next;
        return;        
    }
    
    int j=1;
    for(p=head;j<position;j++)
    {
        q=p;
        p=p->next;
    }
    printf("%d\n",p->data);
    q->next=p->next;  //跳过一个节点,即删除该节点
}

void getElem(struct node* head,int position)
{
    struct node* p;
    
    if(position==1)
    {
        printf("%d\n",head->data);
        return;
    }
    
    int j=1;
    for(p=head;j<position;j++)
    {
        p=p->next;
    }
    
    printf("%d\n",p->data);
}

//注意clean之后不可再进行删除插入之类的操作
void clean(struct node* head)
{
    struct node* p=head;
    struct node* q;
    
    while(p->next!=NULL)
    {
        q=p;
        p=p->next;
        free(q);
    }
    free(p);
    head=NULL;
}

int main()
{
    //自己再想想流程怎么写。
    return 0;
}


   唯实惟新 至诚致志
2016-03-27 22:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-462984-1-1.html




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

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