下面是完整的代码:
/*
* Data Structure
* this file is the implementation of the Linked List
* author: John Woods
* date: 2015/5/7
* statement: anyone can use this file for any purpose
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
/* structure of the node */
typedef struct LLNode {
int data;
struct LLNode * next;
} * LLNode;
/* structure of the linked list */
typedef struct LinkedList {
LLNode Head;
int Length;
}* LinkedList;
/* menu declaration */
void menu();
void insert(LinkedList MyLL);
void delete(LinkedList MyLL);
void current(LinkedList MyLL);
/* operations declaration */
void listInit(LinkedList * MyLL);
void listInsert(LinkedList MyLL, int index, int value);
BOOL listDelete(LinkedList MyLL, int index, int * value);
void listTraverse(LinkedList MyLL);
void listClear(LinkedList MyLL);
void listDestroy(LinkedList * MyLL);
BOOL getElem(LinkedList MyLL, int index, int * value);
BOOL isExist(LinkedList MyLL);
int main(void) {
LinkedList MyLL = NULL;
int choice;
char c;
while(TRUE) {
menu();
while(!scanf("%d", &choice)) while('\n' != (c=getchar()) && EOF != c);
switch(choice) {
case 1:
listInit(&MyLL);
break;
case 2:
insert(MyLL);
break;
case 3:
delete(MyLL);
break;
case 4:
current(MyLL);
break;
case 5:
listTraverse(MyLL);
break;
case 6:
listClear(MyLL);
break;
case 7:
listDestroy(&MyLL);
break;
default:
exit(EXIT_SUCCESS);
break;
}
}
return 0;
}
/* menu implemention */
void menu() {
printf("\n\t**********************MENU**********************\n");
printf("\t* 1.create a linked list 2.insert an element *\n");
printf("\t* 3.delete an element 4.get an element *\n");
printf("\t* 5.traverse the list 6.clear the list *\n");
printf("\t* 7.destroy the list 8.exit *\n");
printf("\t**********************MENU**********************\n");
printf("Give me your choice:");
}
void insert(LinkedList MyLL) {
int index, value;
char c;
while(TRUE) {
printf("Please input the position:");
while('\n' != (c=getchar()) && EOF != c);
while(!scanf("%d", &index)) while('\n' != (c=getchar()) && EOF != c);
printf("Please input the value:");
while('\n' != (c=getchar()) && EOF != c);
while(!scanf("%d", &value)) while('\n' != (c=getchar()) && EOF != c);
listInsert(MyLL, index, value);
while('\n' != (c=getchar()) && EOF != c);
printf("Go on?(y/n):");
c = getchar();
if(c != 'y' && c != 'Y') break;
}
}
void delete(LinkedList MyLL) {
int index, value;
char c;
while(TRUE) {
printf("Please input the position:");
while('\n' != (c=getchar()) && EOF != c);
while(!scanf("%d", &index)) while('\n' != (c=getchar()) && EOF != c);
if(listDelete(MyLL, index, &value)) {
printf("The deleted value is %d in position %d\n", value, index);
}else {
printf("Delete unsuccessfully!\n");
}
while('\n' != (c=getchar()) && EOF != c);
printf("Go on?(y/n):");
c = getchar();
if(c != 'y' && c != 'Y') break;
}
}
void current(LinkedList MyLL) {
int index, value;
char c;
printf("Please input the position:");
while('\n' != (c=getchar()) && EOF != c);
while(!scanf("%d", &index)) while('\n' != (c=getchar()) && EOF != c);
if(getElem(MyLL, index, &value)) {
printf("The value in position %d is %d\n", index, value);
}
}
/* operations implemention */
void listInit(LinkedList * MyLL) {
if(isExist(*MyLL)) {
printf("Linked list already exist!\n");
return;
}
if(NULL == (*MyLL = (LinkedList)malloc(sizeof(struct LinkedList)))) {
printf("Out of Memory!\n");
exit(EXIT_FAILURE);
}
(*MyLL)->Length = 0;
(*MyLL)->Head = NULL;
printf("Initial successfully!\n");
}
void listInsert(LinkedList MyLL, int index, int value) {
if(!isExist(MyLL)) {
printf("This linked list does not exist! Please initial it first!\n");
return;
}
if(index > MyLL->Length+1 || index < 1) {
printf("You input wrong index!\n");
}
LLNode pInsert = NULL, pNode = MyLL->Head;
if(NULL == (pInsert = (LLNode)malloc(sizeof(struct LLNode)))) {
printf("Out of memory!\n");
exit(EXIT_FAILURE);
}
pInsert->data = value;
pInsert->next = NULL;
if(1 == index) {
pInsert->next = MyLL->Head;
MyLL->Head = pInsert;
}else {
--index;
while(--index) pNode = pNode->next;
pInsert->next = pNode->next->next;
pNode->next = pInsert;
}
MyLL->Length++;
pNode = NULL;
pInsert = NULL;
printf("Insert successfully!\n");
}
BOOL listDelete(LinkedList MyLL, int index, int * value) {
if(!isExist(MyLL)) {
printf("This linked list does not exist! Please initial it first!\n");
return FALSE;
}
if(index > MyLL->Length || index < 1) {
printf("You input wrong index!\n");
return FALSE;
}
LLNode pDelete = NULL, pNode = MyLL->Head;
if(1 == index) {
pDelete = MyLL->Head;
MyLL->Head = MyLL->Head->next;
}else {
--index;
while(--index) pNode = pNode->next;
pDelete = pNode->next;
pNode->next = pNode->next->next;
}
MyLL->Length--;
*value = pDelete->data;
free(pDelete);
pNode = NULL;
pDelete = NULL;
printf("Delete successfully!\n");
return TRUE;
}
void listTraverse(LinkedList MyLL) {
if(!isExist(MyLL)) {
printf("This linked list does not exist! Please initial it first!\n");
return;
}
LLNode pNode = MyLL->Head;
int Len = MyLL->Length;
printf("Length of this linked list is %d\nIts elements are following:\n", MyLL->Length);
printf("\tHead->");
while(pNode && Len) {
printf("%d->", pNode->data);
pNode = pNode->next;
Len--;
}
printf("End\n");
}
void listClear(LinkedList MyLL) {
if(!isExist(MyLL)) {
printf("This linked list does not exist! Please initial it first!\n");
return;
}
LLNode pDestroy = NULL, pNode = MyLL->Head;
while(pNode) {
pDestroy = pNode;
pNode = pNode->next;
free(pDestroy);
}
MyLL->Length = 0;
printf("This linked list is empty now!\n");
}
void listDestroy(LinkedList * MyLL) {
if(!isExist(*MyLL)) {
printf("This linked list does not exist!\n");
return;
}
listClear(*MyLL);
free(*MyLL);
*MyLL = NULL;
printf("This linked list has been destroyed successfully!\n");
}
BOOL getElem(LinkedList MyLL, int index, int * value) {
if(!isExist(MyLL)) {
printf("This linked list does not exist! Please initial it first!\n");
return FALSE;
}
if(index > MyLL->Length || index < 1) {
printf("You got wrong index!\n");
return FALSE;
}
LLNode pNode = MyLL->Head;
while(--index) pNode = pNode->next;
*value = pNode->data;
return TRUE;
}
BOOL isExist(LinkedList MyLL) {
if(NULL == MyLL) {
return FALSE;
}else {
return TRUE;
}
}
如果不加链表长度的控制条件,遍历清空之后的链表任然会按照之前的长度进行访问,只不过数据是有问题的,所以需要加长度控制!不知道还有没有其他的方法来解决这个问题!