//单链表插入节点
#include<stdio.h>
#include<stdlib.h>
typedef int elem;
typedef struct Lnode {
elem *data;
struct Lnode *next;
}Lnode,*list;
//头插法节点;
list creat(Lnode &L)
{
Lnode *s;
int x;
L = (list)malloc(sizeof(Lnode));
L->next = NULL;
scanf("%d", &x);
while (x!=9999)
{
s = (list)malloc(sizeof(Lnode));
s->data = x;
s->next = L->next; //这个不能是s->next=NULL,如果这样的话,之后循环的节点的next指针指的都是NULL
L->next = s;
scanf("%d", &x);
}
return L;
}
Lnode *get1(list L, int i)
{
Lnode *p=L->next;
int j=1;
if (i <1)
{
return NULL;
}
if (i == 0)
{
return L;
}
while (p&&j<i)
{
p = p->next;
j++;
}
return p,j;
}
Lnode *get2(list L, elem e)//按值查找,返回查找的值的节点数和节点
{
int j = 1;
Lnode *p = L->next;
while (p!=NULL&&p->data!=e)
{
p = p->next;
j++;
return p,j;
}
}
list insert(list L, elem e,int i)
{
Lnode *p, *s;
int temp;
p = get( L, i - 1);
s = (list)malloc(sizeof(Lnode));
s->data = e;
s->next = p->next; //后插法
p->next = s;
temp = p->data;
s->data = temp;
}
list delect(list &L, elem e)//按值删除,不一定正确,改的
{
int j; elem e;
Lnode *p = L->next,*q;
q = get2(L, e);
p = get1(L, j - 1);
p->next = q->next;
free(q);
}