two problems about linked list

ledfok 2002-07-19 01:14:31
who can solve these two problems?try these if you have the knowledge of data structure for c++.solve it in c++,please.

Problem 1 .
Implement a sorting algorithm using a singly linked list. The
algorithm (known as the Insertion sort algorithm) works as follows:
1. It starts with an empty linked list.
2. When a first node is inserted to the list, the node is inserted at the head of the list.
3. To insert a new node, the algorithm traverses the linked list until it finds a specific
position to insert the new node by comparing the new node’s data value to each
existing node’s data value.
4. Traverses all the elements of the linked list to print out the sorted values. [optional]
5. Repeats steps 3 and 4 for each and every new node.
Implement the insertion sort algorithm in C++ to ascendingly sort integers. Your program
should receive an arbitrary number of input integers one by one until you interrupt or stop
your program (by pressing Ctrl-C, for example). For each input integer, the program prints
out the sorted list after inserting the value. The screen shot of your program may look like the
following:
$ a.out
== Insertion sort by [your name] ==
[Empty List]
Input an integer: 5
Sorted List : 5
Input an integer: 7
Sorted List: 5 7
Input an integer: 2
Sorted List: 2 5 7
Input an integer: 6
Sorted List: 2 5 6 7
Input an integer: 3
Sorted List: 2 3 5 6 7
Also answer the following questions (as the comments at the end of your C++ program) :
� Explain the input sequences that will result in the best and the worst algorithmic
performances, in terms of the number of comparisons required.
� If there are N input values to be sorted, what is the best case complexity and what is the
worse case complexity, in terms of N ?


Problem 2
Implement the doubly circular linked list for integers in C++. Provide the following DCLL operations as the method members of the DCLL class :
1. Add a node at the head of the list
2. Add a node at the tail of the list (i.e. at head->previous)
3. Add a node after a specific n-th node (e.g. after the 4-th node). If such node does not
exist, simply deny the operation
4. Delete a node from the head of the list
5. Delete a node from the tail of the list
6. Delete a specific n-th node (e.g. delete the 5-th node). If such node does not exist, simply
deny the operation.
7. Delete all the nodes that have a specific value (i.e. yes, there can be duplicate nodes)
8. Determine if the list contain a particular value and how many duplicate nodes there are
9. Traverse the list in a forward direction (i.e. from head to tail)
10. Traverse the list in a backward direction (i.e. from tail to head)
Create a user interface in the main() function that is similar to the singly linked list (sll.cc)
example given on the class web page. The user interface should provide the above DCLL
operations appearing in such order. After applying each operation, your DCLL must still
remain consistent, i.e. all the pointers point to where they are supposed to.
...全文
64 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧