单链表存储结构
#include<cstdio>
#define Elemtype int
typedef struct LNode{
Elemtype data;
struct LNode *next;
}*LinkList,LNode;
LinkList InitLinkList(LinkList &L)
{
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode *p;
Elemtype x;
scanf("%d",&x);
while(x!=9999)
{
p = (LNode*)malloc(sizeof(LNode))
s->data = x;
s-next = L->next;
L->next = s;
scanf("%d",&x);
}
return L;
}
LinkList InitLinkList(LinkList &L)
{
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode *p,*q;
Elemtype x;
p = L;
scanf("%d",&x);
while(x!=9999)
{
q = (LNode*)malloc(sizeof(LNode));
q->data = x;
q->next = NULL;
p->next = q;
p = q;
scanf("%d",&x);
}
return L;
}
LNode* GetNode(LinlList L,int i)
{
int j = 1;
LNode *p;
p = L->next;
if(i==0)
{
return L;
}
if(i<1)
{
return NULL;
}
while(p&&j<i)
{
p = p->next;
j++;
}
return p;
}
LNode* GetNode(LinkList L,Elemtype e)
{
LNode *p = L->next;
while(p)
{
if(p->data == e)
{
return p;
}
p = p->next;
}
return NULL;
}
bool InsertLinkList(LinkList &L,Elemtype e,int i)
{
if(i<=0)
{
return false;
}
LNode *q,*p=L;
for(int j=1;j<i;j++)
{
if(!p)
{
return false;
}
p = p->next;
}
q = (LNode*)malloc(sizeof(LNode));
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
bool DeleteLinkListNode(LinkList &L,int i,Elemtype &e)
{
if(i<1)
{
return false;
}
LNode *p=L,*q;
for(int j=1;j<i;j++)
{
if(!p)
{
return false;
}
p = p->next;
}
q = p->next;
p->next = q->next;
e = q->data;
free(q);
return true;
}