| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 682 人关注过本帖
标题:链表删除问题?
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:4 
链表删除问题?
#include"stdio.h"
 typedef struct node
  {  int data;
     struct node *next;
  }NODE;
  int x,i;
  NODE *creat()
 {  NODE *head,*p,*s;
    int x;
    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 delete(head,x)
 {  NODE *head,*p,*q;
   if(head==NULL) printf("链表下益\n");
   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;
    head=creat();
    while(p!=NULL)
    { printf("%d",p->data);
      p=p->next;
    }
   printf("输入要删除的节点:");
   scanf("%d",&x);
   delete(head,x);
    p=head;
    while(p!=NULL)
    { printf("%d",p->data);
      p=p->next;
    }  
 }    //谁能调通这个程序啊?
搜索更多相关主题的帖子: 链表 NODE head next 
2005-03-28 14:00
luckbird
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2005-3-21
收藏
得分:0 
#include"stdio.h"
   #include<malloc.h>
typedef struct node
  {  int data;
     struct node *next;
  }NODE;
  int x,i;
  NODE *creat()
{  NODE *head,*p,*s;
    int x;
    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 delete(NODE *head,int x)
{  NODE *p,*q;
   if(head==NULL) printf("链表下益\n");
   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;
    head=creat();p=head;
    while(p!=NULL)
    { printf("%d ",p->data);
      p=p->next;
    }
   printf("输入要删除的节点:");
   scanf("%d",&x);
   delete(head,x);
    p=head;
    while(p!=NULL)
    { printf("%d ",p->data);
      p=p->next;
    }   ;getch();
}

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

刚才的有点问题,这个可以了,不过输入不能为相同的数字 等我再该下 #include"stdio.h" #include<malloc.h> typedef struct node { int data; struct node *next; }NODE; int x,i; NODE *creat() { NODE *head,*p,*s; int x; 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 * delete(NODE *head,int x) { NODE *p,*q; if(head==NULL) printf("链表下益\n"); 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 *hea,*p; hea=creat();p=hea; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("输入要删除的节点:"); scanf("%d",&x); p=delete(hea,x);

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


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

#include"stdio.h" #include<malloc.h> typedef struct node { int data; struct node *next; }NODE; int x,i; NODE *creat() { NODE *head,*p,*s; int x; 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 *delete(NODE *head,int x) { NODE *p,*q,*r; if(head==NULL) printf("链表下益\n"); while(head->data==x) { p=head; head=head->next; free(p); }

q=head; p=head->next; while(p!=NULL){ while(p!=NULL&&p->data!=x) if(p->data!=x) { q=p;p=p->next; } if(p!=NULL) { q->next=p->next;r=p;p=p->next; free(r); } else printf("没找到\n"); } return head; } main() { NODE *hea,*p; hea=creat();p=hea; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("输入要删除的节点:"); scanf("%d",&x); p=delete(hea,x);

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


2005-04-02 20:52
shangmin
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2005-3-17
收藏
得分:0 

#include"stdio.h"

typedef struct node { int data; struct node *next; }NODE; NODE *creat() { NODE *head1,*p,*s; int a; p=(NODE*)malloc(sizeof(NODE)); head1=p; printf("Input the number a:\n"); scanf("%d",&a); while(a!=0) { s=(NODE*)malloc(sizeof(NODE)); s->data=a; p->next=s; p=s; scanf("%d",&a); } p->next=NULL; return head1; } void delete(NODE *head,int x) { NODE *p,*q,*s; if(head==NULL) printf("error!\n"); else { q=head;p=head->next; while(p!=NULL) { if(p->data==x) { s=p; p=p->next; q->next=p; free(s); break; } q=p; p=p->next; if(p->next==NULL) { printf("can't find x\n"); break; } } } } void main() { int x; NODE *head,*p; head=creat(); printf("Input the number x which you want to delete:"); scanf("%d",&x); delete(head,x); p=head; while(p->next!=NULL) { printf("%d ",p->next->data); p=p->next; } getch(); } 这样应该可以了。

2005-04-02 23:18
快速回复:链表删除问题?
数据加载中...
 
   



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

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