LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef enum {TRUE, FALSE, ERROR} BOOL;
typedef int Data;
typedef struct _node
{
Data data; // 数据域
struct _node *next; // 指针域
}Node;
typedef struct _list
{
Node *head;
}
List;
// 创建链表
List* CreateList();
//头插
BOOL Insert_Head(List *ls,Data data);
//尾插
BOOL Insert_Last(List *ls,Data data);
//插入:在特定的位置插入
//pos:第pos个结点位置
BOOL Insert_Pos(List *ls,int pos,Data data);
//删除特定位置的数据
//pos:第pos个结点位置
BOOL Delete_Pos(List *ls,int pos);
//根据值删除数据
BOOL Delete_Data(List *ls,Data data);
// 链表逆序
BOOL Reverse(List *ls);
void Destroy(List *ls);
void Display(List *ls);
#endif // _LINKLIST_H_
main.c
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建链表失败\n");
}
printf ("创建链表成功\n");
int i;
for(i=0;i<10;i++)
{
//Insert_Head(ls,i);
Insert_Last(ls,i);
}
Display(ls);
Insert_Pos(ls,1,123);
Display(ls);
Insert_Pos(ls,12,123);
Display(ls);
printf("------------\n");
Delete_Pos(ls,1);
Display(ls);
Delete_Pos(ls,12);
Display(ls);
Delete_Data(ls,2);
Display(ls);
Delete_Data(ls,11);
Display(ls);
Reverse(ls);
Display(ls);
Destroy(ls);
return 0;
}
LinkList.c
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List* CreateList()
{
List *ls = (List*)malloc(sizeof(List)/sizeof(char));
if (NULL == ls)
return NULL;
// 创建头节点
ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == ls->head)
{
free(ls);
return NULL;
}
ls->head->next=NULL;//空链表
return ls;
}
BOOL Insert_Head(List *ls,Data data)
{
if (NULL == ls)
return ERROR;
Node *node =(Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL==node)
return ERROR;
node->data=data;
node->next=ls->head->next;
ls->head->next=node;
return TRUE;
}
BOOL Insert_Last(List *ls,Data data)
{
if (NULL == ls)
return ERROR;
Node *node =(Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL==node)
return ERROR;
node->data=data;
node->next=NULL;
Node *tmp=ls->head;
while(tmp->next)//while里面是tmp->next,设定tmp=ls->hesd
{
tmp=tmp->next;
}
tmp->next=node;
return TRUE;
}
BOOL Insert_Pos(List *ls,int pos,Data data)
{
if (NULL == ls||pos<1)
return ERROR;
Node *tmp=ls->head;
int i;
for(i=0;i<pos-1;i++)
{
tmp=tmp->next;
if(tmp==NULL)
{
printf("长度越界:%d\n",pos);
return ERROR;
}
}
Node *node=(Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL==node)
return ERROR;
node->data=data;
node->next=tmp->next;
tmp->next=node;
return TRUE;
}
BOOL Delete_Pos(List *ls,int pos)
{
if (NULL == ls||pos<1)
return ERROR;
Node *tmp=ls->head;
int i;
for(i=0;i<pos-1;i++)
{
tmp=tmp->next;
if(tmp==NULL||tmp->next==NULL)
{
printf("长度越界:%d\n",pos);
return ERROR;
}
Node *p=tmp->next;
tmp->next=p->next;
free(p);
return TRUE;
}
}
BOOL Delete_Data(List *ls,Data data)
{
if (NULL == ls)
return ERROR;
Node *tmp=ls->head;
while(tmp->next)
{
if(tmp->next->data==data)
{
Node *p=tmp->next;
tmp->next=p->next;
free(p);
return TRUE;
}
tmp=tmp->next;
}
}
BOOL Reverse(List *ls)
{
// NULL ==ls || NULL == ls->head 链表不存在
// NULL == ls->head->next 空链表
// NULL == ls->head->next->next 只有一个结点的链表
if (NULL == ls || NULL == ls->head || NULL == ls->head->next || NULL == ls->head->next->next)
return ERROR;
Node *pre = ls->head->next; // 第一个结点
Node *cur = pre->next; // 第二个结点
Node *tmp; // 保存当前结点的下一个结点
while (cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
ls->head->next->next = NULL;
ls->head->next = pre;
return TRUE;
}
void Display(List *ls)
{
if (NULL == ls)
return ;
Node *tmp=ls->head->next;//while里面是tmp,设定tmp=ls->hesd->next
while(tmp)
{
printf("%-4d",tmp->data);
tmp=tmp->next;
}
printf("\n");
}
void Destroy(List* ls)
{
if (NULL == ls)
return;
Node *tmp=ls->head;
while(tmp->next)
{
Node *p=tmp->next;
tmp->next=p->next;
free(p);
}
free(ls->head);
free(ls);
}