链表:
链表由两个部分组成,
1.数据域:用来存储数据
2.指针域:指向下一个元素
链表的创建,插入,删除:
头文件LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef int DATA;
typedef enum{TRUE,FALSE,ERROR}BOOL;
typedef struct _node
{
DATA data;
struct _node *next;
}Node;
typedef struct _list
{
Node *head;
}List;
//创建一个空链表
List *CreateList();
//删除链表
void Destory(List *pList);
//头插法
BOOL Insert_Head(List *pList,DATA n);
//打印链表
void Display(List *pList);
//尾插法
BOOL Insert_Last(List *pList,DATA n);
//指定位置插入
BOOL Insert_Pos(List *pList,DATA pos,DATA data);
//指定位置删除
BOOL Delete_Pos(List *pList,DATA pos);
//指定数值删除
BOOL Delete_data(List *pList,DATA n);
//指定位置查找
BOOL Find_data(List *pList,DATA pos);
BOOL Reverse_List(List *pList);
#endif //_LINKLIST_H_
功能函数LinkList.c
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List *CreateList()
{
List *p = (List*)malloc(sizeof(List)/sizeof(char));
if(NULL == p)
{
return NULL;
}
p->head = (Node*)malloc(sizeof(Node)/sizeof(char));
p->head->next = NULL;
if(NULL == p->head)
{
free(p);
return NULL;
}
return p;
}
BOOL Insert_Head(List *pList,DATA data)
{
if(NULL == pList)
{
return ERROR;
}
Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == p)
{
return ERROR;
}
p->data = data;
p->next = pList->head->next;
pList->head->next = p;
return TRUE;
}
BOOL Insert_Last(List *pList,DATA data)
{
if(NULL == pList)
{
return ERROR;
}
Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == p)
{
return ERROR;
}
p->data = data;
p->next = NULL;
Node *tmp = pList->head;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = p;
return TRUE;
}
BOOL Insert_Pos(List *pList,DATA pos,DATA data)
{
if(NULL == pList||pos<1)
{
return ERROR;
}
Node *p = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == p)
{
return ERROR;
}
p->data = data;
Node* tmp = pList->head;
int i;
for(i=0;i<pos-1;i++)
{
tmp = tmp->next;
if(NULL == tmp)
{
printf("下标越界\n");
return ERROR;
}
}
p->next = tmp->next;
tmp->next = p;
}
BOOL Delete_Pos(List *pList,DATA pos)
{
if(NULL == pList||pos<1)
{
return ERROR;
}
Node *tmp = pList->head;
int i;
for(i=0;i<pos-1;i++)
{
tmp = tmp->next;
if(NULL == tmp||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 *pList,DATA n)
{
if(NULL == pList)
{
return ERROR;
}
Node *tmp =pList->head;
while(tmp->next)
{
if(tmp->next->data == n)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
BOOL Find_data(List *pList,DATA pos)
{
if(NULL == pList)
{
return ERROR;
}
Node *tmp =pList->head;
int i;
for(i=0;i<pos;i++)
{
tmp = tmp->next;
if(NULL == tmp)
{
printf("下标越界\n");
return ERROR;
}
}
printf("------%d\n",tmp->data);
return TRUE;
}
BOOL Reverse_List(List *pList)
{
if(NULL == pList||NULL == pList->head||NULL == pList->head->next||NULL == pList->head->next->next)
{
return ERROR;
}
Node *pre = pList->head->next;
Node *cur = pre->next;
Node *tmp;
while(cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
pList->head->next->next =NULL;
pList->head->next = pre;
}
void Destory(List *pList)
{
if(NULL==pList)
{
return ;
}
Node *tmp = pList->head;
while(tmp->next)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(pList->head);
free(pList);
}
void Display(List *pList)
{
if(NULL==pList)
{
return ;
}
Node *tmp = pList->head->next;
while(tmp)
{
printf("%-4d",tmp->data);
tmp = tmp->next;
}
printf("\n");
}
主函数:
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *L = CreateList();
int i;
for(i=0;i<10;i++)
{
Insert_Last(L,i);
}
Display(L);
Insert_Pos(L,11,699);
Display(L);
Insert_Pos(L,1,99);
Display(L);
Insert_Pos(L,13,65);
Display(L);
Insert_Pos(L,14,85);
Display(L);
Delete_Pos(L,14);
Display(L);
Delete_data(L,699);
Display(L);
Reverse_List(L);
Display(L);
return 0;
}