标题:请帮忙看一下,怎样实现那个删去功能啊,我写的那个输入与输出不符合!谢谢 ...
只看楼主
liuli01282379
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-4-5
 问题点数:0 回复次数:1 
请帮忙看一下,怎样实现那个删去功能啊,我写的那个输入与输出不符合!谢谢!
#include<stdio.h>
# define maxsize 50
 struct sqlist
 {
     int sqlist[maxsize];
    int size;
         
 };

 setnull(struct sqlist *p)  /*装入表*/
 {
     p->size=0;
 }
 
 int length(struct sqlist *p)  /*表长度*/
 {
      return(p->size);
 }
 
 get(struct sqlist *p,int i)  /*读取元素*/
 {
        if (i<1 && i>p->size)
            printf("错误");
          else
              printf("该序号对应的元素为:%d",p->sqlist[i-1]);
 }
 
 locate(struct sqlist *p,int x)  /*查找元素*/
 {  
     int i=1;
        while (i<=p->size && p->sqlist[i-1]!=x)
            i++;
            
          if (i==p->size+1 )
              printf("错误");
    else printf("该元素第一次出现的位置:%d",i);
 }
 
 insert(struct sqlist *p,int i,int x)
 {
     int j;
         if (i<1 && i>p->size+1)
        printf("位置不正确不能行插入\n");
     else
      {
        
        for (j=p->size-1;j>=i;j--)
            p->sqlist[j]=p->sqlist[j-1];
        p->sqlist[j-1]=x;
        p->size++;
      }
     for(j=1;j<=p->size;j++)
         printf("%d ",p->sqlist[j-1]);
 }
 
 void delete(struct sqlist *p,int i)
 {
     int j;
     if (i<1 || i>p->size)
        printf("错误");
     else
    {
         for (j=i;j<p->size;j++)
            p->sqlist[j-1]=p->sqlist[j];
        p->size--;
     }
     printf("此线性表为:\n");
       for (i=1;i<=p->size;i++)
        printf("%d ",p->sqlist[i-1]);
 }

 

display(struct sqlist *p)
{
    int j;
    if (p->size==0)
        printf("此表为空无法显示!");
    else
        printf("the list");
    if (p->size==1)
        printf("%d",p->sqlist[p->size-1]);
     else
     {
         for(j=1;j<=p->size;j++)
             printf("%d ",p->sqlist[j-1]);
        
     }
}

main()
{
    int i,select,a,j;int w;
    struct sqlist *p;
    struct sqlist l;
    setnull(&l);
    printf("请输入数据:\n");
     p=&l ;
      p->size=0;
      for (i=1;i<5; i++)
         {
             scanf("%d",&l.sqlist[i-1]);
             p->size++;
         }
    printf("\n");
    printf("1:置空表! \n");
      printf("2:求长度! \n");
      printf("3:读取元素! \n");
      printf("4:定位查找! \n");
      printf("5:插入! \n");
      printf("6:删除! \n");
      printf("7:显示! \n");

      printf("请选择(1----8) \n");
      scanf("%d",&select);
      switch(select)
    {
        case 1:
            setnull(&l);break;
        case 2:
            printf("chang du %d",length(&l));
            break;
        case 3:
            printf("输入序号:");
            scanf("%d",&i);
            get(&l,i);
            break;
        case 4:
            printf("输入元素:");
            scanf("%d",&i);
            locate (&l,i);
            break;
        case 5:
            printf("输入插入的元素及位置:");
            scanf("%d,%d",&i,&j);
                 insert(&l,i,j);
                 break;
        case 6:
            printf("输入所要删除数据的位置:");
                   scanf("%d",&w);
                   delete(&l,w);   
                   break;
        
        case 7:
            display(&l);  
            break;
      }
}
搜索更多相关主题的帖子: 功能 删去 输出 输入 
2008-05-19 17:18
windlzf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-8-7
得分:0 
#include<stdio.h>
#define maxsize 50

struct sqlist
{
        int sqlist[maxsize];
        int size;

};

setnull(struct sqlist *p)  /*装入表*/
{
        p->size = 0;
}

int length(struct sqlist *p)  /*表长度*/
{
        return (p->size);
}

get(struct sqlist *p, int i)  /*读取元素*/
{
        if (i < 1 || i > p->size)   /*&&->||*/
                printf("错误");
        else
                printf("该序号对应的元素为:%d", p->sqlist[i - 1]);
}

locate(struct sqlist *p, int x)  /*查找元素*/
{
        int i = 1;

        while (i <= p->size && p->sqlist[i - 1] != x)
            i++;

        if (i == p->size + 1 )
                printf("错误");
        else printf("该元素第一次出现的位置:%d", i);
}

insert(struct sqlist *p, int i, int x)
{
        int j;

        if (i < 1 || i > p->size + 1 || i > maxsize)   /*&&->||*/
                printf("位置不正确不能行插入\n");
        else
        {

                for (j = p->size;j >= i;j--)                /*note here */
                        p->sqlist[j] = p->sqlist[j - 1];

                p->sqlist[j] = x;                           /*note here*/

                p->size++;
        }

        for (j = 1;j <= p->size;j++)
                printf("%d ", p->sqlist[j - 1]);
}

void delete(struct sqlist *p, int i)
{
        int j;

        if (i < 1 || i > p->size)
                printf("错误");
        else
        {
                for (j = i;j < p->size;j++)
                        p->sqlist[j - 1] = p->sqlist[j];

                p->size--;
        }

        printf("此线性表为:\n");

        for (i = 1;i <= p->size;i++)
                printf("%d ", p->sqlist[i - 1]);
}



display(struct sqlist *p)
{
        int j;

        if (p->size == 0)
                printf("此表为空无法显示!");
        else
                printf("the list");

        if (p->size == 1)
                printf("%d", p->sqlist[p->size - 1]);
        else
        {
                for (j = 1;j <= p->size;j++)
                        printf("%d ", p->sqlist[j - 1]);

        }
}

main()
{
        int i, select, a, j;
        int w;

        struct sqlist *p;

        struct sqlist l;
        setnull(&l);
        printf("请输入数据:\n");
        p = &l ;
        p->size = 0;

        for (i = 1;i < 5; i++)
        {
                scanf("%d", &l.sqlist[i - 1]);
                p->size++;
        }

        printf("\n");
        printf("1:置空表! \n");
        printf("2:求长度! \n");
        printf("3:读取元素! \n");
        printf("4:定位查找! \n");
        printf("5:插入! \n");
        printf("6:删除! \n");
        printf("7:显示! \n");

        printf("请选择(1----8) \n");
        scanf("%d", &select);

        switch (select)
        {
                case 1:
                setnull(&l);break;
                case 2:
                printf("chang du %d", length(&l));
            break;
                case 3:
                printf("输入序号:");
                scanf("%d", &i);

                get(&l, i);

            break;

                case 4:
                printf("输入元素:");

                scanf("%d", &i);

                locate (&l, i);

            break;

                case 5:
                printf("输入插入的元素及位置:");

                scanf("%d,%d", &i, &j);

                insert(&l, i, j);

            break;

                case 6:
                printf("输入所要删除数据的位置:");

                scanf("%d", &w);

                delete(&l, w);

            break;

                case 7:
                display(&l);

            break;
        }
        getch();
}
2008-05-20 09:31



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




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

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