单链表 链表
来源:互联网 发布:淘宝手机版新品上架 编辑:程序博客网 时间:2024/06/15 01:44
最近学了下数据结构,真心觉得自己学起来比较吃力,东西总是记不住,而且还学得不好,因此,毅然觉得把自己所学的过程中理解写出来,也算是对自己所学知识的一点总结,其中难免错漏百出,如果你发现了请您告诉我一下,欢迎指出错误,以免误导不小心点进来看的人,好了,下面开始写下自己的理解:
structNode
{
int data;
Node *next;
};
Node*create1() //头插法
{
Node *head new= Node;
NULL; head->next =
for(int i = 0; i < 10; i++)
{
Node *s new= Node;
s->data = i + 1;
//这次的s的next区域指向上次头结点next节点,形成连接 s->next = head->next;
//令头结点next区域指向s结点 head->next = s;
}
return head;
}
Node*create2() //尾插法
{
Node *rear,*head,*s;
new head = Node;
//初始化是空表 rear = head;
for ( inti = 0; i < 10; i++)
{
new s = Node;
s->data = i + 10;
//尾结点next区域指向新节点 rear->next = s;
//新节点成为尾结点 rear = s;
}
NULL;//之后不要忘记置尾结点的后继指针为空 rear->next =
return head;
}
intgetLength( Node* head)//获得表的长度
{
Node *s head->next;=
int count = 0;
while( s NULL)!=
{
count++;
s= s->next;
}
return count;
}
Node*getPointer( Node* head,intposition)//获得第position个位置的指针,位置是从1开始计算
{
if(position > head)getLength( ) //位置大于链表的长度
{
"非法获取"<<endl; cout<<
return NULL;
}
int count = 1;
Node *s head->next;=
while(count < position)
{
s = s->next;
count++;
}
cout<<s->data<<endl;
return s;
}
voidinsert( Node* head,intdata , intposition)//往第positon后个位置插入数据data
{
if(position > head)getLength( || position< //位置非法0)
{
"非法插入"<<endl; cout<<
return;
}
Node *s head,position);//得到第position个位置的指针= getPointer(
Node *p new= Node;
data; p->data =
//新结点的next指针指向之前的结点的next指针 p->next = s->next;
//第positioni个next指针指向新结点 s->next = p;
}
Node*deleteData( Node* head,intposition)
{
if(position > head)getLength( || position<= //位置非法0)
{
"删除失败!"<<endl; cout<<
return head;
}
if(position == //如果是删除第一个位置上的数1)
{
//cout<<"正在尝试删除第一个点"<<endl;
Node *s head->next;=
delete(head);
head = s;
return head;
}
Node *s head,position= getPointer( - //得到待删点的前驱1);
Node * //得到待删点的指针p = s->next;
//待删点的前驱指向待删点的后驱,实现了链表的连接 s->next = p->next;
delete(p);
return head;
}
int_tmain(int argc, _TCHAR* argv[])
{
"dd"<<endl; cout<<
Node *head new= Node;
//head->next = NULL;
///*
head = create2();
"表长度为"<<getLength(head)<<endl; cout<<
//insert(head,20,5);
//getPointer(head,1);
//deleteAll(head);
//if(head==NULL)cout<<"表头为空"<<endl;
head=deleteData(head,5);
Node *q = head->next;
while(q != NULL)
{
" cout<<q->data<< ; "
q = q->next;
}
}
- 单链表 链表
- 单链表反转 -面试
-  
-
- 结构体 联合体 链表 枚举和位域
- hdoj acm 1008
- HDOJ 1984 Mispelling4
- em的错误 <转载>
- 集线器 交换机 路由器 网桥 …
- 死亡日志 ——黄妮超
- 使用BBED修改SCN …
- 【单链表】 c语言的单链表
- IOS 99乘法表
- 考 研 问 答
- hdoj acm 1029
- Rightmost Digit hdoj 1061
- HDOJ 1005 Number Sequence
- HDOJ 1722 Cake
- glPushMatrix和glPopMatrix的作用 …
- glViewport()与glOrtho()函数的理…
- [转]java多线程同步,wait和notify
- android自定义view在xml中抛出异常…
- c#下用process.StartTime报异常解…
- 单链表 链表
- 双链表
- 循环双链表
- 栈的链表实现
- 迷宫路径搜索问题(栈的链表实现)
- 随想录2013.5.1
- AsyncTask实现下载网络图片
- 最大字段和(动态规划法)
- 串(链式存储)