#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode, *LinkList; //LNode : 结构体类型,LinkList :结构体指针;
typedef enum {
false = 0,
true = 1,
}bool;
//LNode* 等价于 LinkList
//头插法
LinkList CreatList1(LinkList L) {
LinkList s; int x;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
scanf_s("%d", &x); //输入X的值
while (x != 99) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x; //给第一个节点赋值
s->next = L->next; //设置节点后为NULL
L->next = s; //头插
scanf_s("%d", &x);
}
return L;
}
//尾插法
LinkList CreatList2(LinkList L) {
LinkList s;
L = (LinkList)malloc(sizeof(LNode));
LinkList r = L;
int x;
scanf_s("%d", &x);
while (x != 99) {
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s; //首次放在头节点之后,之后放在最后一个节点后
r = s; //r等于最后一个节点
scanf_s("%d", &x);
}
r->next = NULL; //下一个节点为NULL
return L;
}
//输出链表
void PrintList(LinkList L) {
LinkList r;
r = L->next;
while (r != NULL) {
printf("%d\n", r->data);
r = r->next;
}
}
//输出链表长度(不包括头结点)
int Length(LinkList L) {
int length = 0;
LinkList r;
r = L->next;
while (r != NULL) {
length++;
r = r->next;
}
return length;
}
//判断是否为空
bool Empty(LinkList L) {
if (L->next == NULL) {
printf("List Is Empty\n");
return false;
}
else {
printf("List Is Not Empty\n");
return true;
}
}
//删除整个链表
void DestroyList(LinkList L) {
LinkList s = L->next;
LinkList head = L->next;
printf("List Has Been Destroyed!\n");
while (head != NULL) {
head = head->next;
free(s);
s = head;
}
L->next = NULL;
}
int ListDelete(LinkList L, int i) {
if (i<1 || i>Length(L)) {
printf("Index Is Longer Then List Length!\n");
return -1;
}
int index = 0;
LinkList r = L, s;
while (index != i - 1) {
r = r->next;
index++;
}//r处于第i-1个节点,index=i-1
s = r->next;
index = s->data;
r->next = r->next->next;
free(s);
return index;
}
//插入元素
bool ListInsert(LinkList L, int i, int e) {
int index = 0;
LinkList r = L;
LinkList s;
int length = Length(L);
if (i<1 || i>length + 1) {
printf("Index Is Longer Then List Length!\n");
return false;
}
while (index != i - 1) {
r = r->next;
index++;
}//r处于第i-1个节点,index=i-1
s = (LNode*)malloc(sizeof(LNode));
s->data = e;
//正常情况
if (i<length + 1) {
s->next = r->next;
r->next = s;
}
//如果插入位置为最后一位
else {
r->next = s;
s->next = NULL;
}
return true;
}
//得到指定位置的值
LinkList GetElem(LinkList L, int i) {
if (i<1 || i>Length(L)) {
printf("Index Is Longer Then List Length!\n");
return L;
}
int index = 0;
LinkList r = L;
while (index != i) {
r = r->next;
index++;
}//r处于第i个节点,index=i
// return r->data;
return r;
}
void main() {
int index, i, e;
LinkList r, L = NULL;
while (1) {
printf("+-----------------------------------+\n");
printf("| Choose The Number: |\n");
printf("| 1.Creat List |\n");
printf("| 2.Print All Elem Of List |\n");
printf("| 3.Print List Length |\n");
printf("| 4.Is List Empty? |\n");
printf("| 5.Insert Elem In List's Index |\n");
printf("| 6.Delete Index In List |\n");
printf("| 7.Get Index In List |\n");
printf("| 8.Destroy List |\n");
printf("| 9.Exit |\n");
printf("+-----------------------------------+\n");
printf("Input Your Choice : \n");
scanf_s("%d", &index); printf("============================================\n");
switch (index) {
//Creat List
case 1:
if(L == NULL||L->next==NULL){
printf("Input '99' To Finish Input!\n");
L = CreatList2(L); printf("============================================\n");
}
else{
printf("Please Confirm List Is Empty!\n"); printf("============================================\n");
}
break; /* 可选的 */
//Print All Elem List
case 2:
if (Length(L)>0)
{
PrintList(L); printf("============================================\n");
}
else {
printf("List is Empty\n"); printf("============================================\n");
}
break; /* 可选的 */
//Print List Length
case 3:
printf("List Length : %d\n", Length(L)); printf("============================================\n");
break; /* 可选的 */
//Is List Empty?
case 4:
Empty(L); printf("============================================\n");
break; /* 可选的 */
//Insert Elem In List's Index
case 5:
printf("Input Insert Index : \n");
scanf_s("%d", &i);
printf("Input Insert Elem : \n");
scanf_s("%d", &e);
ListInsert(L, i, e);
printf("============================================\n");
break; /* 可选的 */
//Delete Index In List
case 6:
printf("Input Delete Index : \n");
scanf_s("%d", &i);
ListDelete(L, i); printf("============================================\n");
break; /* 可选的 */
//Get Index In List
case 7:
printf("Input Index : \n");
scanf_s("%d", &i);
r = GetElem(L, i);
if (r!=L){
printf("This Elem is %d\n", r->data); printf("============================================\n");
}
break; /* 可选的 */
case 8:
DestroyList(L);
break; /* 可选的 */
/* 您可以有任意数量的 case 语句 */
default: /* 可选的 */
exit(0);
break;
//statement(s);
}
}
}