qq_34378013 2016-03-31 09:35 采纳率: 14.3%
浏览 1610

不带头结点的链表问题

 #include<stdio.h>
#include<stdlib.h>
typedef struct linknode
{
    int data;
    struct linknode *next;
} node;

node *Creat()
{
    node *head,*p,*s;
    int x,cycle=1;
    printf("Please input total number of data item (press 0 end):");
    head=(node* )malloc(sizeof(node));
    head->next=NULL;
    p=head;
    while (cycle) {
        scanf("%d",&x);
        if (x!=0) {
            s=(node*)malloc(sizeof(node));
            s->data=x;
            p->next=s;
            p=s;
        }
        else cycle=0;
    }
    p->next=NULL;
    p=head;
    head=head->next;
    free(p);
    return head;
}



void Delet(node *head,int i)
{
    int n=1;
    node *p;
    if (i==1){
        p=head;
        head=head->next;
        free(p);
    }
    else{
        while(head!=NULL){
        if(i==n+1) break;
            head=head->next;
            n++;
        }
        head->next=head->next->next;
    }
}

int main(void)
{
    int a;
    node *head,*p=NULL;
    head=Creat();
    printf("输入值a  \n");
    scanf("%d",&a);
        Delet(head,a);
    for(p=head;p!=NULL;p=p->next) printf("%d",p->data);
    return 0;
}

在删除不带头结点的链表的时候,我的这个程序的第一个数,就是头节点怎么删除,求大神帮忙修改一下我的程序。太感谢了。

  • 写回答

1条回答

  • 小灸舞 2016-03-31 11:51
    关注

    楼主要输入的值a代表的是第几个节点还是指节点里存的值?

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大