| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 383 人关注过本帖
标题:链表删除结点出现问题
只看楼主 加入收藏
cangogo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-4-28
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
链表删除结点出现问题
问题是这样的:链表的结点包括学号、姓名。从键盘输入一个学号,如果该学号与链表中某一结点的学号相同,则删去该结点。
我的程序是:
#include "stdio.h"
#include "stdlib.h"
#define N 81
typedef struct student
{
long num;
char name[N];
struct student *next;
}Student;
Student* create()
{
int i;   
Student *p,*head=NULL,*tail=NULL;
for(i=0;i<N;i++)
  {
   p=(Student*)malloc(sizeof(Student));
   scanf("%ld",&p->num);
   
    if(p->num<0)
    {
     getchar();
     free(p);
     break;
    }
   gets(p->name);
   p->next=NULL;
   if(head==NULL)
      head=p;
   else
      tail->next=p;
   tail=p;
  }   
  return head;
}
void output(Student *i)
{
while(i!=NULL)
{
  printf("%ld\t%s\n",i->num,i->name);
  i=i->next;
}
}
Student* Delete(Student* a,long n)
{
Student *head,*temp,*before;
temp=before=head=a;
while(temp!=NULL)
{
  if(temp->num==n)
      if(temp==head)
       {
        head=head->next;
        free(temp);
        temp=before=head;
       }
      else
      {
       before->next=temp->next;
       free(temp);
       temp=before->next;
      }
  else
  {
   before=temp;
   temp=temp->next;
  }
}
return head;
}
void main()
{
long i,j=0;
Student *p,*head;
puts("input Num and name:(the num <0 quit input!)");
p=create();
puts("input need search Num:");
scanf("ld",&i);
head=Delete(p,i);
puts("the former linked list:");
output(p);
puts("after delete linked list:");
output(head);
}
运行可以,但是Delete删除函数没有作用,大神们帮忙看看呗~
搜索更多相关主题的帖子: include create 键盘 姓名 
2015-05-08 22:35
cangogo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2015-4-28
收藏
得分:0 
我想死了。终于发现问题了!!!!!!!!!!!!!!!!!!!!!!啊啊啊啊啊,雅蠛蝶。
scanf("ld",&i); 死在这里了说好的%没有了,怎么编辑器没反应。。。。
2015-05-09 15:17
纳兰伽香
Rank: 10Rank: 10Rank: 10
来 自:北京
等 级:贵宾
威 望:10
帖 子:426
专家分:1650
注 册:2015-4-5
收藏
得分:10 
自己发现问题是好现象  赶紧结贴 分给我

风回小院庭芜绿,柳眼春相续
2015-05-09 17:13
快速回复:链表删除结点出现问题
数据加载中...
 
   



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

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