#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
typedef struct Lnote {
int data;
struct Lnote* next;
}Lnote, * Linklist;
bool InitList(Linklist& L)
{
L = NULL;
return true;
}
bool Empty(Linklist L)
{
if (L == NULL)
return true;
else
return false;
}
Linklist TailInsert(Linklist& L)
{
int x = 0;
Lnote* s, * r = L;
L = (Linklist)malloc(sizeof(Lnote));
scanf("%d", &x);
while (x != 9999)
{
s = (Lnote*)malloc(sizeof(Lnote));
s->data = x;
r->next = s;
r = s;
}
r->next = NULL;
return L;
}//尾插法
Linklist HeadInsert(Linklist& L)
{
Lnote* s ,int x;
s= (Lnote*)malloc(sizeof(Lnote));
s = NULL;
scanf("%d", x);
while (x != 9999)
{
s->data = x;
s->next = L->next;
L -> next = s;
scanf("%d", x);
}
return L;
}//头插法
Linklist GetElem(Linklist& L, int i)
{
if (i==0)
return NULL;
int j = 0;
Lnote* p = L->next;
while (p != NULL || j < i)
{
p = p->next;
j++;
}
return p;//按序查找
}
Linklist LocateElem(Linklist& L, int e)
{
int j = 0;
Lnote* p = L->next;
while (p != NULL || p->data!=e)
p = p->next;
return p;
}//按值查找
bool ListInsert(Linklist& L,int i, int e)
{
if (i < 1 || L->next == NULL)
return false;
Lnote* p;
Lnote*s =(Lnote*)malloc(sizeof(Lnote));
p = GetElem(L, i - 1);
s->data = e;
s->next = p->next;
p->next = s;
return true;
}//单链表插入
bool ListDelete(Linklist& L, int i, int e)
{
if (i < 1 || L->next == NULL)
return false;
Lnote* p,*q;
p = GetElem(L, i-1);
q = p->next;
p->next = q->next;
free(q);
}//删除节点
int Listlength(Linklist& L,int &i)
{
i = 0;
Lnote* p = L->next;
if (p == NULL)
return 0;
while (p != NULL)
i++;
return i;
}//求表长
void test()
{
Linklist L;
InitList (L);
Empty(L);
TailInsert(L);
HeadInsert(L);
int i;
scanf("%d", i);
GetElem(L,i);
int e;
scanf("%d", e);
LocateElem(L, e);
}
单链表的知识点(写的伪代码)
最新推荐文章于 2024-06-14 10:22:04 发布