单链表
链表乃C基本数据结构。
参考:
严蔚敏- 数据结构
直接上代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* 单链表
* @return
*/
#define LIST_SIZE 100
#define LIST_INCREMENT 20
typedef char * ElementType;
typedef struct{
ElementType data;
struct LNode *next;
}LNode, *LnPtr;
typedef struct{
LNode *head; //带头指针的顺序链表
int size;
}LinkList;
typedef enum{
false,
true
}bool;
bool initList(LinkList *list)
{
list->head = (LNode *)malloc(sizeof(LNode)*LIST_SIZE);
if(NULL == list->head)
{
printf("Memory allocated Failure!\n");
exit(-1);
}
LnPtr head = list->head;
/* head->data = (ElementType)malloc(sizeof(ElementType));
if(!head->data)
{
printf("head node allocated memory failure!\n");
exit(-1);
}*/
list->size = 0;
list->head->next = NULL;
return true;
}
bool isFull(LinkList *list)
{
bool flag = (list->size >= LIST_SIZE) ? true : false;
return flag;
}
bool isEmpty(LinkList *list)
{
return (list->size == 0) ? true : false;
}
/**
* 在第i个位置之前插入元素
* @param list
* @param i
* @param e
* @return
*/
bool insert(LinkList *list, int i, ElementType e)
{
if(i > list->size -1)
{
printf("the Index of list i: %d is overflow!\n");
return false;
}
if(isEmpty(list))
{
list->head->data = e;
}
else
{
LnPtr p = list->head;
int j=0;
while(NULL != p && j < i-1)
{
p = p->next;
j++;
}
LnPtr newPtr = (LNode *)malloc(sizeof(LNode));
if(NULL == newPtr)
{
printf("Memory allocate Failure!\n");
exit(-1);
}
newPtr->data = e;
newPtr->next = p->next;
p->next = newPtr;
}
list->size++;
if(isFull(list))
{
list->head = (LNode *)realloc(list->head,
(list->size + LIST_INCREMENT)*sizeof(ElementType));
if(NULL == list->head)
{
printf("Memory relloc Failure!\n");
exit(-1);
}
// list->size += LIST_INCREMENT;
}
return true;
}
/**
* 删除第i个元素
* @param list
* @param i
* @return
*/
bool delete(LinkList *list, int i)
{
if(isEmpty(list))
{
printf("List is empty!\n");
return false;
}
if(i > list->size -1)
{
printf("the Index of list i: %d is overflow!\n");
return false;
}
LnPtr p = list->head;
LnPtr q;
int j = 0;
while(NULL != p && j < i-1)
{
p = p->next;
j++;
}
q = p->next;
p->next = q->next;
ElementType e = q->data;
q->next = NULL;
free(q);
printf("No: %d Element value is %s has deleted!\n",i,e);
return true;
}
/**
* 在链表尾部顺序插入元素
* @param list
* @param e
* @return
*/
bool append(LinkList *list, ElementType e)
{
LNode *p = list->head;
if(isEmpty(list))
{
p->data = e;
}
else
{
LNode *q = NULL;
while(NULL != p)
{
q = p;
p = p->next;
}
LnPtr nodePtr = (LnPtr)malloc(sizeof(LNode));
if(NULL == nodePtr)
{
printf("Memory allocated Failure!\n");
exit(-1);
}
nodePtr->data = e;
q->next = nodePtr;
nodePtr->next = NULL;
}
list->size++;
if(isFull(list))
{
list->head = (LNode *)realloc(list->head,
(list->size + LIST_INCREMENT)*sizeof(ElementType));
if(NULL == list->head)
{
printf("Memory relloc Failure!\n");
exit(-1);
}
// list->size += LIST_INCREMENT;
}
return true;
}
/**
* 查找
* 返回指向节点的指针
* @param list
* @param e
* @return
*/
LnPtr find(LinkList *list, ElementType e)
{
LnPtr p = NULL;
if(isEmpty(list))
{
printf("list is Empty!\n");
return NULL;
}
p = list->head;
bool flag = false;
while(NULL !=p)
{
if(p->data == e)
{
flag = true;
break;
}
p = p->next;
}
if(!flag)
{
printf("Not Found Element: %s.\n", e);
return NULL;
}
printf("Element %s Node ptr is: %p.\n", e, p);
return p;
}
/**
* 打印
* @param list
*/
void traverse(LinkList *list)
{
if(NULL == list)
{
printf("list is null!\n");
exit(-1);
}
if(isEmpty(list))
{
printf("list is empty!\n");
return;
}
LNode *p = list->head;
while(NULL !=p){
printf("Element value is: %s.\n",p->data);
p = p->next;
}
}
int main() {
LinkList *list = (LinkList *)malloc(sizeof(LinkList));
if(NULL == list)
{
printf("Memory allocated Failure!\n");
exit(-1);
}
initList(list);
append(list, "zhao");
append(list, "qian");
append(list, "li");
insert(list,2, "sun");
delete(list,1);
find(list, "li");
traverse(list);
printf("done!\n");
return 0;
}