数据结构实验之链表七:单链表中重复元素的删除
2016-07-27 11:15
519 查看
删除时
用到三个指针
#include <iostream> #include <algorithm> #include <string> using namespace std; typedef struct node { int data; node *next; }Linklist; Linklist *head, *p, *tail, *q; void nxcreat(int n) { head=new Linklist; head->next=NULL; while(n--) { p=new Linklist; cin>>p->data; p->next=head->next; head->next=p; } } void display() { p=head->next; while(p->next) { cout<<p->data<<" "; p=p->next; } cout<<p->data<<endl; } int del(int n) { Linklist *t; p=head->next; while(p) { t=p; q=p->next; while(q) { if(p->data==q->data) { n--; t->next=q->next; free(q); q=t->next; } else { q=q->next; t=t->next; } } p=p->next; } return n; } int main() { int n; cin>>n; nxcreat(n); cout<<n<<endl; display(); cout<<del(n)<<endl; display(); return 0; }
用到三个指针
相关文章推荐
- 高并发数据结构Disruptor解析(3)
- 数据结构实验之链表七:单链表中重复元素的删除 sdutoj2122
- 数据结构实验之栈四:括号匹配
- 数据结构实验之链表二:逆序建立链表
- 数据结构上机测试1:顺序表的应用
- 数据结构实验之链表一:顺序建立链表
- 数据结构上机测试2-2:单链表操作B
- Redis学习笔记~Redis提供的五种数据结构
- 树和二叉树的一些知识
- [树状数组] poj 2155 Matrix
- 数据结构——二叉查找树、AVL树
- 顺序表应用2:多余元素删除之建表算法
- 数据结构上机测试1:顺序表的应用 7.24补
- 数据结构上机测试2-1:单链表操作A
- 顺序表应用1:多余元素删除之移位算法
- SDUT 3363 数据结构实验之图论七:驴友计划
- 数据结构上机测试1:顺序表的应用
- 数据结构实验之查找二:平衡二叉树
- 数据结构实验之查找一:二叉排序树
- POJ 3468 A Simple Problem with Integers (线段树区间修改)