链表(建立.cpp文件)

#include <stdio.h>
#include <stdlib.h>


#define IS_FULL(ptr)  (!(ptr))
// 建立一个整数的链表
typedef struct list_node *list_pointer;
typedef struct list_node {
int data;
list_pointer link;
};
list_pointer ptr = NULL;

//建立二结点的表
list_pointer create()
{
list_pointer first, second;
first = (list_pointer) malloc(sizeof(list_node));
second = (list_pointer) malloc(sizeof(list_node));

second->link = NULL;
second->data = 20;
first->data = 10;
first->link = second;
return first;
}

//在某个结点后插入操作
void insert(list_pointer *ptr, list_pointer node)
{
list_pointer temp;
temp = (list_pointer) malloc(sizeof(list_node));

if (IS_FULL(temp))
{
fprintf(stderr, "The memory is full\n");
exit(1);
}
temp->data = 50;
if (*ptr)
{
temp->link = node->link;
node->link = temp;
}
else
{
temp->link = NULL;
*ptr = temp;
}
}

//删除操作

void deleteitem(list_pointer *ptr, list_pointer trail, list_pointer node)
{
if (trail)
{
trail->link = node->link;
}
else
{
*ptr = (*ptr)->link;
}
free(node);
}

// 表的输出
void print_list(list_pointer ptr)
{
printf("The list contains: ");
for(; ptr; ptr = ptr->link)
printf("%4d", ptr->data);
printf("\n");
}

void main()
{
//获得二结点链表指针
ptr = create();
print_list(ptr);

//在第一个结点后插入
list_pointer first = ptr;
insert(&ptr, first);
print_list(ptr);

//删除第二个结点
list_pointer second = ptr->link;
deleteitem(&ptr, first, second);
print_list(ptr);
}
链接列表(Link List)是一种基础的数据结构,它由一系列节点组成,每个节点包含数据域和指向下一个节点的指针。在C++中,我们可以编一个名为`linklist.cpp`的文件,其中包含单链表的基本操作,例如创建链表、插入元素、删除元素、查找元素、遍历等。 以下是`linklist.cpp`的一个简化版示例,实现了单链表及其基本操作: ```cpp #include <iostream> // 定义链表节点 struct Node { int data; Node* next; }; // 创建一个新的链表节点 Node* createNode(int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = nullptr; return newNode; } // 插入新元素到链表的特定位置 void insertNode(Node** head, int value, int position) { if (position <= 0) { std::cout << "Invalid position.\n"; return; } Node* newNode = createNode(value); if (*head == nullptr) { // 如果链表为空 *head = newNode; return; } Node* current = *head; for (int i = 1; i < position && current != nullptr; ++i) { current = current->next; } if (current != nullptr) { newNode->next = current->next; current->next = newNode; } else { std::cout << "Position out of range.\n"; } } // 删除指定值的节点 void deleteNode(Node** head, int value) { if (*head == nullptr) return; if ((*head)->data == value) { Node* temp = *head; *head = (*head)->next; delete temp; return; } Node* current = *head; while (current->next != nullptr && current->next->data != value) { current = current->next; } if (current->next != nullptr) { Node* toDelete = current->next; current->next = current->next->next; delete toDelete; } else { std::cout << "Value not found in the list.\n"; } } // ... 其他链表操作可以继续添加 ... ``` 接下来,在`exp7-1.cpp`中,你可以导入这个头文件,并通过主函数调用`linklist.cpp`中的函数来完成具体操作,比如构建链表、显示链表内容以及执行其他操作。这里仅展示部分代码: ```cpp #include "linklist.h" // 包含linklist.cpp中的定义 // 主函数 int main() { Node* head = nullptr; // 建立链表 head = createNode(5); insertNode(&head, 3, 0); // 在开头插入 insertNode(&head, 8, 1); // 在第二个位置插入 // 输出链表 printList(head); // 自定义函数,打印链表元素 // 删除节点 deleteNode(&head, 3); // 遍历并输出剩余节点 printList(head); // 等待用户输入或其他操作... return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值