#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
//3 4 5 6 7 9 10 -9999
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode,*Linklist;
bool initList(Linklist& L)
{
L = (LNode *)malloc(sizeof(LNode));
if (NULL == L)
{
return false;
}
L->next = NULL;
return true;
}
bool Empty(Linklist L)
{
return (NULL == L->next);
}
LNode* GetElem(Linklist L, int i)
{
if (i < 0)
{
return NULL;
}
int j = 0;
LNode* p = L;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
LNode* locateElem(Linklist L, ElemType e)
{
LNode* p = L->next;
while (p && p->data != e)
{
p = p->next;
}
return p;
}
bool InsertNextNode(LNode* p,ElemType e)//当前节点后插
{
if (NULL == p) return false;
LNode* q = (LNode*)malloc(sizeof(LNode));
if (NULL == q)
{
return false;
}
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
bool InsertPriorNode(LNode* p, ElemType e)
{
if (NULL == p) return false;
LNode* q = (LNode*)malloc(sizeof(LNode));
if (NULL == q) return false;
q->next = p->next;
p->next = q;
q->data = p->data;
p->data = e;
return true;
}
//当传入的为一个节点插入前一个节点时
bool InsertPriorNOde_2(LNode* p, LNode* q)
{
if (NULL == p || NULL == q)
return false;
q->next = p->next;
p->next = q;
ElemType temp = q->data;
q->data = p->data;
p->data = temp;
return true;
}
bool Delete_IndexNode(Linklist L, int i, ElemType& e)
{
LNode* p = GetElem(L,i-1);
if (p == NULL) return false;
LNode* q = p->next;
if (q == NULL) return false;
p ->next= q->next;
e = q->data;
free(q);
q = NULL;
return true;
}
bool Delete_ElemNode(Linklist L, ElemType e)//删除指定节点同理
{
LNode* p=locateElem(L, e);
if (NULL == p)
return false;
LNode* q = p->next;
if (q == NULL)//当要删除元素为最后一个元素时
{
LNode* s = L->next;
if (NULL == s)
{
return false;
}
while (s->next->data != e)
{
s = s->next;
}
if (NULL == s)
{
printf("no == e\n");
return false;
}
q = s->next;
s->next = NULL;
free(q);
q = NULL;
return true;
}
p->data = q->data;
p->next = q->next;
free(q);
q = NULL;
return true;
}
//按位序插入
bool List_IndexInsert(Linklist L, int i, ElemType e)//此处不用加&,删除的非头节点,插入删除不会改变头节点改变头部节点才需要引用
{
LNode* p = GetElem(L, i - 1);
InsertNextNode(p, e);
return true;
}
bool Listlength(Linklist L, int& len)
{
len = 0;
LNode* p = L->next;
if (NULL == p)
{
return true;
}
while (p)
{
len++;
p = p->next;
}
return true;
}
void printlist(Linklist L)
{
L = L->next;
if (L == NULL)
{
printf("空链表\n");
return;
}
while (L)
{
printf("%4d", L->data);
L = L->next;
}
printf("\n");
}
bool List_HeadInsert(Linklist& L)
{
int number;
printf("INPUT number: \n");
scanf("%d", &number);
while (number!=-9999)
{
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->data = number;
s->next = L->next;
L->next = s;
scanf("%d", &number);
}
return true;
}
bool List_TailInsert(Linklist& L)
{
int number;
LNode* p = L;
printf("INPUT number: \n");
scanf("%d", &number);
while (number != -9999)
{
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->data = number;
p->next = s;
p = s;
scanf("%d", &number);
}
p->next = NULL;
return true;
}
int main()
{
Linklist L; int len=0;
//if-else 省略
initList(L);
if (Empty(L))
{
printf("Empty List\n");
}
List_TailInsert(L);
printlist(L);
Listlength(L, len);
printf("List_length=%d\n",len);
List_IndexInsert(L, 3, 60);
printlist(L);
LNode* p=GetElem(L,4);
InsertNextNode(p, 40);
printlist(L);
Listlength(L, len);
p = locateElem(L, 9);
InsertPriorNode(p, 70);
printlist(L);
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = 50;
InsertPriorNOde_2(p, s);
printlist(L);
ElemType e = -1;
Delete_IndexNode(L, 1, e);
printlist(L);
Delete_ElemNode(L, 10);
printlist(L);
Listlength(L, len);
printf("List_length=%d\n", len);
return 0;
}
单链表_02_带头结点(自用)
于 2022-03-20 10:46:29 首次发布