//单链表
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef struct LinkList{
int data;
LinkList *next;
int length;
}node;
node* Create();
int length(node* H);
node* Del(node *H,int num);
#endif
#include <iostream>
#include <stdlib.h>
#include "LinkList.h"
using namespace std;
node * Create()
{
node *H;
H=(node*)malloc(sizeof(node));
if(NULL==H)
{
printf("Init linklist failure!");
exit(1);
}
H->data = 0;
H->length = 0;
H->next = NULL;
int data,cycle=1;
node *p,*s;
p=H;
while(cycle)
{
printf("please input data:");
scanf("%d",&data);
if(data>0)
{
s=(node*)malloc(sizeof(node));
s->data = data;
s->next = NULL;
p->next = s;
p=s;
H->length ++;
}
else cycle = 0;
}
return H;
}
int length(node* H)
{
int n =0;
node *p;
p=H;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}
node* Del(node* H,int num)
{
node *p=H;
while(p->next->data!=num&&p->next!=NULL)
p=p->next ;
if(p!=NULL&&p->next->data==num)
{
p->next=p->next->next;
}
else
cout<<"can not find data!";
H=H->next;
return H;
}
void main()
{
node *L,*q,*p,*r;
int i,n,num;
L=Create();
q=L->next;
p=L;
cout<<L->length ;
for(i=0;i<L->length;i++)
{
printf("/nLink List data is:%d/n",q->data);
q=q->next ;
}
scanf("%d",&num);
r=Del(L,num);
for(i=0;i<L->length;i++)
{
printf("/nLink List data is:%d/n",r->data);
r=r->next ;
}
//n = length(p);
//printf("%d/n",n);
}