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 << "插入值成功
单链表程序范例(C++)
最新推荐文章于 2024-10-08 13:47:01 发布
本文详细介绍了如何使用C++编程语言实现单链表的数据结构,包括节点定义、插入、删除等基本操作,旨在帮助读者理解数据结构与算法中的单链表概念。
摘要由CSDN通过智能技术生成