当前位置: 代码迷 >> C语言 >> 链表删除问题?
  详细解决方案

链表删除问题?

热度:127   发布时间:2005-03-28 14:00:00.0
链表删除问题?
#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  

----------------解决方案--------------------------------------------------------
#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();
}
----------------解决方案--------------------------------------------------------

刚才的有点问题,这个可以了,不过输入不能为相同的数字 等我再该下 #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(); }


----------------解决方案--------------------------------------------------------

#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(); }


----------------解决方案--------------------------------------------------------

#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(); } 这样应该可以了。


----------------解决方案--------------------------------------------------------
  相关解决方案