您的位置:首页 > 其它

list 基本操作 1 -- 创建,插入,删除,计算长度

2016-02-20 16:17 295 查看
list 的基本操作,包括list 创建,插入,删除,长度计算 ( 迭代和递归算法)

//linked list study

#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

struct Node
{
int val;
struct Node* next;
Node()
{
next = NULL;
val = 0;
};
Node(int v)
{
next = NULL;
val = v;
}
};

Node* create_link_list(vector<int> A)
{
// create link list from vector
Node* head = new Node();
Node* p = head;

for (int i = 0; i < A.size(); ++ i)
{
Node* tmp = new Node(A[i]);
p->next = tmp;
p = p->next;
}
return head;
};

void print_list(Node* head, string description="")
{
cout << description << endl;
while(head && head->next && head->next->next)
{
cout << head->next->val << "->";
head = head->next;
}
if (head->next)
cout << head->next->val<<endl;
return;
};

void insert_front(Node* head, int val)
{
// insert in front of list
Node* tmp = new Node(val);
if (!head)
head = tmp;
else
{
tmp->next = head->next;
head->next = tmp;
}
return;
};

void append(Node* head, int val)
{
// insert at the end of list
Node* tmp = new Node(val);
Node* p = head;
if (!head)
head = tmp;
else
{
while(p->next)
p = p->next;
p->next = tmp;
}
return;
};

void insert_node(Node* prev_node, int val)
{
// insert after prev_node
if (!prev_node)
{
cout << "ERROR: the previous node is empty!!! can not insert after this node" << endl;
return;
}
Node* tmp = new Node(val);
tmp->next = prev_node->next;
prev_node->next = tmp;
return;
};

void deleteNode(Node* head, int key)
{
// delete the key first appear in the head
Node* p;
p = head;
while(p && p->next)
{
if (p->next->val == key)
{
Node* deletenode = p->next;
delete (deletenode);
p->next = p->next->next;
}
p = p->next;
}
};

void deleteNode_by_position(Node* head, int pos)
{
Node *p;
p = head;
for (int i = 0; i < pos && p; i++)
{
p = p->next;
}
if (!p)
{
cout << "ERROR:the length of list is small than pos!" << endl;
return;
}
Node* delete_node = p->next;
delete(delete_node);
p->next = p->next->next;
return;
};

int list_length(Node* head)
{
if (head == NULL || head->next == NULL)
return 0;
int r = 0;
Node* tmp = head;

while(tmp && tmp->next)
{
r++;
tmp = tmp->next;
}
return r;
}

int list_length_recursive(Node* head)
{
if (head == NULL)
return 0;
if (head->next == NULL)
return 1;
return (list_length_recursive(head->next) + 1);
}

void test_case()
{
// for testing code
int A[5] = {1,2,3,4,5};
vector<int> vec_A(A, A + sizeof(A)/sizeof(int));
Node* r = create_link_list(vec_A);
print_list(r, "inital_list:");

// insert at the front list
{
insert_front(r, 10);
print_list(r, "insert 10 in front:");
}

// insert at the end of list
{
append(r, 50);
print_list(r, "insert 50 at end:");
}

// insert at certain node
{
Node* p = r;
for (int i = 0; i < 2; i++)
p = p->next;
insert_node(p, -11);
print_list(r, "insert -11 after 2 element:");
}

// delete node at end
{
deleteNode(r, -11);
print_list(r, "delete -11 element");
}

// delete node at position
{
deleteNode(r, 2);
print_list(r, "delete position 3 element");
}

// get the length of list
cout << "the length of list is " << list_length(r)<<endl;

// get the length of list recursive
{
Node* p = r;
cout << "the length of list by recursive" << list_length_recursive(p->next)<<endl;
}

return;
};

int main() {
test_case();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: