一步一步建立一个单链表
2015-12-08 13:27
387 查看
December 8, 2015 11:53 AM
第二步:建立一个链表
第三步:获得链表长度
第四步:打印链表
第五步:删除指定元素
第六步:在指定位置插入指定元素
第七步:主函数运行测试
1.创建一个单链表
第一步:创建一个链表的节点类[code]class node { public: int data; //存放数据 class node *next; //存放指针,指向下一个结点 };
第二步:建立一个链表
[code]node *create() //建立一个单链表,返回链表的头指针 { node *head, *pLast, *New; //head为头指针,last指向当前列表一个结点 int x, cycle; //New为将要插入的新结点 head = new(node); pLast = head; cout << "please input the number of the records:"<<endl; cin >> cycle; while(cycle--) { cout << "please input the data:"<<endl; while(!(cin >> x)) //判断输入是否有误,如果有误清理缓冲重新输入 { cin.clear(); //清除出错状态 cin.ignore(); //将缓冲区里边现有的数据全部丢弃 cout << "please input the data:"<<endl; } New = new(node); //增加新结点 New->data = x; pLast->next = New; pLast = New; } head = head->next; pLast->next = NULL; return(head); }
第三步:获得链表长度
[code]int length(node*head) //测长 { int n = 0; node *p; p = head; while(p!=NULL) { p = p->next; n++; } return (n); }
第四步:打印链表
[code]void print (node* head) //打印列表 { node *p; int n = length(head); //打印列表 长度 cout << "these "<< n << " records are: " <<endl; p = head; while(p != NULL) { cout << p->data << endl; p = p->next; } }
第五步:删除指定元素
[code]node *del(node* head, int num) //删除值为num的第一个元素 { node *pCur, *pLast; //pCur指向当前正在判断的结点,pLast指向上一个结点 if (NULL != head) //判断是否是空链表 pCur = head; else return (head); while(num != pCur->data && pCur->next != NULL) //迭代找到num的位置 { pLast = pCur; pCur = pCur->next; } if(num == pCur->data) { if(pCur == head) //num在头指针指向的位置 { head = pCur->next; delete(pCur); pCur = NULL; } else //num在其他位置 { pLast->next = pCur->next; delete(pCur); pCur = NULL; } } else { cout << num <<" could't been found!!!"<< endl; } return(head); }
第六步:在指定位置插入指定元素
[code]node *insert(node* head, int num, int pos) //在pos位置插入值为num的结点 { node *pCur, *pNew; //pCur指向当前迭代的结点,pNext下一个结点 int n = 1; //pNew指向新插入的结点 if (NULL != head) pCur = head; else return (head); if (pos<0 || pos>length(pCur)+1) //索引超出范围 { cout << "index out og range!!!" <<endl; } else { while(++n != pos && n < pos+1) //找到需要插入新结点的位置 { pCur = pCur->next; } pNew = new(node); pNew->data = num; if(1 == pos) //开始插入 { pNew->next = head; head = pNew; } else if(length(head)+1 == pos) //结尾处插入 { pNew->next = NULL; pCur->next = pNew; } else //中间位置插入 { pNew->next = pCur->next; pCur->next = pNew; } } return(head); }
第七步:主函数运行测试
[code]int main(int argc, char** argv) { node * head = create(); //创建一个链表 print(head); cout << "let's delete the the 5." << endl; //删除链表元素5 head = del(head, 5); print(head); cout << "let's insert the the 2." << endl; //插入链表元素2 head = insert(head, 2, 2); print(head); return 0; }
2.头指针和头结点:
头指针 | 头结点 |
---|---|
指向第一个节点,是链表必要的元素 | 第一个节点,包括指针和数据,数据可以存放长度等额外信息,不是链表必要的元素 |
3.编程核心:指针
[code]1) 头指针 2) 新对象指针 3) 当前节点指针 4) 下一个节点指针(插入操作) 5) 上一个节点指针(删除操作)
4.编程注意:
[code]1) 函数执行前先判断数据是否合理,是否为空; 2) 数据数据输入后检查输入是否输错,是否存在;类型不匹配的问题; 3) 用delete新建的对象,指针在函数运行结束指向NULL,避免内存泄露; 4) 再做插入、删除操作时,注意需要插入或删除的位置是否越界,是否在边界;
相关文章推荐
- Oracle账号——用于java JDK等软件的下载
- 黑马程序员————IO流
- 重置 MySQL root 密码
- Failed to start LSB 网络服务启动失败的四种解决方法
- Android 性能优化 (1)
- android异步加载图片
- java源码检查工具列表--pmd
- 猫学习IOS(三)UI纯代码UI——图片浏览器
- OC 复合
- 事务边界问题
- php 序列化 serialize
- UIScrollview与Autolayout的那点事
- 自定义ShareDialog视图
- 设置键盘只能输入数字( phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;)
- 【蓝桥杯】 逆波兰表达式
- C# 与 http 协议
- 闯红灯理论
- Android 获取WebView的高度
- SOAP+WSDL
- [UML]UML 教程 - 第二部分