#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int date;
struct node *next;
}lnode,*list;
void printf_list(list &l){
lnode *p;
p=l->next;
while(p!=NULL)
{
printf("%d,",p->date);
p=p->next;
}
printf("\n");
}
list list_tail(list &l)
{
int x;
l=(lnode *)malloc(sizeof(lnode));
lnode *r,*s;
r=l;
scanf("%d",&x);
while(x!=9999)
{
s=(lnode *)malloc(sizeof(lnode));
s->date=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return l;
}
list list_head(list &l)
{
l=(lnode *)malloc(sizeof(lnode));
l->next=NULL;
lnode *s;
int x;
scanf("%d",&x);
while(x!=9999)
{
s=(lnode *)malloc(sizeof(lnode));
s->date=x;
s->next=l->next;
l->next=s;
scanf("%d",&x);
}
return l;
}
bool check_null(list &l)
{
if(l->next==NULL)
return true;
return false;
}
bool insert_index(list &l,int i,int e)
{
if(i<1)
return false;
lnode *p;
p=l;
int j=0;
while(p!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(p==NULL) return false;
lnode *s=(lnode *)malloc(sizeof(lnode));
s->date=e;
s->next=p->next;
p->next=s;
return true;
}
bool insert_prior(lnode *p, int e)
{
if(p==NULL)
return false;
lnode *s = (lnode *)malloc(sizeof(lnode));
if(s == NULL)
return false;
s->next = p->next;
p->next = s;
s->date = p->date;
p->date = e;
return true;
}
lnode * find_index(list &l,int pos){
if(pos<1)
return NULL;
if(pos<1)
return NULL;
int j = 1;
lnode *p = l->next;
while(p != NULL && j<pos)
{
p = p->next;
j++;
}
return p;
}
bool list_del(list &l, int i, int &e)
{
if (i < 1) return false;
lnode *p = l;
int j = 0;
while (p != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (p == NULL)
return false;
if (p->next == NULL)
return false;
lnode* q = p->next;
e = q->date;
p->next = q->next;
free(q);
return true;
}
bool list_orderdel(lnode *p)
{
if(NULL == p)
return false;
lnode *q = p->next;
p->date = p->next->date;
p->next = q->next;
free(q);
return true;
}
int main()
{
list l;
int e;
l=list_head(l);
printf("头插法:");
printf_list(l);
printf("按位序插入:");
insert_index(l,1,100);
printf_list(l);
lnode *p=find_index(l,2);
printf("按位序查找:");
printf("%d",p->date);
printf_list(l);
}