单链表
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define MAX_QSIZE 5
typedef int elemType;
typedef struct lNode
{
elemType data;
struct lNode * next;
}* linkListPtr;
void initList(linkListPtr lPtr)
{
lPtr = (linkListPtr)malloc(sizeof(lNode));
if(!lPtr)
{
exit(OVERFLOW);
}
lPtr->next = NULL;
}
bool listInsert(linkListPtr lPtr, int i, elemType val)
{
int j = 0;
linkListPtr s, p = lPtr;
while(p && j<i-1)
{
j++;
p = p->next;
}
if(!p || j > i-1)
{
return ERROR;
}
s = (linkListPtr)malloc(sizeof(lNode));
if(!s)
{
exit(OVERFLOW);
}
s->data = val;
s->next = p->next;
p->next = s;
return OK;
}
bool deleteList(linkListPtr lPtr, int i, elemType * val)
{
linkListPtr s, p = lPtr;
int j = 0;
while(p && j<i-1)
{
p = p->next;
j++;
}
if(!p || j > i-1)
{
return ERROR;
}
s = p->next;
p->next = s->next;
*val = s->data;
free(s);
return OK;
}
void listTraverse(linkListPtr lPtr, void(* visit)(elemType))
{
linkListPtr p = lPtr->next;
while(p != NULL)
{
visit(p->data);
p = p->next;
}
printf("\n");
}
void visit(elemType e)
{
printf("%d, ", e);
}
void destoryList(linkListPtr lPtr)
{
linkListPtr p ;
while(lPtr)
{
p = lPtr->next;
free(lPtr);
lPtr = p;
}
}
bool listEmpty(linkListPtr lPtr)
{
if(lPtr->next == NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
void clearList(linkListPtr lPtr)
{
linkListPtr p = lPtr->next;
lPtr->next = NULL;
destoryList(p);
}
int listLength(linkListPtr lPtr)
{
int i = 0;
linkListPtr p = lPtr->next;
while(p)
{
i++;
p = p->next;
}
return i;
}
bool getElement(linkListPtr lPtr, int i, elemType * val)
{
if(listEmpty(lPtr))
{
return FALSE;
}
linkListPtr p = lPtr->next;
int j = 0;
while(p && j<i-1)
{
j++;
p = p->next;
}
if(p && j == i-1)
{
*val = p->data;
return TRUE;
}
else
{
return FALSE;
}
}
int locateElement(linkListPtr lPtr, elemType val)
{
linkListPtr p = lPtr->next;
int j = 1;
while(p)
{
if (p->data == val)
{
return j;
}
else
{
j++;
p = p->next;
}
}
return 0;
}
bool priorElement(linkListPtr lPtr, elemType curVal, elemType *preVal)
{
linkListPtr p = lPtr;
while(p && p->next)
{
if(p->next->data == curVal)
{
*preVal = p->data;
return OK;
}
else
{
p = p->next;
}
}
return ERROR;
}
bool nextElement(linkListPtr lPtr, elemType curVal, elemType *nextVal)
{
linkListPtr p = lPtr->next;
while(p && p->next)
{
if(p->data == curVal)
{
*nextVal = p->next->data;
return OK;
}
p = p->next;
}
return ERROR;
}
int main(int argc, char const *argv[])
{
linkListPtr lPtr;
bool bl;
elemType e;
elemType preVal;
elemType nextVal;
initList(lPtr);
for(int i=1; i<=10; i++)
{
listInsert(lPtr, 1, i);
}
listTraverse(lPtr, visit);
printf("list length is:%d\n", listLength(lPtr));
bl = getElement(lPtr, 2, &e);
printf("element 2 is:%d\n", e);
printf("the location of element 7 is:%d\n", locateElement(lPtr, 7));
printf("the location of element 70 is:%d\n", locateElement(lPtr, 70));
bl = priorElement(lPtr, 5, &preVal);
printf("the prior element of 5 is %d\n", preVal);
bl = nextElement(lPtr, 5, &nextVal);
printf("the next element of 5 is %d\n", nextVal);
bl = deleteList(lPtr, 2, &e);
printf("delete element e = %d\n", e);
listTraverse(lPtr, visit);
printf("Is the list is empty?:%d\n", listEmpty(lPtr));
clearList(lPtr);
printf("Is the list is empty?:%d\n", listEmpty(lPtr));
destoryList(lPtr);
printf("Success!!!\n");
return 0;
}