常规单链表操作有init, destroy, insert, remove, get, set, getNode, find, findNode, prev, prevNode, next, nextNode
#include <stdio.h>
//链表结点
typedef struct ListNode
{
int val;
struct ListNode *pNext;
}ListNode;
//链表头
typedef struct List
{
int length;
ListNode *pHead;
}List;
//初始化
void init(List &list)
{
list.length = 0;
list.pHead = NULL;
}
//销毁链表
void destroy(List &list)
{
ListNode *p = list.pHead, *pNext = NULL;
while(NULL != p)
{
pNext = p->pNext;
delete p;
p = pNext;
}
list.length = 0;
list.pHead = NULL;
}
//获得索引处的结点
ListNode *getNode(List &list, int index)
{
ListNode *p = NULL;//如果index超出范围,都为NULL
if(index >= 0 && index < list.length)
{
int i = 0;
p = list.pHead;
while(i != index)
{
p = p->pNext;
i++;
}
}
return p;
}
//插入链表结点
void insert(List &list, int index, int value)
{
if(index == -1 || index == list.length || index >= 0 && index <= list.length)
{
ListNode *p = new ListNode;
p->val = value;
if(index >= 0 && index < list.length)
{
if(index == 0)
{
//链表不为空,头插
p->pNext = list.pHead;
list.pHead = p;
}
else
{
//链表不为空,中插(不包括尾插)
ListNode *pPrev = getNode(list, index - 1);
p->pNext = pPrev->pNext;
pPrev->pNext = p;
}
}
else//index == -1 || index == list.length
{
if(list.length != 0)
{
//链表不为空,尾插
ListNode *pPrev = getNode(list, list.length - 1);
p->pNext = NULL;
pPrev->pNext = p;
}
else
{
//链表为空或index == -1
p->pNext = NULL;
list.pHead = p;
}
}
list.length++;
}
}
//删除链表结点
void remove(List &list, int index)
{
if(index >= 0 && index < list.length)
{
ListNode *p = NULL;
if(index != 0)
{
//非头删
ListNode *pPrev = getNode(list, index - 1);
p = pPrev->pNext;
pPrev->pNext = p->pNext;
}
else
{
//头删
p = list.pHead;
list.pHead = p->pNext;
}
delete p;
list.length--;
}
}
//getLength
int getLength(List &list)
{
return list.length;
}
//get
int get(List &list, int index)
{
ListNode *p = getNode(list, index);
int n = 0;
if(NULL != p)
{
n = p->val;
}
return n;
}
//set
void set(List &list, int index, int value)
{
ListNode *p = getNode(list, index);
if(NULL != p)
{
p->val = value;
}
}
//find
int find(List &list, int value)
{
int pos = 0;
ListNode *p = list.pHead;
while(NULL != p)
{
if(p->val == value)
{
break;
}
pos++;
p = p->pNext;
}
if(pos == list.length)
{
pos = -1;
}
return pos;
}
//findNode
ListNode *findNode(List &list, int value)
{
ListNode *p = list.pHead;
while(NULL != p)
{
if(p->val == value)
{
break;
}
p = p->pNext;
}
return p;
}
int prev(List &list, int index)
{
return get(list, index - 1);
}
int next(List &list, int index)
{
return get(list, index + 1);
}
//prevNode
ListNode *prevNode(List &list, ListNode *pCrt)
{
if(NULL == pCrt)
{
return NULL;
}
ListNode *pPrev = list.pHead, *p = NULL;
while(NULL != pCrt)
{
p = pPrev->pNext;
if(p == pCrt)
{
break;
}
pPrev = p;
}
return pPrev;
}
//nextNode
ListNode *nextNode(ListNode *p)
{
return p->pNext;
}
//print
void output(List &list)
{
ListNode *p = list.pHead;
while(NULL != p)
{
printf("%d", p->val);
if(p->pNext != NULL)
{
printf("->");
}
p = p->pNext;
}
printf("\n");
}
//测试用例
int main()
{
List list;
init(list);
insert(list, 0, 1);
insert(list, 0, 2);
insert(list, 0, 3);
insert(list, 1, -2);
insert(list, -1, -5);
insert(list, 5, -7);
output(list);
printf("%d, %d\n", find(list, 10), find(list, -5));
printf("%d, %d, %d, %d\n", prevNode(list, getNode(list, 2))->val, nextNode(getNode(list, 2))->val, prev(list, 2), next(list, 2));
remove(list, 0);
output(list);
remove(list, 1);
output(list);
// destroy(list);
// output(list);
return 0;
}
// 3->-2->2->1->-5->-7
// -1, 4
// -2, 1, -2, 1
// -2->2->1->-5->-7
// -2->1->-5->-7