线性表的链式存储
1、结构体
typedef struct LNode{
ElemType data; //节点的数据域
struct LNode *next; //节点的指针域,指针的类型为LNode,即指向节点
}LNode, *LinkList; //LNode为结构体的名称
2、初始化
bool InitList(LinkList &L)
{
L = (LNode *) malloc(sizeof(LNode)); //申请空间
if(L==NULL) //申请失败,返回false
return false;
L->next = NULL; //申请成功,将头节点置空
return true;
}
3、头插法生成链表
LinkList List_HeadInsert(LinkList &L)
{
LNode *s;int x;
L =(LinkList)malloc(sizeof(LNode));
L->next = NULL;
cin >> x;
while(x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
cin >> x;
}
return L;
}
4、尾插法生成链表
LinkList List_TailInsert(LinkList &L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s,*r=L; //r为表尾指针
cin >> x;
while(x !=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = NULL;
r->next = s;
r = s;
cin >> x;
}
}
5、按序号查找元素
LNode *GetElem(LinkList L, int i)
{
int j = 1;
LNode *p = L->next;
if(i==0)
return L;
if(i<1)
return NULL;
while(p && j<i)
{
p = p->next;
j++;
}
return p;
}
6、按值查找元素
LNode *LocateElem(LinkList L,ElemType e)
{
LNode *p = L->next;
while(p!=NULL && p->data!=e)
p = p->next;
return p;
}
7、插入节点
void InsertNode(LinkList &L, int location ,LNode *node)
{
int j=1;
//由于此处链表结构中没有长度属性,故在此处不进行长度越界判断
LNode *p;
p = GetElem(L,location-1);
node->next = p->next;
p->next = node;
}
8、删除节点
void DeleteNode(LinkList &L, LNode *p)
{
LNode *q;
q=p->next;
p->next = q->next;
free(q);
}
以上是所有的函数实现的功能,下面是完整程序。
9、完整代码
#include<iostream>
#include<malloc.h>
#define ElemType int
using namespace std;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
bool InitList(LinkList &L)
{
L = (LNode *) malloc(sizeof(LNode));
if(L==NULL)
return false;
L->next = NULL;
return true;
}
LinkList List_HeadInsert(LinkList &L)
{
LNode *s;int x;
L =(LinkList)malloc(sizeof(LNode));
L->next = NULL;
cin >> x;
while(x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
cin >> x;
}
return L;
}
LinkList List_TailInsert(LinkList &L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s,*r=L; //r为表尾指针
cin >> x;
while(x !=9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = NULL;
r->next = s;
r = s;
cin >> x;
}
}
//按照序号查找元素
LNode *GetElem(LinkList L, int i)
{
int j = 1;
LNode *p = L->next;
if(i==0)
return L;
if(i<1)
return NULL;
while(p && j<i)
{
p = p->next;
j++;
}
return p;
}
//按值查找元素
LNode *LocateElem(LinkList L,ElemType e)
{
LNode *p = L->next;
while(p!=NULL && p->data!=e)
p = p->next;
return p;
}
void printLinkList(LinkList L)
{
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
cout << endl;
}
//插入节点
void InsertNode(LinkList &L, int location ,LNode *node)
{
int j=1;
//由于此处链表结构中没有长度属性,故在此处不进行长度越界判断
LNode *p;
p = GetElem(L,location-1);
node->next = p->next;
p->next = node;
}
void DeleteNode(LinkList &L, LNode *p)
{
LNode *q;
q=p->next;
p->next = q->next;
free(q);
}
int main()
{
LinkList L1;
InitList(L1);
List_TailInsert(L1);
printLinkList(L1);
LNode *node;
node = LocateElem(L1,3);
cout << node->data << endl;
LNode *n;
n->data = 0;
n->next = NULL;
InsertNode(L1,2,n);
printLinkList(L1);
return 0;
}