Exercises on STL List - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Exercises on STL List

Description:

A data structure is an arrangement of data in a computer's memory. ... iter = intList.begin(); while (iter != intList.end()){ cout *iter endl; iter ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 10
Provided by: yxie
Category:
Tags: stl | exercises | iter | list

less

Transcript and Presenter's Notes

Title: Exercises on STL List


1
Exercises on STL List
  • Ying Xie

2
Data Structure A data structure is an
arrangement of data in a computers memory.
Each data structure is associated with some
typical operations - Insert a new data
item - Access a specified item -
Delete a specified item - Iterate through
all items
3
list
list
front
back
Model of a list
  • Create a list object.
  • - listltintgt intList
  • - listltintgt intList(5)
  • - listltintgt intList(arr, arrsize)
  • Insert, access and remove item at the both
    ends of the list.
  • - push_back(), pop_back(), back()
  • - push_front(), pop_front(), front()
  • Intermediate items can be accessed by using
    iterator
  • e.g. the following code access the third item
  • listltintgtiterator iter
    intList.begin()
  • while (iter ! intList.end())
  • cout ltlt iter ltlt endl iter
  • list is very efficient in insert and delete
    item at an arbitrary position
  • - insert, erase

4
Exercise 0
  • a) Use STL list constructor listltTgtlist() to
    create a list l and let l store the following
    integer values in the ascending order 1, 2, 3,
    4, 5.
  • b) Use STL list constructor listltTgtlist(int
    size) to create a list l and let l store the
    following integer values in the ascending order
    1, 2, 3, 4, 5.
  • c) Use STL list constructor listltTgtlist(T
    first, T last) to create a list l and let l
    store the following integer values in the
    ascending order 1, 2, 3, 4, 5.

5
Exercise 1
  • Please implement the following function
  • templatelttypename Tgt
  • int frequency(const listltTgt aList, T target)
  • This function returns the number of occurrences
    of a target value in a STL list. For example, if
    a list q contains the following integers 1, 2,
    3, 1, 3, 1, 9, then the function call
    frequency(q, 1) will return 3.

6
Exercise 2
  • Please implement the following function
  • templatelttypename Tgt
  • void insertOrder(listltTgt aList, const T item)
  • This function inserts an item into a list,
    meanwhile, keeping the elements in the list in
    the ascending order.

7
Exercise 3
  • Please implement the following function
  • templatelttypename Tgt
  • void removeDuplicates(listltTgt aList)
  • This function will modify a list, such that the
    list only contains unique values.

8
Exercise 4
  • Please implement the following function
  • templatelttypename Tgt
  • void removeDuplicates(listltTgt aList)
  • This function will modify a list, such that the
    list only contains unique values.

9
Quiz
  • Please implement the following function
  • templatelttypename Tgt
  • void replace(listltTgt aList, const T a, const
    T b)
  • This function will replace every appearance of a
    in the list with value b.
Write a Comment
User Comments (0)
About PowerShow.com