思叭整形网思叭整形网

当前位置: 思叭整形网 > 整形百科 > 正文

数据结构单链表代码 数据结构单链表的基本操作代码

本文章由注册用户 陈安芃 上传提供

发布:2024-01-22 评论 纠错/删除



1、数据结构单链表代码

下面是一个简单的单链表的代码示例:

```python

class Node:

def __init__(self, data=None):

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def insert_at_end(self, data):

new_node = Node(data)

if self.head is None:

self.head = new_node

return

current = self.head

while current.next:

current = current.next

current.next = new_node

def delete(self, data):

if self.head is None:

return

if self.head.data == data:

self.head = self.head.next

return

current = self.head

while current.next and current.data != data:

prev = current

current = current.next

if current.data == data:

prev.next = current.next

def display(self):

current = self.head

while current:

print(current.data, end=" ")

current = current.next

print()

# 测试代码

linked_list = LinkedList()

linked_list.insert_at_end(1)

linked_list.insert_at_end(2)

linked_list.insert_at_end(3)

linked_list.insert_at_end(4)

linked_list.display() # 输出: 1 2 3 4

linked_list.delete(3)

linked_list.display() # 输出: 1 2 4

```

以上代码实现了一个简单的单链表,包含了插入、删除和显示链表的功能。节点使用`Node`类表示,链表使用`LinkedList`类表示。使用`insert_at_end`方法可以在链表的末尾插入一个节点,使用`delete`方法可以删除指定节点,使用`display`方法可以显示链表的内容。

在测试代码中,首先创建了一个链表对象`linked_list`,然后调用`insert_at_end`方法插入了四个节点。接着调用`display`方法显示了链表的内容,然后调用`delete`方法删除了值为3的节点,最后再次调用`display`方法显示了修改后的链表内容。

2、数据结构单链表的基本操作代码

以下是单链表的基本操作的代码实现:

```

#include

#include

// 定义节点结构

typedef struct Node {

int data;

struct Node* next;

} Node;

// 初始化链表

Node* init() {

Node* head = (Node*)malloc(sizeof(Node));

head->next = NULL;

return head;

// 链表插入元素

void insert(Node* head, int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;

Node* cur = head;

while (cur->next != NULL) {

cur = cur->next;

}

cur->next = newNode;

// 链表删除元素

void removeNode(Node* head, int val) {

Node* cur = head;

while (cur->next != NULL) {

if (cur->next->data == val) {

Node* temp = cur->next;

cur->next = cur->next->next;

free(temp);

return;

}

cur = cur->next;

}

// 打印链表

void printList(Node* head) {

Node* cur = head->next;

while (cur != NULL) {

printf("%d ", cur->data);

cur = cur->next;

}

printf("\n");

// 销毁链表

void destroy(Node* head) {

Node* cur = head;

while (cur != NULL) {

Node* temp = cur;

cur = cur->next;

free(temp);

}

int main() {

Node* head = init(); // 初始化链表

insert(head, 1); // 插入元素

insert(head, 2);

insert(head, 3);

printf("链表:");

printList(head);

removeNode(head, 2); // 删除元素

printf("删除元素后的链表:");

printList(head);

destroy(head); // 销毁链表

return 0;

```

这段代码实现了链表的初始化,插入元素,删除元素,打印链表和销毁链表等基本操作。通过调用这些函数,可以对链表进行相应的操作。

3、数据结构单链表代码查询方法

查询单链表的代码可以按照以下步骤进行:

1. 定义单链表的节点类。

```java

class ListNode {

int val;

ListNode next;

ListNode(int val) {

this.val = val;

this.next = null;

}

```

2. 定义单链表类,并实现相关方法。

```java

class LinkedList {

private ListNode head;

public LinkedList() {

this.head = null;

}

// 在链表末尾插入节点

public void insert(int val) {

ListNode newNode = new ListNode(val);

if (head == null) {

head = newNode;

} else {

ListNode current = head;

while (current.next != null) {

current = current.next;

}

current.next = newNode;

}

}

// 在指定位置插入节点

public void insert(int val, int pos) {

if (pos size()) {

throw new IndexOutOfBoundsException("Invalid position");

}

ListNode newNode = new ListNode(val);

if (pos == 0) {

newNode.next = head;

head = newNode;

} else {

ListNode current = head;

for (int i = 0; i current = current.next;

}

newNode.next = current.next;

current.next = newNode;

}

}

// 删除指定位置的节点

public void delete(int pos) {

if (pos = size()) {

throw new IndexOutOfBoundsException("Invalid position");

}

if (pos == 0) {

head = head.next;

} else {

ListNode current = head;

for (int i = 0; i current = current.next;

}

current.next = current.next.next;

}

}

// 获取链表长度

public int size() {

int count = 0;

ListNode current = head;

while (current != null) {

count++;

current = current.next;

}

return count;

}

// 输出链表节点的值

public void printList() {

ListNode current = head;

while (current != null) {

System.out.print(current.val + " ");

current = current.next;

}

System.out.println();

}

```

3. 在主函数中测试单链表的代码。

```java

public class Main {

public static void main(String[] args) {

LinkedList list = new LinkedList();

list.insert(1);

list.insert(2);

list.insert(3);

list.insert(4);

list.printList(); // 输出:1 2 3 4

list.insert(5, 2);

list.printList(); // 输出:1 2 5 3 4

list.delete(3);

list.printList(); // 输出:1 2 5 4

System.out.println("Size: " + list.size()); // 输出:Size: 4

}

```

以上代码演示了如何创建、插入、删除和输出单链表的节点。可以根据需求调用相应的方法,并传入相应的参数来操作单链表的节点。

4、数据结构单链表代码c语言

下面是一个简单的C语言代码实现单链表:

```c

#include

#include

// 定义链表节点

struct Node {

int data;

struct Node* next;

};

// 插入节点到链表末尾

void append(struct Node** head_ref, int data) {

// 创建新节点

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

struct Node* last = *head_ref;

// 设置节点值

new_node->data = data;

new_node->next = NULL;

// 如果链表为空,将新节点设为头节点

if (*head_ref == NULL) {

*head_ref = new_node;

return;

}

// 遍历链表找到最后一个节点

while (last->next != NULL) {

last = last->next;

}

last->next = new_node;

// 输出链表

void printList(struct Node* node) {

while (node != NULL) {

printf("%d ", node->data);

node = node->next;

}

printf("\n");

int main() {

struct Node* head = NULL;

// 插入节点到链表末尾

append(&head, 1);

append(&head, 2);

append(&head, 3);

append(&head, 4);

// 输出链表

printList(head);

return 0;

```

上述代码定义了一个简单的`Node`结构体来表示链表节点,其中包含了一个整型的`data`和一个指向下一个节点的指针`next`。`append`函数用于向链表末尾插入新节点,`printList`函数用于输出链表的每个节点的值。

在`main`函数中创建了一个空的链表头`head`,然后调用`append`函数多次插入节点,最后调用`printList`函数输出链表。运行上述代码将输出链表的节点值`1 2 3 4`。

m20220518

相关资讯

文章阅读排行榜

热门话题

猜你喜欢