c++实现单链表创建,删除,遍历,插入,修改操作

Posted shixin_0125

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++实现单链表创建,删除,遍历,插入,修改操作相关的知识,希望对你有一定的参考价值。

#include<iostream>    
#include<string>
#include<malloc.h>
using namespace std;

struct Node

int data;
struct Node* next;
;

struct Node* build_list(int n)

Node *head =(struct Node*)malloc(sizeof(Node));
if (head == NULL)

cout<<"malloc failed!"<<endl;
return head;

head->data = 1;
head->next = NULL;

Node *curNode = head;
for (int i=1;i<n;i++)

Node *newNode = (struct Node*)malloc(sizeof(Node));
if (newNode == NULL)

cout<<"malloc failed!"<<endl;
return head;

newNode->data = i+1;
newNode->next = NULL;

curNode->next = newNode;
curNode = newNode;

return head;

struct Node* addtail_node_list(struct Node* head, struct Node* newNode)

if (head == NULL || newNode == NULL)

return head;

struct Node* pcurrNode=head;
while(pcurrNode->next != NULL)

pcurrNode = pcurrNode->next;

if (pcurrNode != NULL)

pcurrNode->next = newNode;
newNode->next = NULL;

return head;


struct Node* addprev_node_list(struct Node* head, struct Node* newNode,int key)

if (head == NULL || newNode == NULL)

return head;

struct Node* pcurrNode = head;
struct Node* pprevNode = NULL;
while(pcurrNode != NULL)

if (pcurrNode->data == key)

if (pcurrNode == head)

newNode->next = head;
head = newNode;

else

pprevNode->next = newNode;
newNode->next = pcurrNode;

break;

pprevNode = pcurrNode;
pcurrNode = pcurrNode->next;

return head;


struct Node* modkey_node_list(struct Node* head, int key, int modval)

if(head==NULL)

return head;


struct Node* pCurrNode=head;
while(pCurrNode!=NULL)

if (pCurrNode->data == key)

pCurrNode->data = modval;
break;

pCurrNode=pCurrNode->next;



return head;


struct Node* query_node_list(struct Node* head, int key)

if(head==NULL)

return head;


struct Node* pCurrNode=head;
while(pCurrNode!=NULL)

if (pCurrNode->data == key)

return pCurrNode;

pCurrNode=pCurrNode->next;



return NULL;

struct Node* delete_node_list(struct Node* head, int key)

if(head==NULL)

cout<<"list is null"<<endl;
return head;


单链表的创建插入删除遍历的Go实现

双向链表(c++实现)

输入一组整数,建立带头结点的单链表,并实现线性表的求长度、插入和删除等操作?

数据结构作业~急求~~~用c语言或c++ 使用单链表实现系统进程列表,完成插入、删除、查询等操作。

单链表实现“插入”和“删除”操作

本科课程数据结构与算法实验2——单链表与双向循环链表的插入删除操作(C++实现)