顺序表的问题及思考
问题:
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
逻辑结构:
实际中要实现的链表的结构非常多样,以下情况组合起来就有8种链表结构:
虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:
typedef int STLDataType;
typedef struct STListNode
{
STLDataType data;
struct STListNode* next;
}STLNode;
//向内存申请一个新节点
STLNode* BuySTLNode(STLDataType x);
//打印链表中的元素
void SListPrint(STLNode* phead);
//尾插
void SlistPushBack(STLNode** pphead, STLDataType x);
//头插
void SlistPushFront(STLNode** pphead, STLDataType x);
//尾删
void SlistPopBack(STLNode** pphead);
//头删
void SlistPopFront(STLNode** pphead);
//查找
STLNode* SlistFind(STLNode * phead,STLDataType x);
//在pos位置之前插入x
void SlistInsert(STLNode** pphead, STLNode* pos, STLDataType x);
//删除操作
void SlistErase(STLNode** pphead, STLNode* pos);
//向内存申请一个新节点
STLNode* BuySTLNode(STLDataType x)
{
STLNode* newnode = (STLNode*)malloc(sizeof(STLNode));
newnode->next = NULL;
newnode->data = x;
return newnode;
}
代码实现:
//打印链表中的元素
void SListPrint(STLNode* phead)
{
STLNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
//尾插
void SlistPushBack(STLNode** pphead, STLDataType x)
{
STLNode* newnode =BuySTLNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
STLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
//头插
void SlistPushFront(STLNode** pphead, STLDataType x)
{
STLNode * newnode = BuySTLNode(x);
newnode->next= *pphead;
*pphead = newnode;
}
//头删
void SlistPopFront(STLNode** pphead)
{
STLNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
//尾删
void SlistPopBack(STLNode** pphead)
{
//1.空节点
//2.1个节点
//3.多个节点
if ((*pphead) == NULL)
{
return;
}
else if ((*pphead)->next== NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
STLNode* prev = NULL;
STLNode* tail = *pphead;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
代码实现:
//查找
STLNode* SlistFind(STLNode* phead, STLDataType x)
{
STLNode* cur = phead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos前面插入x
void SlistInsert(STLNode** pphead,STLNode* pos, STLDataType x)
{
if (*pphead == pos)
{
SlistPushFront(pphead, x);
}
else
{
STLNode* newnode = BuySTLNode(x);
STLNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
newnode = cur->next;
newnode->next = pos;
}
}
代码实现:
//删除pos 位置的值
void SlistErase(STLNode** pphead, STLNode* pos)
{
if (pos == *pphead)
{
SListPopFront(pphead);
}
else
{
STLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
跟单链表类似
typedef int LTDataType;
//有头双向循环链表
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
LTDataType data;
}ListNode;
//初始化,申请一个头结点
ListNode* ListInit();
//双链表尾插
void ListPushBack(ListNode* phead,LTDataType x);
//双链表头插
void ListPushFront(ListNode* phead, LTDataType x);
//打印
void ListPrint(ListNode* phead);
//双链表尾删
void ListPopBack(ListNode* phead);
//双链表头删
void ListPopFront(ListNode* phead);
//查找
ListNode* ListFind(ListNode* phead,LTDataType x);
//在pos前插入一个结点
void ListInsert(ListNode* pos, LTDataType x);
//删除pos位置上的结点
void ListErase(ListNode* pos);
//销毁链表
void ListDestroy(ListNode* phead);
ListNode* BuyListNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
ListNode* ListInit()
{
ListNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
//ListInsert(phead, x);
}
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* first = phead->next;
ListNode* newnode = BuyListNode(x);
// phead newnode first
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);
first = NULL;
//ListErase(phead->next);
}
void ListPopBack(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
//ListErase(phead->prev);
}
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
// pos位置之前插入x
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = BuyListNode(x);
// prev newnode pos
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
// 删除pos位置的值
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
代码实现:
void ListDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
测试案例1:
void ListTest1()
{
ListNode* plist = ListInit();
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPushBack(plist, 5);
ListPushBack(plist, 6);
ListPushBack(plist, 7);
ListPrint(plist);
ListPushFront(plist, 2);
ListPushFront(plist, 1);
ListPrint(plist);
ListPopBack(plist);
ListPopBack(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPrint(plist);
}
int main()
{
ListTest1();
return 0;
}
void ListTest2()
{
ListNode* plist = ListInit();
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPushBack(plist, 5);
ListPushBack(plist, 6);
ListPushBack(plist, 7);
ListPrint(plist);
ListPushFront(plist, 2);
ListPushFront(plist, 1);
ListPrint(plist);
ListErase(ListFind(plist, 3));
ListInsert(ListFind(plist, 4), 30);
ListPrint(plist);
}
int main()
{
ListTest2();
return 0;
}