数据结构/循环链表
2014-12-03 17:20
204 查看
尾指针 *real
(head->next=dead)
1初始化
void ds_init( node **pNode)
{
int item;
node *temp,*target;
printf("输入终点的值,输入0完成出初始化\n");
while(1)
{
scanf("%d",&item);
fflush(stdin);
if (item==0)
return ;
if ((*pNode)==NULL)
{//循环链表只有一个节点
*pNode=(node*)malloc(sizeof(struct CLinklist));
if (!(*pNode))
{
exit (0);
(*pNode)->date=item;
(*pNode)->next=*pNode;
}
else {
//找到next指向第一个节点的终点
for (target=(*pNode); target->next!=(*pNode);target=target->next)
{
//生成一个新的终点
temp=(*node)malloc(sizeof(struct CLinklist));
if (!temp)
exit(0);
temp->date=item;
temp->next=*pNode;
target->next=temp;
}
}
}
}
}
2链表的插入:
//链表存储结构的定义
typedef struct CLinkList
{
int date;
struct CLinkList *next;
}node;
//插入终点
//参数: 链表的第一个节点;插入的位置
void ds_insert(node **pNode,int i )
{
node *temp;
node *target,*p;
int j=i;
printf("输入要插入的节点的值:");
scanf("%d",&item);
if (i==1)
{
//新插入的节点的第一个节点
temp=(node*)malloc(sizeof(struct CLinkList));
if (!temp)
exit(0);
temp->date=item;
//寻找到最后一个节点
for (target=(*pNode);target->next!=(*pNode);target=target->next);
temp->next=(*pNode);
target->next=temp;
*pNode=temp;
}
else
{
target=*pNode;
for (; j < (i-1); j++)
{
target=target->next;
/* code */
//循环2次
}
temp=(node*)malloc(sizeof(struct CLinkList));
if (!item)
exit (0);
temp->date=item;
p=target->next;
temp->next=p;
}
}
3 删除节点:
//删除节点
//参数说明:参数1:该节点的值;参数2:第i个节点
void ds_delete(node **pNode ,int i)
{
node *target,*temp;
int j=1;
if (i==1)
{
//删除的是di一个节点
//找到最后一个节点
for (target =*pNode ;target->next!=*pNode;target=target->next)
{
temp=*pNode;
*pNode=(*pNode)->next;
target->next=*pNode;
free(temp);
/* code */
}
else
{
target=*pNode;
for (; j < i-1; j++)
{
target=target->next;
/* code */
}
temp=target->next;
target->next=temp->next;
free(temp);
}
}
}
4 查找节点:
//查找节点
int ds_search(node *pNode ,int elem)
{
node *target;
int i=1;
for (target=*pNode; target->date!=elem&&target->next!=*pNode; ++i)
{
target=target->next;
/* code */
}
if (target->next==pNode)//表中不存在的元素
return -1;
else return 1;
}
(head->next=dead)
1初始化
void ds_init( node **pNode)
{
int item;
node *temp,*target;
printf("输入终点的值,输入0完成出初始化\n");
while(1)
{
scanf("%d",&item);
fflush(stdin);
if (item==0)
return ;
if ((*pNode)==NULL)
{//循环链表只有一个节点
*pNode=(node*)malloc(sizeof(struct CLinklist));
if (!(*pNode))
{
exit (0);
(*pNode)->date=item;
(*pNode)->next=*pNode;
}
else {
//找到next指向第一个节点的终点
for (target=(*pNode); target->next!=(*pNode);target=target->next)
{
//生成一个新的终点
temp=(*node)malloc(sizeof(struct CLinklist));
if (!temp)
exit(0);
temp->date=item;
temp->next=*pNode;
target->next=temp;
}
}
}
}
}
2链表的插入:
//链表存储结构的定义
typedef struct CLinkList
{
int date;
struct CLinkList *next;
}node;
//插入终点
//参数: 链表的第一个节点;插入的位置
void ds_insert(node **pNode,int i )
{
node *temp;
node *target,*p;
int j=i;
printf("输入要插入的节点的值:");
scanf("%d",&item);
if (i==1)
{
//新插入的节点的第一个节点
temp=(node*)malloc(sizeof(struct CLinkList));
if (!temp)
exit(0);
temp->date=item;
//寻找到最后一个节点
for (target=(*pNode);target->next!=(*pNode);target=target->next);
temp->next=(*pNode);
target->next=temp;
*pNode=temp;
}
else
{
target=*pNode;
for (; j < (i-1); j++)
{
target=target->next;
/* code */
//循环2次
}
temp=(node*)malloc(sizeof(struct CLinkList));
if (!item)
exit (0);
temp->date=item;
p=target->next;
temp->next=p;
}
}
3 删除节点:
//删除节点
//参数说明:参数1:该节点的值;参数2:第i个节点
void ds_delete(node **pNode ,int i)
{
node *target,*temp;
int j=1;
if (i==1)
{
//删除的是di一个节点
//找到最后一个节点
for (target =*pNode ;target->next!=*pNode;target=target->next)
{
temp=*pNode;
*pNode=(*pNode)->next;
target->next=*pNode;
free(temp);
/* code */
}
else
{
target=*pNode;
for (; j < i-1; j++)
{
target=target->next;
/* code */
}
temp=target->next;
target->next=temp->next;
free(temp);
}
}
}
4 查找节点:
//查找节点
int ds_search(node *pNode ,int elem)
{
node *target;
int i=1;
for (target=*pNode; target->date!=elem&&target->next!=*pNode; ++i)
{
target=target->next;
/* code */
}
if (target->next==pNode)//表中不存在的元素
return -1;
else return 1;
}
相关文章推荐
- 数据结构---线性表----循环链表和双向链表
- 数据结构面试之二——双向链表表、循环链表、有序链表的常见操作
- 数据结构-循环链表(学习笔记)
- 数据结构——循环链表
- 数据结构 -- 双向循环链表
- 数据结构通用双向循环链表实现
- Java数据结构——循环链表的实现
- 【数据结构】循环链表与双向链表
- 数据结构---线性表----循环链表和双向链表
- 一步一步学数据结构之1--1(循环链表)
- 数据结构(六)——循环链表
- Python写数据结构:双向循环链表
- 数据结构循环链表基本操作实例演示
- 数据结构---线性表----循环链表和双向链表
- 数据结构-C++实现(三)循环链表
- 数据结构——循环链表、仅设尾指针的循环链表、约瑟夫环
- 数据结构系列之链表——循环链表(约瑟夫环)
- [数据结构]循环链表(c++/类模板)用例JosephRing
- 数据结构学习笔记(5.线性表之双向循环链表)
- JAVA 数据结构链表操作循环链表