数据结构中对链表的创建、排序、插入、删除、反转、遍历

# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <stdbool.h>

typedef struct node
{
int data;
struct node *pNext;
}Node,*pNode;

pNode creat_list ();
void travese_list (); 
bool is_empty (pNode );
int length_empty (pNode );
void sort_list (pNode,int );
bool insert_list (pNode );
bool delete_list (pNode ); 
bool inversion_list (pNode );
 
int main ()
{
pNode head = NULL;
int len;

head = creat_list ();         //创建一个非循环单链表 
travese_list (head);         //遍历此单链表 

if (is_empty(head))
  printf ("链表为空!\n");
else
  printf ("链表不为空!\n");
  
len=length_empty (head);      //计算链表的长度 
sort_list (head,len);                //对链表中的元素进行排序 
travese_list (head);              //输出排序后的链表 
insert_list (head);               //插入一个元素 
travese_list (head);           //输出插入后的新链表 
delete_list(head);            //删除链表中的某个数据
travese_list (head);        //输出删除后的新链表
inversion_list (head);
travese_list (head);

return 0;
} 
//创建一个链表(带头节点的尾插法) 
pNode creat_list ()
{
int len;                    //节点个数 
int i;                      //循环变量 
int val;                  //用来保存数据域的数据
 
pNode head=(pNode)malloc(sizeof(Node));

pNode Tail = head;               //定义一个尾结点指向头结点 
Tail->pNext = NULL;           //尾结点的指针域置空 

if (head == NULL)
{
printf ("链表创建失败!");
exit (-1);
}
else
{
printf ("您想创建几个节点?");
scanf ("%d",&len);

for (i=0;i<len;i++)
{
   printf ("请输入第%d个节点的值:",i+1);
   scanf ("%d",&val);
   
   pNode pNew = (pNode)malloc(sizeof(Node));    //创建一个新的临时结点 
   
   pNew->data = val;                           //给新节点的数据域赋值 
   Tail->pNext = pNew;                       //将信结点挂在前一个结点的后面 
   pNew->pNext = NULL;                    //新节点的数据域置空 
   Tail = pNew;                                    //尾结点指向新节点 
} 
}
return head;                                             //返回头指针 
}
//遍历此链表 
void travese_list (pNode head)
{
pNode p = head->pNext;                             //将头结点的指针域赋给p 

while (p!=NULL)                                         //判断指针域是否为空 
{
printf ("%d ",p->data);                     //输出数据域中的数据 
p=p->pNext;                                  //指针向后移动一个位置 
}
printf ("\n");
return;
}
//判断链表是否为空 
bool is_empty (pNode head)
{
if (head->pNext == NULL)
  return true;
else
  return false;
}
//计算链表的长度 
int length_empty (pNode head)
{
int cnt=0;
pNode p = head->pNext;         //p指向首节点 

while (p!=NULL)                      //判断p的指向是否为空 
  {
    cnt++;
    p=p->pNext;
  }
  
printf ("这个链表的长度为:%d\n",cnt);
return cnt;
}
//对链表的元素进行排序 
void sort_list (pNode head,int len)
{
pNode p,q;     
pNode temp;  //int k                                   
int i,j,t;
printf ("链表排序如下:\n");

for (i=0,p=head->pNext;i<len-1;i++,p=p->pNext)     //p相当于数组中的a[i] 
{
temp=p;                                       //将下标保存下来,相当于k=i; 
 for (j=i+1,q=p->pNext;j<len;j++,q=q->pNext) //q相当于数组中的a[j] 
      if (temp->data > q->data)                  //相当于a[k]>a[j] 
        temp=q;                                    //相当于k=j
    if (temp!=p)                                  //相当于k!=i;
    {
    	t=temp->data;                           //相当于t=a[k]
    	temp->data = p->data;                   //相当于a[k]=a[i]
    	p->data = t;                            //相当于a[i]=t
}
}
return;	    
}
//在链表中插入一个数据 
bool insert_list (pNode head)
{
int i=0,pos,val;
pNode p=head,q;

printf ("您想在第几个位置插入数据:");
scanf ("%d",&pos);

printf ("您想插入的数字是:");
scanf ("%d",&val);

while (p!=NULL && i<pos-1)
{
p=p->pNext;
i++;
}

if (i>pos-1 || p==NULL)          //判断要插入的数据的位置是否超过链表的长度+1 
  return false;
  
pNode pNew = (pNode)malloc(sizeof(Node));   //创建一个新的节点 
 
if (pNew == NULL)
{
printf ("节点创建失败!");
exit (-1);
}

pNew->data = val;             //将要插入的数据存在新节点的数据域 
q = p->pNext;                   //将要插入节点的数据域先保存到q里面 
p->pNext = pNew;            //让p指向新节点 
pNew->pNext = q;           //新节点的指向p后面的节点q 

return true;
}
//删除链表中的某个数据 
bool delete_list(pNode head)
{
int i=0,pos;
pNode p=head,q;

printf ("您想在第几个位置删除数据:");
scanf ("%d",&pos);

while (p->pNext!=NULL && i<pos-1)
{
p=p->pNext;
i++;
}

if (i>pos-1 || p->pNext==NULL)          //判断要删除的数据的位置是否超过链表的长度+1 
  {
   printf ("删除失败!");
   return false;
  }
  
q=p->pNext;                      //将要删除的节点保存起来 
p->pNext=q->pNext;              //指向后一个节点 
free(q);                       //释放掉要删除节点的内存 
q = NULL;
return true;
} 
//翻转链表 
bool inversion_list (pNode head)
{
pNode tmp = NULL;                   //定义一个临时节点 
    pNode p = NULL;                    //定义p为头结点 
    
    printf ("翻转后的链表是:\n");
    
    if (head == NULL)                //判断链表是否为空 
    {
        return false;
    }
    
    tmp = head->pNext;                   //tmp指向第一个节点 
    
    while (tmp->pNext != NULL)          //判断临时节点是否为空 
    {
        p = tmp->pNext;                       //p指向第二个节点 
        tmp->pNext = p->pNext;         //第一个节点指向第三个节点 
        p->pNext = head->pNext;        //第二个节点指向第一个节点 
        head->pNext = p;                    //头结点的指针域指向p 
    }
    return true;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值