单向链表的实现
2015-10-19 22:28
281 查看
一、单向链表的实现
struct list*create_list()//建立一个节点
void traverse(struct list*ls)//循环遍历链表
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
int delete_list(struct list*ls,int n)
//删除指定位置元素
int count_list(struct list *ls)
//返回链表元素个数
void merge_list(struct list*ls1,struct list*ls2)
//合并两个链表,结果放入ls1中
struct list*last_list(struct list*ls)
//得到链表最后一个节点
int elem_pos(struct list*ls,int data)
//返回数据域等于data的节点位置
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
int empty_list(struct list*ls)
//返回链表是否为空
void clear_list(struct list*ls)
//清空链表,只保留首节点
void reverse(struct list*ls)
//链表逆置
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;//数据域
struct list *next;//指针域
};
struct list*create_list()
//建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls)
//循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;//p指向它对应的下一个节点
}
}
int main()
{
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
traverse(first);
return 0;
}
打印结果:
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;
//数据域
struct list *next;//指针域
};
struct list*create_list() //建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;
//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls) //循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;
//p指向它对应的下一个节点
}
}
int delete_list(struct list*ls,int n) //删除指定位置元素
{
struct list*tmp;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return -1;
//n的位置不合适
}
tmp=p->next;
p->next=p->next->next;
free(tmp);
return 0;
//删除成功
}
int count_list(struct list *ls) //返回链表元素个数
{
struct list*p=ls;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
void clear_list(struct list*ls) //清空链表,只保留首节点
{
struct list*p=ls->next;
while(p)
{
struct list*tmp=p->next;
free(p);
p=tmp;
}
ls->next=NULL;//只有首节点,那么首节点的next也应该置为NULL
}
int empty_list(struct list*ls) //返回链表是否为空
{
if(ls->next)
return 0;
else
return -1;
}
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
{
struct list*p=ls;
while(p && n--)
{
p=p->next ;
}
if(p==NULL)
return NULL;
return p;
}
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
{
struct list*p=ls;
while(p)
{
if(p->data==data)
return p;
p=p->next;
}
return NULL;
//没有找到数据域等于data的节点
}
int elem_pos(struct list*ls,int data) //返回数据域等于data的节点位置
{
int index=0;
struct list*p=ls;
while(p)
{
index++;
if(p->data==data)
return index;
p=p->next;
}
return -1;
//没有找到数据域等于data的节点
}
struct list*last_list(struct list*ls) //得到链表最后一个节点
{
struct list*p=ls;
while(p->next)
{
p=p->next;
}
return p;
}
int main()
{
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
delete_list(first,2);
//clear_list(first);
traverse(first);
printf("-------------\n");
printf("count=%d\n",count_list(first));
printf("%d\n",locale_list(first,3)->data);
printf("data=%d\n",last_list(first)->data);
return 0;
}
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;
//数据域
struct list *next;//指针域
};
struct list*create_list() //建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;
//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls) //循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;
//p指向它对应的下一个节点
}
}
int delete_list(struct list*ls,int n) //删除指定位置元素
{
struct list*tmp;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return -1;
//n的位置不合适
}
tmp=p->next;
p->next=p->next->next;
free(tmp);
return 0;
//删除成功
}
int count_list(struct list *ls) //返回链表元素个数
{
struct list*p=ls;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
void clear_list(struct list*ls) //清空链表,只保留首节点
{
struct list*p=ls->next;
while(p)
{
struct list*tmp=p->next;
free(p);
p=tmp;
}
ls->next=NULL;//只有首节点,那么首节点的next也应该置为NULL
}
int empty_list(struct list*ls) //返回链表是否为空
{
if(ls->next)
return 0;
else
return -1;
}
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
{
struct list*p=ls;
while(p && n--)
{
p=p->next ;
}
if(p==NULL)
return NULL;
return p;
}
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
{
struct list*p=ls;
while(p)
{
if(p->data==data)
return p;
p=p->next;
}
return NULL;
//没有找到数据域等于data的节点
}
int elem_pos(struct list*ls,int data) //返回数据域等于data的节点位置
{
int index=0;
struct list*p=ls;
while(p)
{
index++;
if(p->data==data)
return index;
p=p->next;
}
return -1;
//没有找到数据域等于data的节点
}
struct list*last_list(struct list*ls) //得到链表最后一个节点
{
struct list*p=ls;
while(p->next)
{
p=p->next;
}
return p;
}
void merge_list(struct list*ls1,struct list*ls2)
//合并两个链表,结果放入ls1中
{
//只合并链表的节点,不合并链表头
last_list(ls1)->next=ls2->next;
free(ls2);
//链表头不要了
}
void reverse(struct list*ls) //链表逆置
{
struct list*last;
struct list *pre;
struct list *cur;
struct list *next;
if(ls->next==NULL)
return ;
//只有一个首节点,不需要逆置
if(ls->next->next==NULL)
return ;
//也不需要逆置
last=ls->next;//逆置后ls->next就成了最后一个节点了
pre=ls;
//上一个节点的指针
cur=ls->next;
//当前节点的指针
next=NULL;
//下一个节点的指针
while(cur)
{
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
ls->next=pre;
last->next=NULL;
}
int main()
{
struct list*first1;
int i;
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
delete_list(first,2);
//clear_list(first);
traverse(first);
printf("-------------\n");
printf("count=%d\n",count_list(first));
printf("%d\n",locale_list(first,3)->data);
printf("data=%d\n",last_list(first)->data);
printf("---------------------------\n");
first1=create_list();
for(i=0;i<10;i++)
{
insert_list(first1,0,i);
}
merge_list(first,first1);
printf("-------------------------\n");
traverse(first);
printf("---------------------------------------\n");
reverse(first);
traverse(first);
return 0;
}
struct list*create_list()//建立一个节点
void traverse(struct list*ls)//循环遍历链表
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
int delete_list(struct list*ls,int n)
//删除指定位置元素
int count_list(struct list *ls)
//返回链表元素个数
void merge_list(struct list*ls1,struct list*ls2)
//合并两个链表,结果放入ls1中
struct list*last_list(struct list*ls)
//得到链表最后一个节点
int elem_pos(struct list*ls,int data)
//返回数据域等于data的节点位置
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
int empty_list(struct list*ls)
//返回链表是否为空
void clear_list(struct list*ls)
//清空链表,只保留首节点
void reverse(struct list*ls)
//链表逆置
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;//数据域
struct list *next;//指针域
};
struct list*create_list()
//建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls)
//循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;//p指向它对应的下一个节点
}
}
int main()
{
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
traverse(first);
return 0;
}
打印结果:
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;
//数据域
struct list *next;//指针域
};
struct list*create_list() //建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;
//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls) //循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;
//p指向它对应的下一个节点
}
}
int delete_list(struct list*ls,int n) //删除指定位置元素
{
struct list*tmp;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return -1;
//n的位置不合适
}
tmp=p->next;
p->next=p->next->next;
free(tmp);
return 0;
//删除成功
}
int count_list(struct list *ls) //返回链表元素个数
{
struct list*p=ls;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
void clear_list(struct list*ls) //清空链表,只保留首节点
{
struct list*p=ls->next;
while(p)
{
struct list*tmp=p->next;
free(p);
p=tmp;
}
ls->next=NULL;//只有首节点,那么首节点的next也应该置为NULL
}
int empty_list(struct list*ls) //返回链表是否为空
{
if(ls->next)
return 0;
else
return -1;
}
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
{
struct list*p=ls;
while(p && n--)
{
p=p->next ;
}
if(p==NULL)
return NULL;
return p;
}
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
{
struct list*p=ls;
while(p)
{
if(p->data==data)
return p;
p=p->next;
}
return NULL;
//没有找到数据域等于data的节点
}
int elem_pos(struct list*ls,int data) //返回数据域等于data的节点位置
{
int index=0;
struct list*p=ls;
while(p)
{
index++;
if(p->data==data)
return index;
p=p->next;
}
return -1;
//没有找到数据域等于data的节点
}
struct list*last_list(struct list*ls) //得到链表最后一个节点
{
struct list*p=ls;
while(p->next)
{
p=p->next;
}
return p;
}
int main()
{
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
delete_list(first,2);
//clear_list(first);
traverse(first);
printf("-------------\n");
printf("count=%d\n",count_list(first));
printf("%d\n",locale_list(first,3)->data);
printf("data=%d\n",last_list(first)->data);
return 0;
}
//单向链表的实现
#include <stdio.h>
#include <stdlib.h>
struct list
{
int data;
//数据域
struct list *next;//指针域
};
struct list*create_list() //建立一个节点
{
return calloc(sizeof(struct list),1);
}
struct list*insert_list(struct list*ls,int n,int data)//在指定位置插入元素
{
struct list*node;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return NULL;
//n的位置大于链表节点数
}
node=create_list();//新建立一个节点
node->data=data;
node->next=p->next;
p->next=node;
return node;
}
void traverse(struct list*ls) //循环遍历链表
{
struct list*p=ls;
while(p)
{
printf("%d\n",p->data);
p=p->next;
//p指向它对应的下一个节点
}
}
int delete_list(struct list*ls,int n) //删除指定位置元素
{
struct list*tmp;
struct list*p=ls;
while(p && n--)
{
p=p->next;
}
if(p==NULL)
{
return -1;
//n的位置不合适
}
tmp=p->next;
p->next=p->next->next;
free(tmp);
return 0;
//删除成功
}
int count_list(struct list *ls) //返回链表元素个数
{
struct list*p=ls;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
void clear_list(struct list*ls) //清空链表,只保留首节点
{
struct list*p=ls->next;
while(p)
{
struct list*tmp=p->next;
free(p);
p=tmp;
}
ls->next=NULL;//只有首节点,那么首节点的next也应该置为NULL
}
int empty_list(struct list*ls) //返回链表是否为空
{
if(ls->next)
return 0;
else
return -1;
}
struct list*locale_list(struct list*ls,int n)
//返回链表指定位置的节点
{
struct list*p=ls;
while(p && n--)
{
p=p->next ;
}
if(p==NULL)
return NULL;
return p;
}
struct list*elem_locale(struct list*ls,int data)
//返回数据域等于data的节点
{
struct list*p=ls;
while(p)
{
if(p->data==data)
return p;
p=p->next;
}
return NULL;
//没有找到数据域等于data的节点
}
int elem_pos(struct list*ls,int data) //返回数据域等于data的节点位置
{
int index=0;
struct list*p=ls;
while(p)
{
index++;
if(p->data==data)
return index;
p=p->next;
}
return -1;
//没有找到数据域等于data的节点
}
struct list*last_list(struct list*ls) //得到链表最后一个节点
{
struct list*p=ls;
while(p->next)
{
p=p->next;
}
return p;
}
void merge_list(struct list*ls1,struct list*ls2)
//合并两个链表,结果放入ls1中
{
//只合并链表的节点,不合并链表头
last_list(ls1)->next=ls2->next;
free(ls2);
//链表头不要了
}
void reverse(struct list*ls) //链表逆置
{
struct list*last;
struct list *pre;
struct list *cur;
struct list *next;
if(ls->next==NULL)
return ;
//只有一个首节点,不需要逆置
if(ls->next->next==NULL)
return ;
//也不需要逆置
last=ls->next;//逆置后ls->next就成了最后一个节点了
pre=ls;
//上一个节点的指针
cur=ls->next;
//当前节点的指针
next=NULL;
//下一个节点的指针
while(cur)
{
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
ls->next=pre;
last->next=NULL;
}
int main()
{
struct list*first1;
int i;
struct list* first=create_list();//在堆中间创建一个节点
struct list* second=create_list();//在堆中间创建一个节点
struct list* third=create_list();//在堆中间创建一个节点
first->next=second;
second->next=third;
third->next=NULL;//对应链表的最后一个节点,next域一定为NULL
first->data=1;
second->data=2;
third->data=3;
insert_list(first,1,10);
insert_list(first,1,20);
insert_list(first,1,30);
delete_list(first,2);
//clear_list(first);
traverse(first);
printf("-------------\n");
printf("count=%d\n",count_list(first));
printf("%d\n",locale_list(first,3)->data);
printf("data=%d\n",last_list(first)->data);
printf("---------------------------\n");
first1=create_list();
for(i=0;i<10;i++)
{
insert_list(first1,0,i);
}
merge_list(first,first1);
printf("-------------------------\n");
traverse(first);
printf("---------------------------------------\n");
reverse(first);
traverse(first);
return 0;
}
相关文章推荐
- expect命令问题1
- LA-4255 Guess (拓扑排序+构造)
- 项目检出JRE问题(Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'idweb')
- Android:AppCompat v21 - Material Design for Pre-Lollipop Devices!
- C++入门学习——虚函数表介绍
- LeetCode 13: Roman to Integer
- display.syncExec方法例子
- lintcode-Candy-412
- 字符串的逆序
- Linux tail 命令 【转载】
- 脚本绘图工具总结(转)
- 自定义Angular插件 - 网站用户引导
- java web parameter & attrubute
- Linux apt-get安装与默认路径
- opencv 学习笔记2
- 关系数据库标准语言SQL基础1续
- python连接oracle
- 14.Python标准库_数据库 (sqlite3)
- FFT结果的物理意义
- ti