单链表程序范例(C++)

本文详细介绍了如何使用C++编程语言实现单链表的数据结构,包括节点定义、插入、删除等基本操作,旨在帮助读者理解数据结构与算法中的单链表概念。
摘要由CSDN通过智能技术生成
  1 #include<iostream>
  2 #include<stdlib.h>
  3 using namespace std;
  4 typedef struct tagNote
  5 {
  6     int nNumber;
  7     struct tagNote* pNext;
  8 }Note;
  9 
 10 bool CreateList(Note*&pListHead);
 11 bool DisposeList(Note*&pListHead);
 12 bool ListInsertItem(Note*&pListHead, int nValue, int nIndex = -1);
 13 bool ListItemPushBack(Note*&pListHead,int nValue);
 14 bool ListItemPushFront(Note*&pListHead,int nValue);
 15 bool ListDeleteItem(Note*&pListItem,int nIndex=-1);
 16 bool ListDeleteFirstItem(Note*&pListHead);
 17 bool ListDeleteLastItem(Note*&pListHead);
 18 bool ListGetFirstItem(Note*&pListHead,int &nValue);
 19 bool ListGetLastItem(Note*&pListHead,int &nValue);
 20 bool ListGetItemNumber(Note*&pListHead,int &nNumber);
 21 bool ListGetItem(Note*&pListHead,int&nValue,int nIndex=-1);
 22 bool ListPrint(Note*&pListHead);
 23 void ShowMenu();
 24 
 25 int main()
 26 {
 27     Note*pListHead = NULL;
 28     int nSelect = 0;
 29     bool isLoop = true;
 30     int nTmpInput1, nTmpInput2, nTmpOut;
 31     while (isLoop)
 32     {
 33         ShowMenu();
 34         cin >> nSelect;
 35         switch (nSelect)
 36         {
 37         case 0:
 38             isLoop = false;
 39             break;
 40         case 1:
 41             if (!CreateList(pListHead))
 42             {
 43                 cout << "创建链表失败" << endl;
 44             }
 45             else
 46             {
 47                 cout << "创建链表成功" << endl;
 48             }
 49             break;
 50         case 2:
 51             cout << "请输入插入项的值:" << endl;
 52             cin >> nTmpInput1;
 53             if (!ListItemPushFront(pListHead, nTmpInput1))
 54             {
 55                 cout << "输入插入值(" << nTmpInput1 << ")失败" << endl;
 56             }
 57             else
 58             {
 59                 cout << "插入项成功" << endl;
 60             }
 61             break;
 62         case 3:
 63             cout << "请输入插入项的值:" << endl;
 64             cin >> nTmpInput1;
 65             if (!ListItemPushBack(pListHead, nTmpInput1))
 66             {
 67                 cout << "输入插入值(" << nTmpInput1 << ")失败" << endl;
 68             }
 69             else
 70             {
 71                 cout << "插入值成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值