#include <iostream>
#include <string>
using namespace std;
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <stdexcept>
template<typename T>
class Node
{
public:
T element; // Element contained in the node
Node<T> *previous; // Pointer to the previous node
Node<T> *next; // Pointer to the next node
Node() // No-arg constructor
{
previous = NULL;
next = NULL;
}
Node(T element) // Constructor
{
this->element = element;
previous = NULL;
next = NULL;
}
};
template < typename T >
class LinkedList
{
public:
LinkedList();
void addFirst(T element);
void addLast(T element);
T getFirst();
T getLast();
T removeFirst() throw(runtime_error);
T removeLast();
void add(T element);
void add(int index, T element);
void clear();
bool contains(T element);
T get(int index);
int indexOf(T element);
bool isEmpty();
int lastIndexOf(T element);
void remove(T element);
int getSize();
T removeAt(int index);
T set(int index, T element);
void addAll(LinkedList < T > otherList);
void removeAll(LinkedList < T > otherList);
void retainAll(LinkedList < T > otherList);
private:
Node < T > * head, * tail;
int size;
};
template < typename T >
LinkedList < T >::LinkedList()
{
head = tail = NULL;
size = 0;
}
template < typename T >
void LinkedList < T >::addFirst(T element)
{
Node < T > * newNode = new Node < T > (element);
newNode->next = head;
head = newNode;
size++;
if (tail == NULL)
tail = head;
}
template < typename T >
void LinkedList < T >::addLast(T element)
{
if (tail == NULL)
{
head = tail = new Node < T > (element);
}
else
{
tail->next = new Node < T > (element);
tail = tail->next;
}
size++;
}
template < typename T >
T LinkedList < T >::getFirst()
{
if (size == 0)
throw runtime_error("Index out of range");
else
return head->element;
}
template < typename T >
T LinkedList < T >::getLast()
{
if (size == 0)
throw runtime_error("Index out of range");
else
return tail->element;
}
template < typename T >
T LinkedList < T >::removeFirst() throw(runtime_error)
{
if (size == 0)
throw runtime_error("No elements in the list");
else
{
Node < T > * temp = head;
head = head->next;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template < typename T >
T LinkedList < T >::removeLast()
{
if (size == 0)
throw runtime_error("No elements in the list");
else if (size == 1)
{
Node < T > * temp = head;
head = tail = NULL;
size = 0;
T element = temp->element;
delete temp;
return element;
}
else
{
Node < T > * current = head;
for (int i = 0; i < size - 2; i++)
current = current->next;
Node < T > * temp = tail;
tail = current;
tail->next = NULL;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template < typename T >
void LinkedList < T >::add(T element)
{
addLast(element);
}
template < typename T >
void LinkedList < T >::add(int index, T element)
{
if (index == 0)
addFirst(element);
else if (index >= size)
addLast(element);
else
{
Node < T > * current = head;
for (int i = 1; i < index; i++)
current = current->next;
Node < T > * temp = current->next;
current->next = new Node < T > (element);
(current->next)->next = temp;
size++;
}
}
template < typename T >
void LinkedList < T >::clear()
{
while (head != NULL)
{
Node < T > * temp = head;
delete temp;
head = head->next;
}
tail = NULL;
}
template < typename T >
T LinkedList < T >::get(int index)
{
if (index < 0 || index > size - 1)
throw runtime_error("Index out of range");
Node < T > * current = head;
for (int i = 0; i < index; i++)
current = current->next;
return current->element;
}
template < typename T >
int LinkedList < T >::indexOf(T element)
{
// Implement it in this exercise
Node < T > * current = head;
for (int i = 0; i < size; i++)
{
if (current->element == element)
return i;
current = current->next;
}
return -1;
}
template < typename T >
bool LinkedList < T >::isEmpty()
{
return head == NULL;
}
template < typename T >
int LinkedList < T >::getSize()
{
return size;
}
template < typename T >
T LinkedList < T >::removeAt(int index)
{
if (index < 0 || index >= size)
throw runtime_error("Index out of range");
else if (index == 0)
return removeFirst();
else if (index == size - 1)
return removeLast();
else
{
Node < T > * previous = head;
for (int i = 1; i < index; i++)
{
previous = previous->next;
}
Node < T > * current = previous->next;
previous->next = current->next;
size--;
T element = current->element;
delete current;
return element;
}
}
template < typename T >
void LinkedList < T >::addAll(LinkedList < T > otherList)
{
for (int i = 0; i < otherList.getSize(); i++)
add(otherList.get(i));
}
template < typename T >
void LinkedList < T >::remove(T element)
{
while (true)
{
int index = indexOf(element);
if (indexOf(element) == -1)
return;
removeAt(index);
}
}
template < typename T >
bool LinkedList < T >::contains(T element)
{
return (indexOf(element) != -1);
}
template < typename T >
void LinkedList < T >::removeAll(LinkedList < T > otherList)
{
for (int i = 0; i < otherList.getSize(); i++)
remove(otherList.get(i));
}
template < typename T >
void LinkedList < T >::retainAll(LinkedList < T > otherList)
{
LinkedList < T > temp;
for (int i = 0; i < getSize(); i++)
if (!otherList.contains(get(i)))
temp.add(get(i));
for (int i = 0; i < temp.getSize(); i++)
remove(temp.get(i));
}
#endif
void printList(LinkedList < string > list)
{
for (int i = 0; i < list.getSize(); i++)
{
cout << list.get(i) << " ";
}
cout << endl;
}
int main()
{
// Create a list for strings
LinkedList < string > list1;
list1.add("Beijing");
list1.add("Tokyo");
list1.add("New York");
list1.add("London");
list1.add("Paris");
// Create a list for strings
LinkedList < string > list2;
list2.add("Beijing");
list2.add("Shanghai");
list2.add("Paris");
list2.add("Berlin");
list2.add("Rome");
// Remove elements from the list
list1.retainAll(list2);
printList(list1);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Introduction to Programming with C++(Pearson) 课后习题答案
共180个文件
cpp:173个
obj:2个
tds:2个
4星 · 超过85%的资源 需积分: 16 97 下载量 135 浏览量
2012-12-13
12:58:01
上传
评论 3
收藏 347KB ZIP 举报
温馨提示
Introduction to Programming with C++(Pearson) 课后习题答案
资源推荐
资源详情
资源评论
收起资源包目录
Introduction to Programming with C++(Pearson) 课后习题答案 (180个子文件)
Exercise20_6.cpp 6KB
Exercise20_4.cpp 6KB
Exercise20_2.cpp 6KB
Exercise21_8.cpp 5KB
Exercise21_10.cpp 5KB
Exercise21_2.cpp 4KB
Exercise10_2.cpp 4KB
Exercise17_20.cpp 4KB
Exercise14_2.cpp 4KB
Exercise21_4.cpp 4KB
Exercise21_6.cpp 4KB
Exercise17_18.cpp 3KB
Exercise6_4.cpp 3KB
Exercise5_24.cpp 3KB
Exercise19_4.cpp 3KB
Exercise18_6.cpp 2KB
Exercise4_28.cpp 2KB
Exercise7_20.cpp 2KB
Exercise6_20.cpp 2KB
Exercise19_2.cpp 2KB
Exercise8_10.cpp 2KB
Exercise8_8.cpp 2KB
Exercise11_8.cpp 2KB
Exercise12_12.cpp 2KB
Exercise5_14.cpp 2KB
Exercise3_4.cpp 2KB
Exercise8_4.cpp 2KB
Exercise4_34.cpp 2KB
Exercise23_10.cpp 2KB
Exercise8_6.cpp 2KB
Exercise8_18.cpp 2KB
Exercise4_22.cpp 2KB
Exercise8_12.cpp 2KB
Exercise3_18.cpp 2KB
Exercise9_2.cpp 1KB
Exercise15_4.cpp 1KB
Exercise12_8.cpp 1KB
Exercise18_2.cpp 1KB
Exercise10_20.cpp 1KB
Exercise23_12.cpp 1KB
Exercise13_10.cpp 1KB
Exercise12_10.cpp 1KB
Exercise7_6.cpp 1KB
Exercise9_6.cpp 1KB
Exercise7_18.cpp 1KB
Exercise13_4.cpp 1KB
Exercise19_6.cpp 1KB
Exercise23_4.cpp 1KB
Exercise4_2.cpp 1KB
Exercise10_18.cpp 1KB
Exercise3_8.cpp 1KB
Exercise4_20.cpp 1KB
Exercise10_24.cpp 1KB
Exercise7_22.cpp 1KB
Exercise7_16.cpp 1KB
Exercise10_16.cpp 1KB
Exercise8_16.cpp 1KB
Exercise16_6.cpp 1KB
Exercise10_6.cpp 1KB
Exercise13_12.cpp 1KB
Exercise7_12.cpp 1KB
Exercise6_16.cpp 1KB
Exercise4_6.cpp 1KB
Exercise23_8.cpp 1021B
Exercise2_12.cpp 1005B
Exercise8_14.cpp 998B
Exercise3_12.cpp 982B
Exercise15_2.cpp 980B
Exercise10_12.cpp 954B
Exercise18_4.cpp 936B
Exercise6_22.cpp 932B
Exercise12_6.cpp 928B
Exercise13_8.cpp 914B
Exercise17_16.cpp 900B
Exercise23_6.cpp 881B
Exercise23_2.cpp 878B
Exercise5_28.cpp 872B
Exercise6_8.cpp 872B
Exercise7_14.cpp 852B
Exercise3_14.cpp 849B
Exercise3_24.cpp 849B
Exercise5_26.cpp 831B
Exercise6_6.cpp 831B
Exercise16_2.cpp 827B
Exercise13_6.cpp 820B
Exercise6_2.cpp 816B
Exercise5_8.cpp 815B
Exercise4_40.cpp 801B
Exercise14_4.cpp 800B
Exercise7_26.cpp 797B
Exercise4_30.cpp 796B
Exercise10_4.cpp 791B
Exercise3_22.cpp 773B
Exercise5_22.cpp 757B
Exercise6_18.cpp 751B
Exercise4_42.cpp 750B
Exercise18_8.cpp 748B
Exercise20_8.cpp 745B
Exercise11_2.cpp 735B
Exercise22_12.cpp 727B
共 180 条
- 1
- 2
资源评论
- xqever2014-03-11习题的源码包 比较有用
- xiaodao19922015-04-16没搞懂。这个资源是哪本书的。。。
- qq_250498772015-06-03并不是我想要的那本书的答案
- bzx29932962014-10-14还不错,比较有用
heymylord
- 粉丝: 3
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功