|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct Node
{
ElemType data;
/*struct Node* next;*/
struct Node* last;
}Node,*LinkList;
Status visit(ElemType c);
Status InitList(LinkList* L);
Status EmptyList(LinkList L);
Status DestroyList(LinkList* L);
Status ClearList(LinkList* L);
int ListLength(LinkList L);
Status GetElem(LinkList L, int i, ElemType* e);
int LocateElem(LinkList L, ElemType e);
Node* LocateElem2(LinkList L, ElemType e);
Status ListInsert(LinkList* L, int i, ElemType e);
Status ListDelete(LinkList* L, int i, ElemType* e);
void CreatListHead(LinkList* L, int n);
void CreatListTail(LinkList* L, int n);
Status ListTraverse(LinkList L);
Status visit(ElemType c)
{
cout << c << " ";
return OK;
}
Status InitList(LinkList *L)
{
*L = new Node;
if(!(*L))
{
return ERROR;
}
(*L)->last = *L;
return OK;
}
Status EmptyList(LinkList L)
{
if (L->last!=L)
{
return FALSE;
}
else
{
return TRUE;
}
}
Status DestroyList(LinkList *L)
{
LinkList p = (*L);
while ((*L)->last!=(*L))
{
p = (*L);
(*L) = (*L)->last;
delete p;
}
return OK;
}
Status ClearList(LinkList *L)
{
LinkList p, q;
p = (*L)->last;
while (p!=(*L))
{
q = p->last;
delete p;
p = q;
}
(*L)->last =(*L);
return OK;
}
int ListLength(LinkList L)
{
int i = 0;
LinkList p = L->last;
while (p!=L)
{
i++;
p = p->last;
}
return i;
}
Status GetElem(LinkList L,int i, ElemType *e)
{
int j = 1;
LinkList p = L->last;
while (p != L&&j<i )
{
p = p->last;
j++;
}
if (p==L||j>i)
{
return ERROR;
}
*e = p->data;
return OK;
}
int LocateElem(LinkList L, ElemType e)
{
LinkList p = L->last;
int i = 0;
while (p!=L)
{
++i;
if (p->data==e)
{
return i;
}
p = p->last;
}
return i;
}
Node* LocateElem2(LinkList L, ElemType e)
{
LinkList p = L->last;
while (p!=L)
{
if (p->data==e)
{
return p;
}
}
return p;
}
Status ListInsert(LinkList* L, int i, ElemType e)
{
int length = ListLength(*L)+1;
if (i > length||i<1)
{
return ERROR;
}
LinkList p, s;
p = (*L);
int j = 0;
while (j<i-1)
{
++j;
p = p->last;
}
/*if (j>i-1)
{
return ERROR;
}*/
s = new Node;
s->data=e;
s->last = p->last;
p->last = s;
return OK;
}
Status ListDelete(LinkList* L, int i, ElemType* e)
{
int length = ListLength(*L);
if (i>length||i<1)
{
return ERROR;
}
int j=0;
LinkList p, q;
p = *L;
while (p->last!=*L &&j<i-1)
{
p = p->last;
++j;
}
/*if (j > i - 1)
{
return ERROR;
}*/
q = p->last;
p->last = q->last;
*e = q->data;
delete q;
return OK;
}
Status ListTraverse(LinkList L)
{
LinkList p = L->last;
while (p!=L)
{
visit(p->data);
p = p->last;
}
cout << endl;
return OK;
}
void CreatListHead(LinkList* L, int n)
{
*L = new Node;
(*L)->last = *L;
LinkList p;
for (int i=0;i<n;i++)
{
p = new Node;
cin >> p->data;
p->last = (*L)->last;
(*L)->last = p;
}
}
void CreatListTail(LinkList* L, int n)
{
LinkList p, r;
*L = new Node;
(*L)->last = (*L);
r = (*L);
for (int i=0;i<n;i++)
{
p = new Node;
cin >> p->data;
r->last = p;
r = p;
}
r->last = *L;
}
int main()
{
int i;
int j;
int k;
ElemType e;
LinkList L;
LinkList p;
LinkList La, Lb;
i = InitList(&L);
cout << "初始化链表(1.成功 0.失败)"<<i<< endl;
for (j=1;j<=5;j++)
{
ListInsert(&L, 1, j);
}
cout << "表头插入5个元素后,L.data=";
ListTraverse(L);
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
i = ClearList(&L);
cout << "清空L后ListLength(L)=" << ListLength(L) << endl;
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
for (j = 1; j <= 10; j++)
{
ListInsert(&L, j, j);
}
cout << "表尾插入10个元素后,L.data=";
ListTraverse(L);
GetElem(L, 5, &e);
cout << "第5个元素的值为" << e << endl;
e = 9;
i=LocateElem(L, e);
if (i == 0)
{
cout << "没有找到值为" << e << "的元素" << endl;
}
else
{
cout << "值为" << e << "的元素的位序为" << i << endl;
}
/*p=LocateElem2(L, e);
cout << "值为" << e << "元素的地址为" << p<< endl;*/
放上代码,当我用p去接收e的地址的时候,输出不了,这个"cout << "值为" << e << "元素的地址为" << p<< endl"这条语句,其后面的语句也无法跟着输出.
|
|