数据结构-单链表
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int data;
LNode *next;
}LNode,*LinkList;
bool InitNoHeadLinkList(LinkList &L)
{
L = NULL;
return true;
}
bool InitLinkList(LinkList &L)
{
L= (LNode *) malloc(sizeof(LNode));
if (L==NULL)
{
return false;
}
L->next = NULL;
return true;
}
bool InsertNoHList(LinkList &L, int i, int e)
{
if (i < 1)
{
return false;
}
if(i==1)
{
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = L;
L=s;
return true;
}
LNode *p;
int j = 1;
p = L;
while (p != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool InsertNode(LinkList &L,int i,int e)
{
if (i<1)
{
return false;
}
LNode *p;
int j = 0;
p = L;
while (p!=NULL&&j<i-1)
{
p = p->next;
j++;
}
if (p==NULL)
{
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next=p->next;
p->next = s;
return true;
}
bool InsertNextNode(LNode *p,int e)
{
if (p == NULL)
{
return false;
}
LNode *s = (LNode *)malloc(sizeof(LNode));
if (s==NULL)
{
return false;
}
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool InsertPriorNode(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->data = p->data;
p->data = e;
return true;
}
bool DeleteIndexNode(LinkList &L, int i, int &e)
{
if(i<1)return false;
LNode *p;
int j = 0;
p = L;
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->data;
p->next = q->next;
free(q);
return true;
}
bool DeleteNode(LNode *p, int e)
{
if (p == NULL)
return false;
LNode *q;
q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
return true;
}
LNode * SearchIndexNode(LinkList L, int i)
{
if (i < 0)
return NULL;
LNode *p;
int j = 0;
p = L;
while (p != NULL && j < i )
{
p = p->next;
j++;
}
return p;
}
LNode *SearchNode(LinkList L,int e)
{
LNode *p = L->next;
while (p!=NULL&&p->data==e)
{
p = p->next;
}
return p;
}
void InsertTail(LinkList &L)
{
LNode *s, *r;
r = L;
int a = 0;
scanf("%d",&a);
while (a!=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = 1;
r->next = s;
r = s;
}
r->next = NULL;
}
void InsertHeadList(LinkList &L)
{
LNode *s;
int a = 0;
scanf("%d", &a);
L->next = NULL;
while (a != 9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = 1;
s->next = L->next;
L->next = s;
}
}
typedef struct
{
int data;
DNode *prior, *next;
}DNode,*DLinkList;
bool InitDLinkList(DLinkList &L)
{
L = (DNode *)malloc(sizeof(DNode));
if (L==NULL)
{
return false;
}
L->prior = NULL;
L->next = NULL;
return true;
}
void InsertNextDNode(DNode *p,DNode *s)
{
s->next = p->next;
if (p->next!=NULL)p->next->prior = s;
s->prior = p;
p->next = s;
}
bool deleteDNode(DNode *p)
{
DNode *q = p->next;
if (q==NULL)
{
return false;
}
p->next = q->next;
if(q->next!=NULL)
q->next->prior = q;
free(q);
return true;
}
void DestroyDNode(DLinkList &L)
{
while (L->next!=NULL)
{
deleteDNode(L);
}
free(L);
L = NULL;
}
int main()
{
DLinkList L;
InitDLinkList(L);
}