在 STL 中实现列表的 C++ 程序
列表是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除就会很快。
功能及说明:
From main(), we have called following functions: fl.resize() = Returns the resize of list. fl.push_front() = It is used to push elements into a list from the front. fl.remove() = Deletes elements from list. fl.unique() = Deletes duplicate elements from list. fl.reverse() = Reverses the list. fl.front() = Returns the front element of list.
示例代码
#include#include #include
#include using namespace std; int main() { list l; list ::iterator it; int c, i; while (1) { cout<<"1.Insert Element at the Front"< >c; switch(c) { case 1: cout<<"输入要在前面插入的值: "; cin>>i; l.push_front(i); break; case 2: cout<<"输入要在最后插入的值: "; cin>>i; l.push_back(i); break; case 3: i= l.front(); l.pop_front(); cout<<"Element "<>i; if (i <= l.size()) l.resize(i); else l.resize(i, 0); break; case 9: cout<<"输入要删除的元素: "; cin>>i; l.remove(i); break; case 10: l.unique(); cout<<"Duplicate Items Deleted"< 输出结果 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 1 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 2 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 3 Element 2 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 4 Element 1 deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 2 输入要在最后插入的值: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 6 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 7 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 12 列表元素: 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 1 输入要在前面插入的值: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 12 列表元素: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 10 Duplicate Items Deleted 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 5 列表的前端元素: 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 12 列表元素: 4 5 7 6 5 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 11 List reversed 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 12 列表元素: 5 6 7 5 4 1.Insert Element at the Front 2.Insert Element at the End 3.Delete Element at the Front 4.Delete Element at the End 5.Front Element of List 6.Last Element of the List 7.Size of the List 8.Resize List 9.Remove Elements with Specific Values 10.Remove Duplicate Values 11.Reverse the order of elements 12.Display the List 13.Exit 输入您的选择: 13
热门推荐
10 启程出发路途祝福语简短
11 写给男生简短的祝福语
12 夸赞老师祝福语搞笑简短
13 老板端午简短祝福语大全
14 职场离职文案祝福语简短
15 简短的祝福语12字
16 女生 节日祝福语简短大气
17 送女士祝福语长辈简短
18 文学短句祝福语大全简短