| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2492 人关注过本帖, 1 人收藏
标题:线性表顺序存储结构的插入和删除,程序有点问题
只看楼主 加入收藏
zcs302567601
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-9-21
结帖率:100%
收藏(1)
已结贴  问题点数:10 回复次数:4 
线性表顺序存储结构的插入和删除,程序有点问题
程序有点问题,请各位看看,指点,指点
#include <stdio.h>
#include <stdlib.h>
#define Listinitsize 100
#define Listincrement 10
typedef struct {
    int *elem;
    int length;
    int listsize;
}Sqlist;
int InitList_Sq(Sqlist *L)
{
    L->elem=(int *)malloc((Listinitsize) * sizeof(int));
    if(! L->elem)return(-1);
    L->length=0;
    L->listsize=Listinitsize;
    return 1;
}
int ListInsert_Sq(Sqlist *L,int i,int e)
{
    int *p,*q,*base;
    if(i<1||i>L->length+1)return 0;
    if(L->length>=L->listsize){
        base =(int *)realloc(L->elem,(L->listsize+Listincrement) *sizeof(int));
        if(! base)return -1;
        L->elem=base;
        L->listsize+=Listincrement;}
    q=&(L->elem[i-1]);
    for(p=&(L->elem[L->length-1]);p>=q;--p) *(p+1)=*p;
    *q=e;
    ++L->length;
    return 1;
    }
int ListDelete_Sq(Sqlist *L,int i)
{
    int *p,*q;
    if((i<1)||(i>L->length))return (-1);
    p=&(L->elem[i-1]);
    q=L->elem+L->length-1;
    printf("the deleted elem is %d \n",L->elem[i-1]);
    for(++p;p<=q;++p)
    *(p-1)=*p;
    --L->length;
    return 1;
    }
int inputlist(Sqlist *L)
{
    int i,*p;
 p=L->elem;
    printf("the list L_length is:");
    scanf("%d",&L->length);
    printf("\n");
    printf("the list element is:");
    for(i=0;i<L->length;i++)
    scanf("%d",(p+i));
    return 1;   
}
int display(Sqlist *L)
{   int i;
    printf("the list is:");
    for(i=0;i<L->length;i++)
    printf("%5d",*(L->elem++));
    printf("\n");
    return 1;   
    }
int InitList_Sq(Sqlist *L);
int display(Sqlist *L);
int inputlist(Sqlist *L);
int ListInsert_Sq(Sqlist *L,int i,int e);
int ListDelete_Sq(Sqlist *L,int i);
main()
{
    int i,choice, x;
    Sqlist La;
    InitList_Sq(&La);
   inputlist(&La);
do{
    printf("1  ListInsert\n");
    printf("2  ListDelete\n");
    printf("3  exit\n");
    printf("welcome to choice: ");
    scanf("%d",&choice);
    printf("\n");
    switch(choice){
      case 1:{
        printf("i,x=");
            scanf("%d,&d",&i,&x);
            ListInsert_Sq(&La, i, x);
            display(&La);}
            break;
      case 2:
          {
              printf("i=");
          scanf("%d",&i);
          ListDelete_Sq(&La,i);
          display(&La);
          }
          break;}
      }while(choice!=3);
    getchar();
}






搜索更多相关主题的帖子: 删除 线性 结构 顺序 
2009-10-03 11:28
zcs302567601
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-9-21
收藏
得分:0 
运行时,结果不正确,本人觉得是那个主函数中循环 有问题,求那位帮忙改一下
2009-10-03 15:14
chenaiyuxue
Rank: 5Rank: 5
来 自:山东滨州
等 级:职业侠客
帖 子:334
专家分:370
注 册:2008-5-20
收藏
得分:0 
把问题描述清楚一点儿,方便大家帮你

你是雪,我是尘埃,相遇是意外;你坠落,在我胸怀,流进我血脉。
2009-10-03 16:59
zcs302567601
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-9-21
收藏
得分:0 
回复 3楼 chenaiyuxue
比如,插入一个数时,结果插入的不是那个数,是其它的数,而且特别大;在循环的话,线性表中数基本上全部变成其它的数了。。。我也说不好,麻烦你运行一下就清楚问题了
2009-10-03 19:39
jackwain
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:168
专家分:134
注 册:2009-3-21
收藏
得分:10 
#include <stdio.h>
#include <stdlib.h>
typedef struct node    /*我自已写的。也不知道好不好。你们都看的是清华大学那本。*/
                       /* 我实在看不懂只好看别人的代码。再自已编了。*
{
    int num;
     struct node *next;
}Node;
Node *make_list(int *array,int len)
{
     Node *head;/*头结点*/
     Node *new_node;/*新结点*/
     Node *previous;/*当前结点*/
      int i;
     /*建立头结点*/
      head=(Node *)malloc(sizeof(Node));
      head->num=array[0];
      head->next=NULL;
      previous=head;
      for(i=0;i<len;i++)
      {
           new_node=(Node *)malloc(sizeof(Node));
           if(!new_node)
           {
               fprintf(stderr,"ERROR");
               exit(1);
           }
           new_node->num=array[i];
           new_node->next=NULL;
           previous->next=new_node;
           previous=new_node;
      }
      return head;
}
Node *insert(Node *head,int ops,int value)
{
  Node *current;
  Node *keep;
  Node *new_node;
  current=head->next;
  keep=head;
  ops--;
  while(current!=NULL&&ops>0)
  {
      keep=current;
      current=current->next;
     ops--;
  }
 new_node=(Node *)malloc(sizeof(Node));
 if(!new_node)
 {
     fprintf(stderr,"ERROR");
     EXIT_FAILURE;
 }
 new_node->num=value;
 new_node->next=keep->next;
 keep->next=new_node;
 return head;
}
Node *del_delete(Node *head,int value)
{
    Node *current;
    Node *ptr;
    current=head->next;
    ptr=head;
    while(current!=NULL&&current->num!=value)
    {
        ptr=current;
        current=current->next;
    }
    ptr->next=current->next;
    free(current);
   return head;
}
void show(Node *head)
{
    Node *ptr;
    ptr=head->next;
    while(ptr!=NULL)
    {
        printf("[%d]",ptr->num);
        ptr=ptr->next;
    }
    printf("\n");
}
 
int main()
{
    Node *head=NULL;
    int num;
    int value;
    int array[10]={1,3,5,4,8,9,7,10,6,2};
    head=make_list(array,10);
    printf("创建链表内容=>");
    show(head);
    printf("输入插入值=>");
    scanf("%d",&num);
    printf("\n输入位置==>");
    scanf("%d",&value);
    head=insert(head,value,num);
    printf("插入后=>");
    show(head);
    printf("输入删除值=>");
    scanf("%d",&value);
    head=del_delete(head,value);
    printf("\n删除后=>");
    show(head);
    return 0;
}

[ 本帖最后由 jackwain 于 2009-10-3 23:31 编辑 ]
2009-10-03 23:27
快速回复:线性表顺序存储结构的插入和删除,程序有点问题
数据加载中...
 
   



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

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