#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;
}
在删除不带头结点的链表的时候,我的这个程序的第一个数,就是头节点怎么删除,求大神帮忙修改一下我的程序。太感谢了。