STL中的转发列表实现单链列表。列表与forward_list不同,该列表跟踪下一个和上一个元素。
前向列表仅跟踪下一个元素的位置,因此增加了存储每个元素所需的存储空间。forward_list的缺点是单个元素无法直接访问,并且不能向后迭代。
From main(), we have called following functions: fl.resize() = Returns the resize of forward_list. fl.push_front() = It is used to push elements into a foward_list from the front. fl.remove() = Deletes elements from forward_list. fl.unique() = Deletes duplicate elements from forward_list. fl.reverse() = Reverses the forward_list. fl.front() = Returns the front elements of forward_list
#include<iostream> #include <forward_list> #include <string> #include <cstdlib> using namespace std; int main() { forward_list<int> fl; forward_list<int>::iterator it; int c, n; while (1) { cout<<"1.Insert Element at the Front"<<endl; cout<<"2.Delete Element at the Front"<<endl; cout<<"3.Front Element of Forward List"<<endl; cout<<"4.Resize Forward List"<<endl; cout<<"5.Remove Elements with Specific Values"<<endl; cout<<"6.Remove Duplicate Values"<<endl; cout<<"7.Reverse the order of elements"<<endl; cout<<"8.Display Forward List"<<endl; cout<<"9.Exit"<<endl; cout<<"Enter your Choice: "; cin>>c; switch(c) { case 1: cout<<"Enter value to be inserted at the front: "; cin>>n; fl.push_front(n); break; case 2: n = fl.front(); fl.pop_front(); cout<<"Element "<<n<<" deleted"<<endl; break; case 3: cout<<"Front Element of the Forward List: "; cout<<fl.front()<<endl; break; case 4: cout<<"Enter new size of Forward List: "; cin>>n; if (n <= fl.max_size()) fl.resize(n); else fl.resize(n, 0); break; case 5: cout<<"Enter element to be deleted: "; cin>>n; fl.remove(n); break; case 6: fl.unique(); cout<<"Duplicate Items Deleted"<<endl; break; case 7: fl.reverse(); cout<<"Forward List reversed"<<endl; break; case 8: cout<<"Elements of Forward List: "; for (it = fl.begin(); it != fl.end(); it++) cout<<*it<<" "; cout<<endl; break; case 9: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }
输出结果
1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 2 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 3 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 3 Front Element of the Forward List: 3 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 4 Enter new size of Forward List: 6 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 5 Enter element to be deleted: 1 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 3 2 0 0 0 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 4 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 5 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 1 Enter value to be inserted at the front: 8 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 8 5 4 3 2 0 0 0 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 47 Wrong Choice 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 7 Forward List reversed 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 0 0 0 2 3 4 5 8 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 4 Enter new size of Forward List: 4 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 8 Elements of Forward List: 0 0 0 2 1.Insert Element at the Front 2.Delete Element at the Front 3.Front Element of Forward List 4.Resize Forward List 5.Remove Elements with Specific Values 6.Remove Duplicate Values 7.Reverse the order of elements 8.Display Forward List 9.Exit Enter your Choice: 9 Exit code: 1
本文向大家介绍在STL中实现Vector的C ++程序,包括了在STL中实现Vector的C ++程序的使用技巧和注意事项,需要的朋友参考一下 向量具有在插入或删除元素时自动像动态数组一样自动调整大小的能力,容器可以自动处理其存储。矢量元素放置在连续的存储中,以便可以使用迭代器对其进行访问和遍历。可以在向量的开头,中间或结尾插入或删除数据。 功能和说明: 范例程式码 输出结果
本文向大家介绍在STL中实现集的C ++程序,包括了在STL中实现集的C ++程序的使用技巧和注意事项,需要的朋友参考一下 Set是抽象数据类型,其中每个元素都必须是唯一的,因为元素的值可以标识它。一旦将元素的值添加到集合中,就无法对其进行修改,但是可以删除并添加该元素的修改后的值。 功能和说明: 范例程式码 输出结果
本文向大家介绍在STL中实现Priority_queue的C ++程序,包括了在STL中实现Priority_queue的C ++程序的使用技巧和注意事项,需要的朋友参考一下 优先级队列是一种容器适配器,其中队列的第一个元素是队列中所有元素中最大的。元素在优先级队列中的顺序也不减。在优先级队列中,高优先级的元素在低优先级的元素之前得到服务。 功能和说明: 范例程式码 输出结果
本文向大家介绍C ++程序在STL中实现Prev_Permutataion,包括了C ++程序在STL中实现Prev_Permutataion的使用技巧和注意事项,需要的朋友参考一下 STL中的Prev_permutation用于将范围[first,last]中的元素重新排列到先前的字典上较小的排列。排列是N的每一个!元素可以采取的可能安排。这是一个在STL中实现Prev_permutation的
本文向大家介绍C ++程序在STL中实现Next_Permutation,包括了C ++程序在STL中实现Next_Permutation的使用技巧和注意事项,需要的朋友参考一下 STL中的next_permutation用于将[first,last]范围内的元素重新排列到下一个字典上更大的排列。排列是N的每一个!元素可以采取的可能安排。这是C ++程序,用于在STL中实现Next_permuta
本文向大家介绍C ++程序在STL中实现堆栈,包括了C ++程序在STL中实现堆栈的使用技巧和注意事项,需要的朋友参考一下 堆栈是遵循特定操作顺序的线性数据结构。订单可以是FILO(先进先出)或LIFO(先进先出) 算法 范例程式码 输出结果