#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define Elemtype int
typedef int Status;
typedef struct LNode {
Elemtype data;
struct LNode *next;
}LNode, *LinkList;
//No.1 Init list
LinkList InitList()
{
LinkList L = NULL;
L = (LinkList)malloc(sizeof(LNode));
if (!L)
{
printf("***** Init Link list failed ! **** \n");
exit(OVERFLOW);
}
L->next = NULL;
printf("Init Link list ...\n");
return L;
}
//No.2 Destroy List
void DestoryList(LinkList L)
{
LinkList p = L;
if (p == NULL)
{
free(p);
}
while (p)
{
L = p->next;
free(p);
p = L;
}
}
// No.3 ClearList
void ClearList(LinkList L)
{
LinkList p = L;
if (p == NULL)
{
printf("List is NULL, clear list done !\n");
exit(-1);
}
while ( p->next != NULL)
{
p = L->next;
free(L);
L = p;
}
printf("clear list done !\n");
}
//No.4 List empty
Status ListEmpty(LinkList L)
{
LinkList p = L->next;
if ( p == NULL)
{
printf("Link is empty !\n");
return FALSE;
}
else
{
return TRUE;
printf("Link is not empty !\n");
}
}
//No.5 List length, include Head number
int ListLength(LinkList L)
{
int i = 0;
LinkList p =L;
if ( p == NULL)
{
printf("Link is empty !\n");
return FALSE;
}
while (p != NULL)
{
p = p->next;
++i;
}
return i;
}
//No.6 GetElem
Status GetElem_L(LinkList L, int i, Elemtype *e)
{
int j=1;
LinkList p = NULL;
p = L->next;
if (p && j < i)
{
p =p->next;
j++;
}
if (!p->next || j > i)
return ERROR;
*e = p->next->data;
printf("node %d value : %d\n",i,*e);
return OK;
}
//No.7 LocateElem
int LocateElem(LinkList L, Elemtype e)
{
LinkList p;
int i = 1;
p = L->next;
while ( p != NULL && p->data != e)
{
p = p->next;
++i;
}
if (p)
{
printf("system has found %d in %d node !\n",e,i);
return i;
}
else
{
printf("%d is not in Link List !\n", e);
return 0;
}
}
//No.8 PriorElem
int PriorElem(LinkList L, Elemtype current_value, Elemtype *prior_value)
{
LinkList p,q;
p = L->next;
q = p;
if (p ==NULL)
{
printf("link list is empty, no Prior !\n");
exit(-1);
}
while ( p != NULL && p->next->data != current_value)
{
p = p->next;
}
if (p->next->data == current_value)
{
*prior_value = p->data;
printf("prior node value = %d\n",*prior_value);
}
return OK;
}
Status ListInsert_L(LinkList L, int i, Elemtype e)
{
int j = 1;
LinkList q = (LinkList) malloc(sizeof(LNode));
if ( !q )
{
printf("system can not malloc memory !\n");
return (OVERFLOW);
}
LinkList p = NULL;
p = L->next;
if (p && j < i-1)
{
p =p->next;
j++;
}
if (!p->next || j > i-1)
return ERROR;
q -> data = e;
q->next = p->next;
p->next = q;
return OK;
}
Status ListDelete_L(LinkList L, int i, Elemtype *e)
{
int j = 1;
LinkList q,p = NULL;
p = L->next;
if (p && j < i-1 )
{
p =p->next;
j++;
}
if (!p->next || j > i -1)
return ERROR;
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
return OK;
}
void CreateList_L(LinkList L)
{
int i,j,e;
int len;
LinkList p,q;
q = L;
q->next = NULL;
printf("please input List len = ");
scanf("%d",&len);
/* 頭插法
for (i = len; i > 0; --i)
{
j = len -i + 1;
p =(LinkList)malloc(sizeof(LNode));
if (!p)
exit (OVERFLOW);
printf("please input data for Link list %d: ", j);
scanf("%d", &e);
p->data = e;
p->next = L->next;
L->next =p;
} */
// 尾插法
for (i = len; i > 0; --i)
{
j = len -i + 1;
p =(LinkList)malloc(sizeof(struct LNode));
if (!p)
exit (OVERFLOW);
printf("please input data for Link list %d: ", j);
scanf("%d", &e);
p->data = e;
q->next = p;
p->next = NULL;
q = p;
}
}
void Print(LinkList L)
{
int i=1;
LinkList p = L->next;
while (p != NULL)
{
printf("\n##### No.%d value : %d ####",i,p->data);
p = p->next;
i++;
}
printf("\n");
}
void InvertLink(LinkList L)
{
LinkList s,p = NULL;
p = L->next;
L->next = NULL;
while (p != NULL)
{
s = p;
p = p->next;
s->next = L->next;
L->next = s;
}
}
int main(void)
{
int n,e,f;
Elemtype locat_e,current_e;
Elemtype *prior_e;
LinkList S;
S = InitList();
CreateList_L(S);
Print(S);
GetElem_L(S, 3, &e);
printf("e : %d\n",e);
Print(S);
//ListInsert_L(S, 3, 34);
//Print(S);
//ListDelete_L(S, 4, &f);
//Print(S);
//InvertLink(S);
//Print(S);
//ListEmpty(S);
//ClearList(S);
//Print(S);
f = ListLength(S);
printf("List length = %d \n",f);
//printf("please input located elem : e = ");
//scanf("%d",&locat_e);
//LocateElem(S,locat_e);
Print(S);
printf("please input node value : ");
scanf("%d",¤t_e);
PriorElem(S,current_e, &prior_e);
return 0;
}