当前位置: 代码迷 >> C语言 >> 不能删除头节点的链表
  详细解决方案

不能删除头节点的链表

热度:182   发布时间:2005-04-06 12:43:00.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("输入要删除的节点:");
   scanf("%d",&x);
   dele(head,x);
    p=head;
while(p->next!=NULL)
    { printf("%d",p->data);
      p=p->next;
    }
}
搜索更多相关的解决方案: 链表  NODE  head  节点  next  

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

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


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

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


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