赞
踩
链表部分的操作无其他说明时,都用此篇文章定义的方法。
链表无特殊说明都为单链表,节点的定义如下
- typedef struct node
- {
- int data;
- node* pNext;
- }Node;
创建一个结点,也可以用来创建链表头
- Node* createNodeList()
- {
- Node *head = new Node;
- if(NULL == head)
- {
- return NULL;
- }
- else
- {
- head->data = -1;
- head->pNext = NULL;
- return head;
- }
- }
增加结点
- bool addNode(Node* head, Node* node)
- {
- if(NULL == head)
- {
- return false;
- }
- Node* p = head->pNext;
- Node* q = head;
- while(NULL != p)
- {
- q = p;
- p = p->pNext;
- }
- q->pNext = node;
- node->pNext = NULL;
- return true;
- }
插入结点
- bool insertNode(Node *before, Node *node)
- {
- if (NULL == before || NULL == node)
- {
- return false;
- }
- node->next = before->next;
- before->next = node;
- return true;
- }
删除结点(删除before结点后的结点,这里只是一个删除的例子)
- bool deleteNode(Node *before)
- {
- if (NULL == before || NULL == node)
- {
- return false;
- }
- Node *p = before->next;
- before->next = p->next;
- delete p;
- return true;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。