接下来进行操作……
1,链表的定义……
对于双向链表的操作,就需要两个指针来进行操作,一个称为前驱指针,一个是后继指针来进行操作的
typedef int LTDateType;
typedef struct ListNode{
struct ListNode* prev;
struct ListNode* next;
LTDateType data;
}ListNode;
2,双向链表的接口定义
接下来的操作进行的比较多……
//1,链表的初始化
ListNode* ListCreat();
//2,链表的打印
void ListPrint(ListNode* phead);
//3,链表的尾插
void ListPushBack(ListNode* phead, LTDateType x);
//4,链表的头插
void ListPushFront(ListNode* phead, LTDateType x);
//5,链表是否为空
bool ListEmpty(ListNode* phead);
//6,链表的尾删
void ListPopBack(ListNode* phead);
//7,链表的头删
void ListPopFront(ListNode* phead);
//8,链表的查找
ListNode* ListFind(ListNode* phead, LTDateType x);
//9,链表在pos前面的插入
void ListInsert(ListNode* pos, LTDateType x);
//10,链表在pos位置处的删除
void ListErase(ListNode* pos);
//11,链表的销毁
void ListDestroy(ListNode* phead);
3,具体的操作实现
操作太多就不一一实现了……
1,进行链表的初始化操作
ListNode* BuyListNode(LTDateType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc");
return NULL;
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//链表的初始化
ListNode* ListCreat()
{
ListNode* phead = BuyListNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
2,进行链表的头插法操作
void ListPushFront(ListNode* phead, LTDateType x)
{
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
3,链表的销毁操作
void ListDestroy(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
4,链表的查找操作
//链表的查找
ListNode* ListFind(ListNode* phead,LTDateType x)
{
assert(phead);
assert(!ListEmpty(phead));
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
好了,分享就到这里,如有有错误 ,请及时指出,万分感谢……