链表的创建与翻转函数以及删除值为key的元素函数
来源:互联网 发布:淘宝客营销案例 编辑:程序博客网 时间:2024/06/05 18:43
#include<iostream>
using namespace std;
using namespace std;
//声明结构体
struct ListNode
{
int val;
ListNode * next;
ListNode(int x) : val(x), next(NULL){}
};
struct ListNode
{
int val;
ListNode * next;
ListNode(int x) : val(x), next(NULL){}
};
//链表创建函数
ListNode * CreatLinkedList(int a[], int n)
{
if (n == 0) return NULL;
ListNode *head = new ListNode(a[0]);
ListNode *cur=head;
ListNode * CreatLinkedList(int a[], int n)
{
if (n == 0) return NULL;
ListNode *head = new ListNode(a[0]);
ListNode *cur=head;
for (int i = 1; i < n; i++)
{
cur->next = new ListNode(a[i]);
cur = cur->next;
}
return head;
}
{
cur->next = new ListNode(a[i]);
cur = cur->next;
}
return head;
}
//链表打印函数
void printLinkedList(ListNode *head)
{
while (head != NULL)
{
cout << head->val << " -> ";
head = head->next;
}
cout << "NULL" << endl;
}
void printLinkedList(ListNode *head)
{
while (head != NULL)
{
cout << head->val << " -> ";
head = head->next;
}
cout << "NULL" << endl;
}
//链表翻转函数
ListNode * reverseLinkedList(ListNode * head)
{
ListNode *pre = NULL;
ListNode *cur = head;
ListNode * reverseLinkedList(ListNode * head)
{
ListNode *pre = NULL;
ListNode *cur = head;
while (cur != NULL)
{
ListNode *next = cur->next;//记录下一个指针的位置
cur->next = pre; //当前指针指向前一个指针
pre = cur;
cur = next;
}
return pre;
}
{
ListNode *next = cur->next;//记录下一个指针的位置
cur->next = pre; //当前指针指向前一个指针
pre = cur;
cur = next;
}
return pre;
}
//删除链表函数
void deleteLinkedList(ListNode *head)
{
ListNode* cur = head;
while (cur != NULL)
{
ListNode* delcur = cur;
cur = cur->next;
delete delcur;
}
}
void deleteLinkedList(ListNode *head)
{
ListNode* cur = head;
while (cur != NULL)
{
ListNode* delcur = cur;
cur = cur->next;
delete delcur;
}
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
ListNode *head = CreatLinkedList(a, n);
printLinkedList(head);
ListNode *head2=reverseLinkedList(head);
printLinkedList(head2);
printLinkedList(head2);
deleteLinkedList(head);
return 0;
}
}
运行如下:
//在链表中删除key值的元素
ListNode * removeElements(ListNode * head,int key)
{
ListNode* dummyHead = new ListNode(0);//设立一个虚拟头结点,更方便处理头结点为key的值
dummyHead->next = head;
{
ListNode* dummyHead = new ListNode(0);//设立一个虚拟头结点,更方便处理头结点为key的值
dummyHead->next = head;
ListNode* cur = dummyHead;
while (cur->next != NULL)
{
if (cur->next->val == key)
{
ListNode* delcur = cur->next;
cur->next = delcur->next;
delete delcur;
}
else
cur = cur->next;
}
while (cur->next != NULL)
{
if (cur->next->val == key)
{
ListNode* delcur = cur->next;
cur->next = delcur->next;
delete delcur;
}
else
cur = cur->next;
}
ListNode *retNode = dummyHead->next;
delete dummyHead;
delete dummyHead;
return retNode;
}
}
删除值为5的元素,运行如下:
//删除链表中倒数第n个元素
ListNode * removeNthFromEnd(ListNode * head, int n)
{
assert( n >0);
ListNode * removeNthFromEnd(ListNode * head, int n)
{
assert( n >0);
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
dummyhead->next = head;
ListNode *p = dummyhead;
ListNode *q = dummyhead;
ListNode *q = dummyhead;
for (int i = 0; i <n+1; i++)
{
assert(q);
q = q->next;
}
{
assert(q);
q = q->next;
}
while (q) //while (q!=NULL)
{
p = p->next;
q = q->next;
}
{
p = p->next;
q = q->next;
}
ListNode *delnode = p->next;
p->next = delnode->next;
delete delnode;
p->next = delnode->next;
delete delnode;
ListNode * retnode = dummyhead->next;
delete dummyhead;
delete dummyhead;
return retnode;
}
}
阅读全文
0 0
- 链表的创建与翻转函数以及删除值为key的元素函数
- 链表的创建、元素插入、元素删除、以及两个有序链表的归并
- 链表的创建与删除链表中的元素
- 双向链表的创建查找与删除元素
- 顺序表来实现函数:删除最小值并返回删除元素,空出位置用尾部元素来填补,删除与给定值相同的所有的元素,删除给定值s,t(s<t)之间的元素
- 无情链表的创建,,翻转翻转
- 单链线性表的创建,元素的插入删除以及合并
- 创建一个结点个数为n的链表,删除指定的元素
- 从链表删除所有值为val的元素
- 编写一个函数, 给定一个链表的头指针,要求只遍历一次, 将单链表中的元素顺序翻转过来
- 链表所有为key的节点全部删除
- JS删除数组元素的函数介绍
- C++删除元素的函数总结
- 4.20 mkdir和rmdir函数-目录的创建与删除
- CRT 中创建、删除线程函数与WinAPI的区别
- 链表的相邻元素的翻转
- 链表的创建、插入以及删除
- 链表的创建,遍历以及删除
- Floyd 判圈算法
- 对话框(2)-标准对话窗
- Less 基础入门
- AdaBoost算法
- 刷LeetCode(11)——Container With Most Water
- 链表的创建与翻转函数以及删除值为key的元素函数
- 投资养老社保不如买房?房产税还未出台就已处境尴尬
- Java集合框架05 fail-fast机制
- host头攻击解决方法
- byte数组与文件的转换
- Git语句
- 堆排序算法Java面向对象实现
- unity中全息投影带贴图shader
- java常用类--日期和时间