| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 736 人关注过本帖
标题:不能删除头节点的链表
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:2 
不能删除头节点的链表
#include"stdio.h"
 typedef struct node
  {  int data;
     struct node *next;
  }NODE;
  NODE *creat()
 {  int x;
    NODE *head,*p,*s;
    head=(NODE*)malloc(sizeof(NODE));
    p=head;
    printf("输入整数,以0标志结束\n");
    scanf("%d",&x);
    while(x!=0)
   {  s=(NODE*)malloc(sizeof(NODE));
      s->data=x;
      p->next=s;
      s->next=NULL;
      p=s;
      scanf("%d",&x);
   }
   p->next=NULL;
   p=head;
   head=head->next;
   free(p);
   return head;
 }
void dele(NODE *head,int x)
 {  NODE *p,*q;
   if(head==NULL) { printf("链表下益\n"); return ;}
   if(head->data==x)
   { p=head;
     head=head->next;
     free(p);
   }
   else
   { q=head; p=head->next;
     while(p!=NULL&&p->data!=x)
      if(p->data!=x)
      { q=p;p=p->next;
      }
      if(p!=NULL)
      { q->next=p->next;
        free(p);
      }
      else printf("没找到\n");
   }
 }
 main()
 {  NODE *head,*p;
    int x;
    head=creat();
    p=head;
    while(p!=NULL)
    { printf("%d",p->data);
      p=p->next;
    }
   printf("输入要删除的节点:");
   scanf("%d",&x);
   dele(head,x);
    p=head;
while(p->next!=NULL)
    { printf("%d",p->data);
      p=p->next;
    }
 }
搜索更多相关主题的帖子: 链表 NODE head 节点 next 
2005-04-06 12:43
luckbird
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2005-3-21
收藏
得分:0 

#include"stdio.h" typedef struct node { int data; struct node *next; }NODE; NODE *creat() { int x; NODE *head,*p,*s; head=(NODE*)malloc(sizeof(NODE)); p=head; printf("输入整数,以0标志结束\n"); scanf("%d",&x); while(x!=0) { s=(NODE*)malloc(sizeof(NODE)); s->data=x; p->next=s; s->next=NULL; p=s; scanf("%d",&x); } p->next=NULL; p=head; head=head->next; free(p); return head; } void dele(NODE **head,int x) { NODE *p,*q; if(*head==NULL) { printf("链表下益\n"); return ;} if((*head)->data==x) { p=head; *head=(*head)->next; free(p); } else { q=*head; p=(*head)->next; while(p!=NULL&&p->data!=x) if(p->data!=x) { q=p;p=p->next; } if(p!=NULL) { q->next=p->next; free(p); } else printf("没找到\n"); } } main() { NODE *head,*p; int x; head=creat(); p=head; while(p!=NULL) { printf("%d",p->data); p=p->next; } printf("输入要删除的节点:"); p=head; scanf("%d",&x); dele(&head,x);p=head;

while(p!=NULL) { printf("%d",p->data); p=p->next; } getch(); }


2005-04-06 15:41
luckbird
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2005-3-21
收藏
得分:0 

#include"stdio.h" typedef struct node { int data; struct node *next; }NODE; NODE *creat() { int x; NODE *head,*p,*s; head=(NODE*)malloc(sizeof(NODE)); p=head; printf("输入整数,以0标志结束\n"); scanf("%d",&x); while(x!=0) { s=(NODE*)malloc(sizeof(NODE)); s->data=x; p->next=s; s->next=NULL; p=s; scanf("%d",&x); } p->next=NULL; p=head; head=head->next; free(p); return head; } NODE * dele(NODE *head,int x) { NODE *p,*q; if(head==NULL) { printf("链表下益\n"); return ;} if(head->data==x) { p=head; head=head->next; free(p); } else { q=head; p=head->next; while(p!=NULL&&p->data!=x) if(p->data!=x) { q=p;p=p->next; } if(p!=NULL) { q->next=p->next; free(p); } else printf("没找到\n"); } return head; } main() { NODE *head,*p; int x; head=creat(); p=head; while(p!=NULL) { printf("%d",p->data); p=p->next; } printf("输入要删除的节点:"); p=head; scanf("%d",&x); p=dele(head,x);

while(p!=NULL) { printf("%d",p->data); p=p->next; } }


2005-04-06 15:48
快速回复:不能删除头节点的链表
数据加载中...
 
   



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

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