#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 = NULL;
return true;
}
bool Empty(Linklist L)
{
return (NULL == L);
}
LNode* Get_Index_p(Linklist L, int i)
{
if (i < 1) return NULL;
if (NULL == L) return NULL;
LNode* p = L;
int j = 1;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
LNode* LocateElem(Linklist L, ElemType e)
{
if (NULL == L) return NULL;
LNode* p = L;
while (p&&p->data!=e)
{
p = p->next;
}
return p;
}
bool InsertNextNode(LNode* p, ElemType e)//!
{
if (NULL == p) return false;
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool InsertPriorNode(LNode* p, ElemType e)//!
{
if (NULL == p) return false;
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->next = p->next;
p->next = s;
s->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)
{
if (i < 1 || NULL == L) return false;
LNode* q = L;
if (1 == i)
{
L = L->next;
free(q);
q = NULL;
}
else
{
LNode* p=Get_Index_p(L, i - 1);
if (NULL == p || p->next == NULL) return false;
q = p->next;
p->next = q->next;
e = q->data;
free(q);
q = NULL;
}
return true;
}
bool Delete_ElemNode(Linklist& L, ElemType e)
{
if (NULL == L) return false;
LNode* p = LocateElem(L, e);
if (NULL == p)
return false;
LNode* q = p->next;
if (NULL == q)
{
LNode* s = L;
while (s->next->data!=e)
{
s = s->next;
}
if (NULL == s) 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)
{
if (i < 1) return false;
if (i == 1)
{
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->data = e;
s->next = L;
L = s;
return true;
}
LNode* p = Get_Index_p(L, i - 1);
InsertNextNode(p, e);
return true;
}
bool Listlength(Linklist L, int& len)
{
len = 0;
if (L == NULL) return true;
LNode* p = L;
while (p)
{
len++;
p = p->next;
}
return true;
}
bool List_TailInsert(Linklist& L)
{
ElemType number;
L = (LNode*)malloc(sizeof(LNode));
L->data = -9999;
L->next = NULL;
LNode* p = L;
LNode* s;
printf("INPUT number: \n");
scanf("%d", &number);
while (number != -9999)
{
s = (LNode*)malloc(sizeof(LNode));
if (NULL == s)
return false;
s->data = number;
if (-9999 == L->data)//代表要输入的为第一个节点数据(不带头节点情况下)
{
L->data = s->data;
}
else
{
p->next = s;
p = s;
}
scanf("%d", &number);
}
p->next = NULL;
return true;
}
bool List_HeadInsert(Linklist& L)
{
ElemType number;
printf("INPIUT number:\n");
scanf("%d", &number);
while (-9999!=number)
{
LNode* s = (LNode*)malloc(sizeof(LNode));
if (NULL == s) return false;
s->data = number;
if (NULL == L)
{
s->next = NULL;
}
else
s->next = L;
L = s;
scanf("%d", &number);
}
return true;
}
bool printList(Linklist L)
{
if (NULL==L) return false;
while (L&&L->data!=-9999)
{
printf("%4d", L->data);
L = L->next;
}
printf("\n");
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 = Get_Index_p(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;
}
单链表_不带头结点(自用)
于 2022-03-21 11:00:06 首次发布