Data_Structure_and_Algorithm 01_Linked_List
Quiz(Python Version)
"""The LinkedList code from before is provided below.
Add three functions to the LinkedList.
"get_position" returns the element at a certain position.
The "insert" function will add an element to a particular
spot in the list.
"delete" will delete the first element with that
particular value."""
class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
# Start setting up a LinkedList
ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
# Test get_position
# Should print 3
print ll.head.next.next.value
# Should also print 3
print ll.get_position(3).value
# Test insert
ll.insert(e4,3)
# Should print 4 now
print ll.get_position(3).value
# Test delete
ll.delete(1)
# Should print 2 now
print ll.get_position(1).value
# Should print 4 now
print ll.get_position(2).value
# Should print 3 now
print ll.get_position(3).value
1. get_position(self, position):
My Answer_init
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def get_position(self, position):
"""Get an element from a particular position.
Assume the first position is "1".
Return "None" if position is not in the list."""
cur_element = self.head
cur_position = 1
while cur_position < position and cur_element:
cur_element = cur_element.next
cur_position += 1
return cur_element
# ignored... answer above
- 没有考虑错误输入position<1的情况:position<1时,函数仍会返回
self.head
Standard Answer
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def get_position(self, position):
counter = 1
current = self.head
if position < 1:
return None
while current and counter <= position:
if counter == position:
return current
current = current.next
counter += 1
return None
# ignored... answer above
我认为无需分别考虑以下两种情况:
- 找到
position
并返回具体element
- 未找到
position
并返回None
可采用 My Answer_init中的循环方式,省去counter == position
的判断,当counter == position
时,循环退出,
- 若
position
不在链表中,则current = None
- 若
position
在链表中,则current
为指定position
的element
因此,直接返回current
即可。
My Answer_final
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def get_position(self, position):
"""Get an element from a particular position.
Assume the first position is "1".
Return "None" if position is not in the list."""
if position < 1:
return None
cur_element = self.head
cur_position = 1
while cur_position < position and cur_element:
cur_element = cur_element.next
cur_position += 1
return cur_element
# ignored... answer above
2. insert(self, new_element, position)
My Answer_init
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def insert(self, new_element, position):
"""Insert a new node at the given position.
Assume the first position is "1".
Inserting at position 3 means between
the 2nd and 3rd elements."""
if position == 1:
next_element = self.head
self.head = new_element
self.head.next = next_element
else:
cur_element = self.head
cur_position = 1
while cur_position < position-1 and cur_element:
cur_element = cur_element.next
cur_position += 1
if cur_element:
next_element = cur_element.next
cur_element.next = new_element
new_element.next = next_element
else:
print "Error: No given position in the list!!"
# ignored... answer above
- 没有考虑错误输入position<1的情况:position<1时,函数仍会将
new_element
插入到self.head.next
- 忽略了
new_element
同样属于Class element
,同样具有next
成员变量,因此插入new_element
时无需用next_element
作为中间变量!
Standard Answer
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def insert(self, new_element, position):
counter = 1
current = self.head
if position > 1:
while current and counter < position:
if counter == position - 1:
new_element.next = current.next
current.next = new_element
current = current.next
counter += 1
elif position == 1:
new_element.next = self.head
self.head = new_element
# ignored... answer above
Standard Answer 中,在while
循环中用if counter == position -1:
语句判断是否找到插入位置position
;
而My Answer_init中,在while
循环中用if cur_element:
语句判断当前位置的cur_element
是否不为None
.
这两种方式都可以避免position
不在链表中时仍将new_element
插入。
My Answer_final
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def insert(self, new_element, position):
"""Insert a new node at the given position.
Assume the first position is "1".
Inserting at position 3 means between
the 2nd and 3rd elements."""
if position < 1:
print "Error: Given position should be greater than 0!!"
elif position == 1:
new_element.next = self.head
self.head = new_element
else:
cur_element = self.head
cur_position = 1
while cur_position < position-1 and cur_element:
cur_element = cur_element.next
cur_position += 1
if cur_element:
new_element.next = cur_element.next
cur_element.next = new_element
else:
print "Error: No given position in the list!!"
# ignored... answer above
3. delete(self, value)
My Answer_init
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def delete(self, value):
"""Delete the first node with a given value."""
if self.head.value == value:
temp_element = self.head
self.head = self.head.next
temp_element.next = None
else:
pre_current = self.head
current = pre_current.next
while current and current.value != value:
pre_current = current
current = current.next
if current:
pre_current.next = current.next
current.next = None
else:
print "Error: No given value found in the list!!"
- 考虑将当前element:
current/self.head
的next
设为None
完全多余。也无需单独考虑self.head.value == value
的情况。
Standard Answer is much better.
Standard Answer
class LinkedList(object):
def __init__(self, head=None):
self.head = head
# ignored... answer below
def delete(self, value):
current = self.head
previous = None
while current.value != value and current.next:
previous = current
current = current.next
if current.value == value:
if previous:
previous.next = current.next
else:
self.head = current.next
C++实现
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
/*
The LinkedList code from before is provided below.
Add three functions to the LinkedList.
"get_position" returns the element at a certain position.
The "insert" function will add an element to a particular
spot in the list.
"delete" will delete the first element with that
particular value.
*/
class Element {
public:
int value;
Element* next;
Element(int value) {
this->value = value;
this->next = nullptr;
}
};
class LinkedList {
public:
Element* head;
LinkedList(Element* head) {
this->head = head;
}
void append(Element* new_element) {
Element* current = this->head;
if (current == nullptr) {
this->head = new_element;
}
else {
while (current->next)
{
current = current->next;
}
current->next = new_element;
}
}
/*Get an element from a particular position.
Assume the first position is "1".
Return "nullptr" if position is not in the list.
*/
Element* get_position(int position) {
if (position < 1) {
return nullptr;
}
else {
int cur_position = 1;
Element* cur_element = this->head;
while (cur_position < position && cur_element) {
cur_position++;
cur_element = cur_element->next;
}
return cur_element;
}
}
/*Insert a new node at the given position.
Assume the first position is "1".
Inserting at position 3 means between
the 2nd and 3rd elements.
*/
void insert(Element* new_element, int position) {
if (position < 1) {
cout << "Error: position not found in given list!!" << endl;
}
else if (position == 1) {
new_element->next = this->head;
this->head = new_element;
}
else {
int cur_position = 1;
Element* cur_element = this->head;
while (cur_position < position-1 && cur_element) {
cur_position++;
cur_element = cur_element->next;
}
if (cur_element) {
new_element->next = cur_element->next;
cur_element->next = new_element;
}
else {
cout << "Error: position not found in given list!!" << endl;
}
}
}
/*Delete the first node with a given value.
*/
void deleteValue(int value) {
Element* cur_element = this->head;
Element* pre_element = nullptr;
while (cur_element->value != value && cur_element->next) {
pre_element = cur_element;
cur_element = cur_element->next;
}
if(cur_element->value == value)
if (pre_element) {
pre_element->next = cur_element->next;
delete cur_element;
}
else
{
pre_element = this->head;
this->head = this->head->next;
delete pre_element;
}
else {
cout << "Error: No given value found in the list!!" << endl;
}
}
};
int main()
{
//Test Cases
//Set up some Elements
Element* e1 = new Element(1);
Element* e2 = new Element(2);
Element* e3 = new Element(3);
Element* e4 = new Element(4);
//Start setting up a LinkedList
LinkedList* l1 = new LinkedList(e1);
l1->append(e2);
l1->append(e3);
//Test get_position
//Should print 3
cout << l1->head->next->next->value << endl;
//Should print 3
cout << l1->get_position(3)->value << endl;
//Should print error message
Element* test_element = l1->get_position(4);
if (test_element) {
cout << test_element->value << endl;
}
else
{
cout << "Error: position not found in given list!!" << endl;
}
//Test insert
l1->insert(e4, 3);
//Should print 4 now
cout << l1->get_position(3)->value << endl;
//Insert should fail, 2 error msgs should be printed
Element* e5 = new Element(5);
l1->insert(e5, 6);
test_element = l1->get_position(5);
if (test_element) {
cout << test_element->value << endl;
}
else
{
cout << "Error: position not found in given list!!" << endl;
}
//Test deleteValue
l1->deleteValue(1);
//Should print 2 now
cout << l1->get_position(1)->value << endl;
//Should print 3 now
l1->deleteValue(4);
cout << l1->get_position(2)->value << endl;
//Should print error message
l1->deleteValue(5);
//Delete newed Elements and LinkedList to prevent memory leak
delete e2;
delete e3;
delete e5;
delete l1;
system("pause");
return 0;
}