C++学习笔记之单向链表
来源:互联网 发布:步摇 知乎 编辑:程序博客网 时间:2024/05/17 03:23
例子:
struct node { int data; node * next; };
next:指向下一个node类型的结构,连接node 的纽带
struct student { int num; char name[20]; char sex; float score; student * next;};
动态申请内存的方法
student *p=(student*) malloc(sizeof(student)); student * p = new student;
建立单向链表
- 声明一个链首指针变量head,并赋初值NULL(包含0个节点的链表)
- 动态分配一个新节点,将该节点链入链尾
- 重复上一步
例子1:建立链表,读入n个整数,每个整数作为一个新结点插入到链尾
#include <iostream>using namespace std;struct node { int data;node * next;};node * createList(int n);int main() {int n;node * listHead = NULL;cout << "Please enter the number of nodes:";cin >> n;if (n > 0)listHead = createList(n);return 0;}node *createList(int n) {node *temp, *tail = NULL, *head = NULL ;int num;cin >> num;head = new node ; // 为新节点动态分配内存if (head == NULL) {cout << "No memory available!";return NULL;}else {head->data = num;head->next = NULL;tail = head;}for ( int i = 0; i < n - 1; i ++) {cin >> num;temp = new node ; // 为新节点动态分配内存if (temp == NULL) {cout << "No memory available!";return head;}else{temp->data = num;temp->next = NULL;tail->next = temp;tail = temp;}}return head ;}
遍历链表
依次访问链表中的每个节点的信息
head->data = 15;head->next->data = 15;
一般遍历方法
node * curNode = head;while (curNode )curNode = curNode->next;
例子2:编写一个函数,输出例1链表中各节点的data成员的值
void outputList(node * head){cout << "List: ";node *curNode = head;while ( curNode ) {cout << curNode->data;if (curNode ->next)cout << " -> ";curNode = curNode ->next;}cout << endl;return;}
例子3:编写一个函数,在例1的链表中查找包含指定整数的节点
node * findData(int n, node * head){node *curNode = head;while ( curNode ) {if ( curNode->data == n) {cout<<"Find "<<n<<" in the list."<<endl;return curNode;}curNode = curNode->next;}cout<<"Can't find "<<n<<" in the list."<<endl;return NULL;}
在链表中节点a之后插入节点c
例子4:编写一个函数,将输入的整数从小到大插入链表
node * insertData(int n, node * head){node *curNode = head;// 指向插入点的后节点node *preNode = NULL;// 指向插入点的前节点node *newNode = NULL;// 指向新建节点while ((curNode!=NULL)&&(curNode->data<n)) {preNode = curNode; // 后节点变为前节点curNode = curNode->next; }newNode = new node ; if (newNode == NULL) {cout << "No memory available!";return head;}newNode->data = n;if (preNode == NULL) //插入到链表头{newNode->next = curNode; return newNode; }else {preNode->next = newNode; newNode->next = curNode; return head; }}
从链表中删除一个节点c
(1)在链表中查找要删除的节点c,用指针cptr指向节点c;
(2)如果c有前驱节点(设为d,用指针dptr指向d),则将d的后继指针指向c的后继节点:dptr->next=cptr->next
例子5:编写一个函数,删除链表中包含指定整数的节点
node * deleteData(int n, node * head) {node *curNode = head;// 指向当前节点node *preNode = NULL;// 指向当前节点的前驱节点 while (curNode && curNode->data != n) { preNode = curNode; // 当前节点变为前驱节点 curNode = curNode->next; } if (curNode == NULL) { cout<<"Can't find "<<n<<" in the list"<<endl; return head; } if (preNode == NULL) //删除链首节点 head = head->next; else preNode->next = curNode->next; delete curNode; return head;// 返回链首指针}
- C++学习笔记之单向链表
- 单向链表学习笔记
- 学习笔记--单向链表
- C语言学习之单向链表操作
- Linux C 学习 单向链表
- Linux C 学习 单向链表
- zephyr学习笔记---单向链表slist
- C语言之单向链表
- C语言之单向链表封装
- 数据结构学习之单向链表结构
- 数据结构学习之单向链表结构
- C——(单向、单向循环、双向、双向循环)链表学习总结
- C——单向链表的学习
- 单向链表(C语言实现)学习记录
- C++ STL学习笔记五 slist单向链表容器
- 数据结构学习笔记——单向链表
- C++ STL学习笔记五 slist单向链表容器
- C++ STL学习笔记五 slist单向链表容器
- HTML <span> 标签简介
- 关于C语言编程中include的用法
- 解决ASP.NET 4.0 "A potentially dangerous Request.Form value was detected from the client". 错误
- 关于S3C2440板子和电脑的网络连线
- DPI
- C++学习笔记之单向链表
- Windows2003 中"FTP:Inode 是远程的"问题
- 优秀压缩算法UDA-PAQD2 0.284
- PHP破坏文件
- 关于《学编程,还有必要上大学吗? 》
- 点到线段的最近点
- Minimum Cost----最小费用流
- 接口和抽象类的作用
- 不论使用什么牌子的美白护肤品都需要注意防晒防冻