#define null 0
struct node
{
  int data;
  struct node *next;
};
struct node *p,*L,*s,*q;   //全局变量
 int n,e;                  //全局变量
creater(int n)        //创建有n个元素的链表,顺序插入结点
{
 int i,k;
 L=(struct node *)malloc(sizeof(struct node));    //动态分配地址
 L->next=null;                                    //初始化链表(带头结点)
 s=L;
 for(i=1;i<=n;i++)      //输入n个元素,存放在单链表L中(尾插法,S为尾指针)
  {printf("input %d:jing",i);
   scanf("%d",&k);
   p=(struct node *)malloc(sizeof(struct node));     //增加结点
   p->data=k;                                       //对结点的元素赋值
   s->next=p;        //把新增结点的地址赋给上一个结点的next成员
   s=p;            //s指向结点p
 }
p->next=null;      //最后一个结点的地址为null
 }print()           //按顺序输出L中的元素
{
  struct node *p;
  p=L->next;             //从第一个结点开始输出
  while(p)
    {printf("%d   ",p->data);
  p=p->next;
     }
 }delete(int i)    //删除L中第i个元素,并把删除的元素输出
{
int e;
  int j=0;
  struct node *p;
  if(i<1 ||i>n)         //i的位置不正确
    printf("error\n");
  else
     {p=L;                 //L为头结点
     while(j<i-1)       //找到第i个结点的前驱结点,循环i-1次,从头结点开始查找
      {
        p=p->next;
        j++;
      }
      q=p->next;         //删除第i个元素
      p->next=q->next;
      e=q->data;
      free(q);        //释放结点
      printf("\ndeleted is:%d\n",e);
    }
}
insert(int i,int x)
{
  int j;
  if(i<1 ||i>n+1)
     printf("insert error!\n");
  else
   {
     j=0,p=L;            //L为头结点
     while(j<i-1)         //查找第i个元素的前驱结点
       {p=p->next;j++;}
     q=(struct node *)malloc(sizeof(struct node)); //动态分配新结点
       q->next=p->next;                           //插入新结点
       q->data=x;
         p->next=q;
   }
}main()
{
int i,j,x;
printf("\nn=");
scanf("%d",&n);
creater(n);                 //创建链表
printf("\nnew link:");
print();                    //输出链表的元素
printf("\ndelete position: ");
scanf("%d",&i);
delete(i);                  //删除链表中的元素
printf("\nafter deleted,the  link is:");
print();                     //输出删除后元素 后的链表
printf("\ninsert new element:");
scanf("%d",&x);
printf("\n insert position:");
scanf("%d",&i);
insert(i,x);  //插入元素
printf("atfer insertde,the new link is:");
print();     //输出插入元素后的链表中的各元素
getch();
}如果你把delete(int i)这个诊断代码删除以后,就无法正常运行了!真的好奇怪哦!
希望各位大大能把我回答一下这个问题!谢谢了!

解决方案 »

  1.   

    delete是关键字,请先重命名delete(int i)
      

  2.   

    重命名过后删除delete()测试代码调试过后没有问题的
      

  3.   

    这个插入的程序它不会报错了,但是出现了一个新的问题,当我的函数creater(int n)打印出来了后,编译框中就显示出Press any key to continue,无法进入插入的运算!这是怎么会事呀?
    我把我的代码COPY出来!大家帮忙看下,谢谢了!
      

  4.   

    #define null 10
    struct node 
    {
    int data;
    struct node *next;
    };
    struct node *p,*L,*s,*q;
    int n,e;
    creater(int n)
    {
    int i,k;
    L = (struct node *)malloc(sizeof(struct node));
    L->next = null;
    s = L;
    for(i = 1; i <= n; i++)
    {
    printf("input %d:",i);
    scanf("%d",&k);
    p = (struct node *)malloc(sizeof(struct node));
    p->data = k;
    s->next = p;
    s = p;
    }
    p->next = null;
    }
    print()
    {
    struct node *p;
    p = L->next;
    while(p)
    {printf("%d    ",p->data);
    p = p->next;}
    }
    insert(int i, int x)
    {
    int j;
    if(i < 1 || i > n+1)
    {
    printf("display error!");
    }
    else
    {
                   j = 0; p = L;
    while(j < i-1)
    {
    p = p->next;
    j++;
    }
    q = (struct node *)malloc(sizeof(struct node));
    q->next = p->next;
            q->data = x;
            p->next = q;
    }
    }
    main()
    {
    int i,j,x;
    printf("\nn=");
    scanf("%d",&n);
    creater(n);                 //创建链表
    printf("\nnew link:");
    print();                    //输出链表的元素
    printf("\ninsert new element:");
    scanf("%d",&x);
    printf("\n insert position:");
    scanf("%d",&i);
    insert(i,x);  //插入元素
    printf("atfer insertde,the new link is:");
    print();     //输出插入元素后的链表中的各元素
    getch();
    }
      

  5.   

    那个VC6.0是可以运行这个程序的,老师给我们的代码都可以运行,但是我不知道为什么我自己打出来了以后就只能运行到creater(int n)这个链表打印出来就无法运行下面的插入程序了!
      

  6.   

    到这这段程序以后,
    printf("\ninsert new element:");
    scanf("%d",&x);
    printf("\n insert position:");
    scanf("%d",&i);
    insert(i,x); //插入元素
    printf("atfer insertde,the new link is:");
    print(); //输出插入元素后的链表中的各元素
    getch();
    它无法显示出来,就像火车没有接轨,差了一截!