C语言中如何删掉一个数据

问题就是 我的首节点删不掉 请各位帮小弟看看
# include<stdio.h>
# include<stdlib.h>
# define M 5
struct student
{
int number;
char name[20];
struct student *next;
};
struct student *p,*pp,*q,*qq,*pop,*temp,*head=NULL;
void main()
{
void cat(struct student *p,struct student *head,int n);

int i=1,n;
printf("请输入要删除的位置");
scanf("%d",&n);
while(i<=M)
{
q=(struct student *)malloc(sizeof(struct student));
scanf("%d %s",&q->number,q->name);
if(head==NULL)
head=p=q;
p->next=q;
p=q;
q->next=NULL;
i++;
}

cat(head,head,n);

p=head;
while(p!=NULL)
{
printf("%d %s",p->number,p->name);
printf("\n");
p=p->next;
}
}

void cat(struct student *p,struct student *head,int n)
{
int i=1;
while(p!=NULL)
{
for(i=1;i<n;i++)
{ q=p;
p=p->next;}

if(p==head)
head=head->next;
else
q->next=p->next;
break;
}
}

# include<stdio.h>
# include<stdlib.h>
# define M 5
struct student
{
int number;
char name[20];
struct student *next;
};
struct student *p,*pp,*q,*qq,*pop,*temp,*head=NULL;
void main()
{
struct student* cat(struct student *p,struct student *head,int n);

int i=1;
while(i<=M)
{
q=(struct student *)malloc(sizeof(struct student));
printf("输入第%d个结点学号和姓名:(注意有空格)\n",i);
scanf("%d %s",&q->number,q->name);
if(head==NULL)
head=p=q;
p->next=q; //将新分配的结点连起来
p=p->next; //指针p指向最后一个结点
q->next=NULL; //最后一个结点的指针域置空
i++;
}
//输出构造的学生链表,作对照
printf("未被删除前:\n");
p=head;
while(p!=NULL)
{
printf("%d\t%s",p->number,p->name);
printf("\n");
p=p->next;
}
int n;
printf("请输入要删除的位置");
scanf("%d",&n);
//删除结点
p=head;
pp=cat(p,head,n);

printf("元素被被删除后:\n");
while(pp->next!=NULL)
{
printf("%d\t%s",pp->number,pp->name);
printf("\n");
pp=pp->next;
}
//输出最后一个结点
printf("%d\t%s\n",pp->number,pp->name);
}

//删除一个结点
struct student* cat(struct student *p,struct student *head,int n)
{
int i=1;
while(p!=NULL)
{
//for循环使p指向要被删除的结点.q指向被删除结点的前一个结点
for(i=1;i<n;i++)
{
q=p;
p=p->next;
}

if(p==head) //删除结点为头结点时
{
head=p->next;
free(p);
return head;
}
else if(p->next==NULL) //删除结点为尾结点时
{
q->next=NULL;
free(p);
return head;
}
else
{
q->next=p->next; //删除非头尾结点时
free(p);
return head;
}
}
}
/*测试数据如下:*/

/*
输入第1个结点学号和姓名:(注意有空格)
1000 wang
输入第2个结点学号和姓名:(注意有空格)
1001 li
输入第3个结点学号和姓名:(注意有空格)
1002 zhang
输入第4个结点学号和姓名:(注意有空格)
1003 yan
输入第5个结点学号和姓名:(注意有空格)
1004 zhao
未被删除前:
1000 wang
1001 li
1002 zhang
1003 yan
1004 zhao
请输入要删除的位置1
元素被被删除后:
1001 li
1002 zhang
1003 yan
1004 zhao
*/

/*
输入第1个结点学号和姓名:(注意有空格)
1000 wang
输入第2个结点学号和姓名:(注意有空格)
1001 li
输入第3个结点学号和姓名:(注意有空格)
1002 zhang
输入第4个结点学号和姓名:(注意有空格)
1003 yan
输入第5个结点学号和姓名:(注意有空格)
1004 zhao
未被删除前:
1000 wang
1001 li
1002 zhang
1003 yan
1004 zhao
请输入要删除的位置3
元素被被删除后:
1000 wang
1001 li
1003 yan
1004 zhao
*/

/*
输入第1个结点学号和姓名:(注意有空格)
1000 wang
输入第2个结点学号和姓名:(注意有空格)
1001 li
输入第3个结点学号和姓名:(注意有空格)
1002 zhang
输入第4个结点学号和姓名:(注意有空格)
1003 yan
输入第5个结点学号和姓名:(注意有空格)
1004 zhao
未被删除前:
1000 wang
1001 li
1002 zhang
1003 yan
1004 zhao
请输入要删除的位置5
元素被被删除后:
1000 wang
1001 li
1002 zhang
1003 yan
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-19
给你个单向顺序链表的程序代码看吧,包括链表的基本操作了,调试通过,你自己研究研究,你的代码看着有点迷糊
#include<stdio.h>
#define NULL 0
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
}
int n;
//建立一个简单动态单向链表函数
struct student *creat(void)
{
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
//链表输出函数
void print(struct student *head)
{
struct student *p;
printf("\nNow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld\t%5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
//删除链表结点函数
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL){printf("\nList null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
}
else
printf("%ld can not been found!\n",num);
end:;//这里一定要有个冒号
return(head);
}
//插入结点函数
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p0=stud;
p1=head;
if(head==NULL)
{head=p0;p0->next=NULL;}
while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;p0->next=NULL;
}
n=n+1;
return(head);
}
//主函数调用
void main()
{
struct student *head,stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the the deleted number:\n");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("\ninput the the deleted number:\n");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:\n");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu.num,&stu.score);
while(stu->num!=0)
{
head=insert(head,&stu);
print(head);
printf("\ninput the inserted record:\n");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu.num,&stu.score);
}
}
第2个回答  推荐于2016-05-28

这个问题很抽象,C语言中,有许多地方都可以删除数据,例如

    删除某种集合当中某一个数据。

    对于数组来说,使用插入法,找到要删除的值,用后面一个变量覆盖前面一个。

    对于链表来说,直接挪动指针的指向,并释放内存。

    对于映射来说,直接用key定位删除即可。