您的位置:首页 > 其它

链表基本操作复习

2016-03-30 12:08 295 查看

链表在数据结构里是种非常重要的,在这里我复习了下链表的基本操作

List init_list();//初始化链表

List list_insert(List list,int key);//向链表插入节点,尾插法

List list_insertBypos(List list,int pos,int key);//向链表指定位置插入结点

void print_list(List list);//打印链表

int cal_list_length(List list);//计算链表长度

node *find_node(List list,int pos);//寻找指定位置结点

List dele_node(List list,int pos);//删除指定位置结点

List sort_list(List list);//对链表排序

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//定义结点
typedef struct NODE
{
int key;
struct NODE * next;
} node,*List;

List init_list();//初始化链表

List list_insert(List list,int key);//向链表插入节点,尾插法

List list_insertBypos(List list,int pos,int key);//向链表指定位置插入结点

void print_list(List list);//打印链表

int cal_list_length(List list);//计算链表长度

node *find_node(List list,int pos);//寻找指定位置结点

List dele_node(List list,int pos);//删除指定位置结点

List sort_list(List list);//对链表排序

List init_list()
{
List list=(List)malloc(sizeof(node));
list->next=NULL;
return list;
}

List list_insert(List list,int key)
{
node *temp;
temp=list;
node *tail=(node *)malloc(sizeof(node));
while(temp->next)
{
temp=temp->next;
}
tail=temp;

node *p=(node *)malloc(sizeof(node));
p->key=key;
p->next=NULL;
tail->next=p;
tail=p;
return list;
}

int cal_list_length(List list)
{
node *p=list->next;
int length=0;
while(p)
{
p=p->next;
length++;
}
return length;
}

void print_list(List list)
{
node *p=list->next;
while(p)
{
printf("%d  ",p->key);
p=p->next;
}
}

List list_insertBypos(List list,int pos,int key)
{
int i=0,list_length=0;
node * p,* temp;//p指向要插入位置之前,temp指向要插入位置之后的链表
p=list->next;
list_length=cal_list_length(list);
if(pos<0||pos>list_length)
{
printf("插入位置不合法");
return list;
}
while(i<pos-1)
{
p=p->next;//将p指向要插入位置之前
i++;
}
node *q=(node *)malloc(sizeof(node));
q->key=key;
temp=p->next;
p->next=q;
q->next=temp;
return list;
}

node *find_node(List list,int pos)
{
int count=0;
node *temp;
temp=list->next;
while(count<pos)
{
temp=temp->next;
count++;
}
return temp;
}

List dele_node(List list,int pos)
{
List l=list;
node *pre=find_node(l,pos-1);//找到待删除结点的前驱
node* temp;
temp=pre->next;
pre->next=pre->next->next;
free(temp);
return list;
}
List sort_list(List list)//采用最简答的排序,即将当前结点逐个和后面的结点比较大小
{
int i=0,j=0;
node* link=list->next;
node* temp,* pointer;
int length=cal_list_length(list);
for( i=0;i<length;i++,link=link->next)
{
for( j=i+1,pointer=link->next;j<length;j++,pointer=pointer->next)
{
if(link->key>pointer->key)
{
temp->key=link->key;
link->key=pointer->key;
pointer->key=temp->key;
}
}
}
return list;
}
int main()
{
List list=init_list();
list=list_insert(list,1);
list=list_insert(list,12);
list=list_insert(list,6);
list=list_insert(list,8);
list=list_insert(list,4);

list=list_insertBypos(list,2,10);

//node *f_node=find_node(list,2);
//printf("找到2号位置的结点值为:%d\n",f_node->key);
int length=cal_list_length(list);
printf("链表长度为:%d\n",length);
print_list(list);
printf("\n");
printf("删除2号位置结点\n");
list=dele_node(list,2);
length=cal_list_length(list);
printf("链表长度为:%d\n",length);
print_list(list);
printf("\n");
printf("排序后链表为");
list=sort_list(list);
print_list(list);
}


I have no secret of success but hard work

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: