用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表)、结点的查找、删除、排序、打印输出、逆置、链表销毁等基本操作。
IDE:vs2013
具体实现代码如下:
#include "stdafx.h"
#include <malloc.h>
#include <iostream>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}*node;
node head_creat() //头插法建立单链表
{
node head = (struct Lnode *)malloc(sizeof(struct Lnode));
head->next = NULL;
node p;
int temp;
while (cin >> temp)
{
p = (node)malloc(sizeof(struct Lnode));
p->data = temp;
p->next = head->next;
head->next=p;
}
return head;
}
bool search(node h, int target) //查找某元素是否在链表中
{
int flag = 0;
node p = h->next;
if (h->next == NULL)
return false;
for (; p != NULL;)
{
if (p->data == target)

这篇博客详细介绍了如何使用C++实现单链表的基本操作,包括头插法和尾插法创建链表,节点查找、删除、排序、打印输出、链表逆置以及销毁链表等功能。所有操作都在VS2013环境下完成。
最低0.47元/天 解锁文章
11万+

被折叠的 条评论
为什么被折叠?



