以下是头文件:
#pragma once
#ifndef __LIST_H__
#define __LIST_H__
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
#endif
以下是代码实现:
#include "list.h"
typedef int ElemType;
typedef struct node{ //节点
ElemType elem;
struct node* next;
}node,*pNode;
typedef struct { //链表
pNode head, tail; //头、尾、指向下一个的节点指针
int len; //长度
}LinkList,*PlinkList;
enum { ERROR, OK }; //0 1
PlinkList initList(); //申请头节点
int clearList(LinkList* L); //清空
int destroyLIst(LinkList* L); //销毁
int insFirst(LinkList* L, ElemType e); //头插
int insEnd(LinkList* L, ElemType e); //尾插
int insDelete(LinkList* L); //头删
int locateElem(const LinkList* L, ElemType e); //查询e出现的第一个位置
int deleteELem(LinkList* L, ElemType e); //删除第一个出现的e
int isEmpty(const LinkList* L); //判空
void printList(const LinkList* L); //打印
int lenList(const LinkList* L); //长度
void test4();
//int main()
//{
// printf("------------------单链表测试------------------\n");
// test4();
//
// int i = 43;
// printf("%d\n", printf("%d", printf("%d", i)));
//
// system("pause");
// return 0;
//}
void test4()
{
PlinkList list = initList();
printf("%d\n", list->head->elem);
printf("%p\n", list->head->next);
printf("%p\n", list->head);
printf("%p\n", list->tail);
printf("%p\n", list->tail->next);
printf("%p\n", list);
printf("%d\n", isEmpty(list));
insFirst(list, 1); insFirst(list, 2); insFirst(list, 3);
insEnd(list, 1); insEnd(list, 222); insEnd(list, 222);
printList(list);
deleteELem(list, 222);
printList(list);
printf("%d\n", locateElem(list, 222));
insDelete(list);
printList(list);
printf("len:%d\n", lenList(list));
clearList(list);
printList(list);
printf("%d\n", destroyLIst(list));
}
PlinkList initList() //申请头节点
{
LinkList* L;
L = (PlinkList)malloc(sizeof(LinkList));
L->head = (node*)malloc(sizeof(node));
L->tail = (node*)malloc(sizeof(node));
L->head->next = NULL;
L->tail->next = NULL;
L->head->elem = 0;
L->tail->elem = 0;
L->len = 0;
return L;
}
int clearList(LinkList* L)
{
assert(L);
pNode temp = NULL;
pNode p = L->head->next;
while (p != NULL)
{
temp = p->next;
free(p);
p = temp;
}
L->head->next = NULL;
L->tail->next = NULL;
L->len = 0;
return OK;
}
int destroyLIst(LinkList* L)
{
assert(L);
//删除链表L中除了头节点之外的所有节点
clearList(L);
free(L);
L = NULL;
return OK;
}
int insFirst(LinkList* L, ElemType e) //头插
{
assert(L);
pNode temp = (pNode)malloc(sizeof(node));
temp->elem = e;
if (L->head->next == NULL)
{
L->tail = temp;
}
temp->next = L->head->next;
L->head->next = temp;
L->len++;
return OK;
}
int insEnd(LinkList* L, ElemType e) //尾插
{
assert(L);
pNode temp = (pNode)malloc(sizeof(node));
temp->elem = e;
if (L->head->next == NULL)
{
L->head = temp;
}
L->tail->next = temp;
temp->next = NULL;
L->tail = temp;
L->len++;
return OK;
}
int insDelete(LinkList* L) //头删
{
assert(L);
pNode temp = L->head->next->next;
free(L->head->next);
L->head->next = temp;
L->len--;
return OK;
}
int locateElem(const LinkList* L, ElemType e)
{
assert(L);
int i = 0;
pNode p = L->head->next;
while (p != NULL)
{
++i;
if (p->elem == e)
{
return i;
}
p = p->next;
}
return ERROR;
}
int deleteELem(LinkList* L, ElemType e) //删除第一个出现的e
{
assert(L);
pNode p = L->head->next;
pNode temp = L->head; //记录p的前驱
while (p != NULL)
{
if (p->elem == e)
{
temp->next = p->next;
free(p);
L->len--;
return OK;
}
p = p->next;
temp = temp->next;
}
return ERROR;
}
int isEmpty(const LinkList* L)
{
assert(L);
if (L->head->next == L->tail->next)
{
return 1;
}
return 0;
}
void printList(const LinkList* L)
{
assert(L);
pNode p = L->head->next;
while (p != NULL)
{
printf("%d ", p->elem);
p = p->next;
}
printf("\n");
}
int lenList(const LinkList* L)
{
assert(L);
return L->len;
}