使用四種編程語言實作單鏈表的增刪查改
本文接著上一篇文章~
本篇文章僅僅只是為了實作,所以很多地方就不使用條件判斷了~
線性表的基本實作和概念C語言
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node* next;
}*Node;
/*
初始化
*/
Node head;
void init() {
head = (Node)malloc(sizeof(Node));
head->next = NULL;
head->data = NULL;
}
/*
添加
*/
void add(int data) {
Node temp = (Node)malloc(sizeof(Node));
temp->data = data;
temp->next = head->next;
head->next = temp;
}
/*
遍歷
*/
void toString() {
//定義一個變數替代遍歷,防止破壞原鏈表的資料
Node temp = head->next;
while (temp != NULL) {
//判斷是不是到了最后,如果是最后就省去->
if (temp->next != NULL) {
printf("%d->",temp->data);
}
else {
printf("%d",temp->data);
}
temp = temp->next;
}
}
/*
根據索引查找
*/
Node get(int index) {
int i;
Node temp = head->next;
for (i = 0; i <= index; i++) {
temp = temp->next;
}
return temp;
}
/*
根據索引洗掉
*/
void del(int index) {
int i = 0;
Node front, rear;
//呼叫查找方法找到它
front = get(index);
//開始洗掉
rear = front->next;
front->next = front->next->next;
}
/*
根據索引修改
*/
void replace(int index,int data) {
Node temp = get(index);
temp->data = data;
}
//開始測驗
int main(int argc,char *argv[]) {
Node temp;
//先初始化一下單鏈表
init();
//新增
add(1); add(2); add(3); add(4); add(5); add(6); add(7);
//遍歷
toString();
//查找
temp = get(3); printf("\n%d\n",temp->data);
//修改
replace(3, 9); toString();
//洗掉
del(4); printf("\n"); toString();
return 0;
}
C++語言
#include<iostream>
using namespace std;
//結點
template<class T>
class Node {
public:
T data;
Node<T>* next;
};
//鏈表操作
template<class T>
class LinkList {
private:
Node<T> *head = new Node<T>();
public:
//清理垃圾
~LinkList() {
delete head;
}
/*
添加
*/
void add(int data) {
Node<T>*temp = new Node<T>();
temp->data = data;
temp->next = head->next;
head->next = temp;
}
/*
遍歷
*/
void toString() {
//定義一個變數替代遍歷,防止破壞原鏈表的資料
Node<T> *temp = head->next;
while (temp != NULL) {
//判斷是不是到了最后,如果是最后就省去->
if (temp->next != NULL) {
printf("%d->", temp->data);
}
else {
printf("%d", temp->data);
}
temp = temp->next;
}
}
/*
根據索引查找
*/
Node<T>*get(int index) {
int i;
Node<T>*temp = head->next;
for (i = 0; i <= index; i++) {
temp = temp->next;
}
return temp;
}
/*
根據索引洗掉
*/
void del(int index) {
int i = 0;
Node<T>*front, *rear;
//呼叫查找方法找到它
front = get(index);
//開始洗掉
rear = front->next;
front->next = front->next->next;
delete rear;
}
/*
根據索引修改
*/
void replace(int index, int data) {
Node<T>*temp = get(index);
temp->data = data;
}
};
/*
測驗 洗掉和修改呼叫了查找,就懶得寫了,,,
*/
int main(int argc,char*argv[]) {
LinkList<int>list;
//增加
for(int i = 1; i < 10; i++)
list.add(i);
//遍歷
list.toString(); cout << endl;
//洗掉
list.del(5); list.toString(); cout << endl;
//修改
list.replace(2, 666); list.toString();
}
Java語言
public class Test {
public static void main(String[] args) {
SingleLinkedList list = new SingleLinkedList();
//添加
for(int i = 1; i < 10; i++)
list.add(i);
//遍歷
System.out.println(list.toString());
//洗掉
list.remove(5);
System.out.println(list.toString());
//修改
list.replace(3,999);
System.out.println(list.toString());
}
}
class SingleLinkedList{
private Node head = new Node(); //頭節點
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
Node p = head.next;
while(p != null){
//看看是不是到了最后
if(p.next != null){
stringBuilder.append(p.data + "->");
}else{
stringBuilder.append(p.data);
}
p = p.next;
}
stringBuilder.append("]");
return stringBuilder.toString();
}
//這里換一種思路,省的代碼都一樣,,,
public void add(Object object) {
//先讓p指向頭節點
Node p = head;
//將p移動到最后一個結點
while(p.next != null){
p = p.next;
}
//添加元素
p.next = new Node(object);
}
//洗掉
public void remove(int index) {
要洗掉的前一個結點
Node prev = (Node) getNode(index - 1);
//當前要洗掉的結點
Node currNode = prev.next;
Node nextNode = currNode.next;
prev.next = nextNode;
currNode.next = null;
}
//查找
public Object getNode(int index) {
// 指向了第一個結點
Node p=head.next;
for (int i = 0; i <index ; i++) {
p=p.next;
}
// 回傳的是p結點
return p;
}
//修改
public void replace(int index,Object data){
Node temp = (Node)getNode(index);
temp.data = data;
}
//將Node作為單鏈表的內部類來使用
class Node {
Object data;//存盤是資料
Node next;//指向下個節點的指標
public Node() {
}
public Node(Object data) {
this.data = data;
}
public Node(Object data, Node next) {
super();
this.data = data;
this.next = next;
}
public String toString() {
return "Node [data="https://blog.csdn.net/qq_41424688/article/details/ + data + ", next=" + next + "]";
}
}
}
Python語言
class Node():
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def __str__(self):
return 'Node:{}'.format(self.value)
class LinkedList():
def __init__(self):
self.root = Node()
# 記錄有多少元素
self.size = 0
# 增加新資料時,將新資料的地址與誰關聯
self.next = None
#新增
def append(self, value):
node = Node(value)
# 判斷是否已經有資料
if not self.next: # 如果沒有節點時
# 將新節點掛到root后面
self.root.next = node
else:
# 將新節點掛到最后一個節點上
self.next.next = node
self.next = node
self.size += 1
def append_first(self, value):
node = Node(value)
if not self.next:
self.root.next = node
self.next = node
else:
# 獲取原來root后面的那個節點
temp = self.root.next
# 將新的節點掛到root上
self.root.next = node
# 新的節點的下一個節點是原來的root后的節點
node.next = temp
self.size += 1
def __iter__(self):
current = self.root.next
if current:
while current is not self.next:
yield current
current = current.next
yield current
def find(self, value):
for n in self.__iter__():
if n.value == value:
return n
def find_count(self, value):
count = 0
for n in self.__iter__():
if n.value == value:
count += 1
return count
def remove(self, value):
temp = self.root
for n in self.__iter__():
# 判斷節點的值與要洗掉的值是否相等
if n.value == value:
# 查看是不是最后一個節點
if n == self.next:
# 更新倒數第二節點的關系
temp.next == None
# 更新最后一個節點為原倒數第二個節點
self.next = temp
temp.next = n.next
del n
self.size -= 1
return True
temp = n
def remove_all(self, value):
temp = self.root
for n in self.__iter__():
if n.value == value:
if n == self.next:
temp.next == None
self.next = temp
temp.next = n.next
del n
self.size -= 1
continue
temp = n
if __name__ == "__main__":
link = LinkedList()
link.append('姜子牙')
link.append('姬昌')
link.append('姬發')
link.append('雷震子')
link.append('商紂王')
link.append('商紂王')
link.append('楊戩')
link.append('商紂王')
link.append('通天教主')
link.append_first('申公豹')
link.append_first('商紂王')
print('-----洗掉之前------')
for v in link:
print(v)
link.remove_all('商紂王')
print('-----洗掉之后------')
for v in link:
print(v)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/3406.html
標籤:區塊鏈
上一篇:python-酷我音樂(爬蟲)
下一篇:Python中的條件分支結構
