# 单链表ADT

—————————华丽的分割线——————————————

List.h:

#ifndef LIST_H
#define LIST_H

typedef char ElementType;

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;

List MakeEmpty(List L);
int IsLast(Position P, List L);
int IsEmpty(List L);
Position FindPrevious(List L, ElementType x);
Position Find(List L, ElementType x);
void Delete(List L, ElementType x);
void Insert(List L, ElementType x, Position P);
void InsertToTail(List L, ElementType x);
void InsertToHead(List L, ElementType x);
void PrintList(List L);
void DeleteList(List L);
void Reverse(List L);

#endif

SingleLinkedList.c:

#include"List.h"
#include<stdio.h>
#include<stdlib.h>

struct Node{
ElementType Element;
PtrToNode Next;
};

List MakeEmpty(List L)
{
if(NULL != L)
{
L->Next = NULL;
}
return L;
}

int IsLast(Position P, List L)//L is unused
{
return P->Next == NULL;
}

int IsEmpty(List L)
{
return L->Next == NULL;
}

Position FindPrevious(List L, ElementType x)
{
Position P;
P = L;
while(P->Next && P->Next->Element != x)
P = P->Next;
return P;
}

Position Find(List L, ElementType x)
{
Position P;
P = L->Next;
while(P && P->Element != x)
P = P->Next;
return P;
}

void Delete(List L, ElementType x)
{
Position Pre, Tmpcell;

Pre = FindPrevious(L, x);//we need find the previous of deleted element
if(!IsLast(Pre, L))
{
Tmpcell = Pre->Next;
Pre->Next = Tmpcell->Next;
free(Tmpcell);
}
}

//we insert the element after the position p
void Insert(List L, ElementType x, Position P)
{
Position NewCell;
TmpCell = (List)malloc(sizeof(struct Node));
if(NULL == NewCell)
printf("No space for allocation!!");
else
{
NewCell->Element = x;
NewCell->Next = P->Next;
P->Next = NewCell;
}
}
//插入链表尾部(尾插法)
void InsertToTail(List L, ElementType x)
{
Position Last, NewCell;
Last = L;
/*遍历链表找到最后一个结点*/
while(NULL != Last->Next)
Last = Last->Next;
Insert(L, x, Last);
}
//插入链表头部(头插法)
void InsertToHead(List L, ElementType x)
{
Insert(L, x, L);
}

void PrintList(List L)
{
PtrToNode Tmp;
Tmp = L->Next;
while(Tmp->Next)
{
printf("%c-", Tmp->Element);
Tmp = Tmp->Next;
}
printf("%c\n", Tmp->Element);
}

void DeleteList(List L)
{
Position Tmp, P;
P = L->Next;
L->Next = NULL;
while(P != NULL)
{
Tmp = P->Next;
free(P);
P = Tmp;
}
free(L);
}

void Reverse(List L)
{
Position CurrentPos, NextPos, PreviousPos;

CurrentPos = L->Next;//当前单链表的第一个节点
PreviousPos = NULL;//指向新链表的第一个节点，假设开始为空
while(CurrentPos != NULL)
{
NextPos = CurrentPos->Next;//取得当前节点的下一个节点位置
CurrentPos->Next = PreviousPos;//当前节点连接成新的链表
PreviousPos = CurrentPos;
CurrentPos = NextPos;//遍历到下一个节点
}
L->Next = PreviousPos;//哑元节点连接新链表的头节点
}
//与上述思想差不多，主要在返回上
/*Asumming(假如）is no header and L is not empty*/
//List Reverse(List L)
//{
//    Position CurrentPos, NextPos, PreviousPos;
//
//    CurrentPos = L;
//    PreviousPos = NULL;
//    while(CurrentPos != NULL)
//    {
//        NextPos = CurrentPos->Next;
//        CurrentPos->Next = PreviousPos;
//        PreviousPos = CurrentPos;
//        CurrentPos = NextPos;
//    }
//    return PreviousPos;
//}
//下面是复杂记忆写法
//void Reverse(List L)//含有头节点
//{
//    Position Tmp, P;
//    Tmp = L->Next;
//    L->Next = NULL;
//    while(Tmp != NULL)
//    {
//        P = Tmp->Next;
//        Tmp->Next = L->Next;
//        L->Next = Tmp;
//        Tmp = P;
//    }
//}
//List Reverse(List L)//不含头节点
//{
//    PtrToNode Tmp, P;
//    P = L;
//    L = NULL;
//    while(P != NULL){
//        Tmp = P->Next;
//        P->Next = L;
//        L = P;
//        P = Tmp;
//    }
//    return L;
//}

Test.c:

#include"List.h"
#include<stdio.h>
#include<stdlib.h>

int main()
{
ElementType Elem, De, PreElem, Ins;
Position Tmp;
List L;
L = (List)malloc(sizeof(struct Node));
if(NULL == L)
printf("Allocation failure!!!");
L = MakeEmpty(L);
printf("Please enter the element until the end of '#':");
while((Elem = getchar()) != '#')
{
InsertToTail(L, Elem);
}
getchar();
PrintList(L);
//删除并输出
printf("Please enter the element you want to delete:");
scanf("%c", &De);
getchar();

Delete(L, De);
PrintList(L);
//插入并输出
printf("After which element do you want to insert:");
scanf("%c", &PreElem);
getchar();
Tmp = Find(L, PreElem);

printf("What element do you want to insert:");
scanf("%c", &Ins);
getchar();

Insert(L, Ins, Tmp);
PrintList(L);
//将整个表倒置
Reverse(L);
printf("Now the reverse list is:");
PrintList(L);
//删除整个表
DeleteList(L);
return 0;
}

10-29 1294
04-02 238
12-29 4512
04-05 46
03-24 524
08-16 584
03-11 597
09-26 369
10-05 134
06-18 5424
10-08 1479
09-25 495
01-25 2304
04-17 210
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客