链表操作:创建,插入,排序,反转

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

typedef struct LIST_NODE
{
 int data;
 struct LIST_NODE* next;
}list_node,*plist_node;

/************************************************************************/
/* 随机生成node的数据。按序插入                                      */
/************************************************************************/
plist_node creat_list_sort()
{
 plist_node head = (plist_node)malloc(sizeof(list_node));
 head->next = NULL;
 plist_node ntemp;
 int cycle = 1,ndata = 0;
 plist_node curp = head;
 plist_node prep;
 while (cycle<20)
 {
//   printf("please input the data for the node: ");
//   scanf("%d",&ndata);
//   if (ndata==-1)
//   {
//    break;
//   }
  ndata = rand()%100;
  ntemp = (plist_node)malloc(sizeof(list_node));
  ntemp->data = ndata;
  if (head->next==NULL)
  {
   head->next = ntemp;
   curp = ntemp;
   curp->next = NULL;
  }
  else
  {
   prep = head;
   curp = head->next;
   while (curp&&curp->data<ndata)
   {
    prep=curp;
    curp=curp->next;
   }

   prep->next = ntemp;
   ntemp->next = curp;
  }
  cycle++;
 }

 return head;
}

/************************************************************************/
/* 随机生成node的数据。不按序插入                                       */
/************************************************************************/
plist_node create_list()
{
 plist_node head = (plist_node)malloc(sizeof(list_node));
 head->next = NULL;
 int cycle = 0,ndata = 0;
 plist_node curp = head;
 while (cycle<10)
 {
  //printf("please input the data for the node: ");
  //scanf("%d",&ndata);
  ndata = rand()%100;
  if (ndata == -1)
  {
   break;
  }
  else
  {
   plist_node ntemp = (plist_node)malloc(sizeof(list_node));
   ntemp->data = ndata;
   curp->next = ntemp;
   curp = ntemp;
  }
  cycle++;
 }
 curp->next = NULL;
 return head;
}

/************************************************************************/
/* 向链路顺序插入数据节点                                       */
/************************************************************************/
plist_node list_insert(plist_node head,plist_node node)
{
 plist_node prev = head;
 plist_node curp = head->next;
 if (curp==NULL)
 {
  head->next = node;
  node->next = NULL;
  return head;
 }
 while (curp)
 {
  if (curp->data<=node->data)
  {
   prev=curp;
   curp = curp->next;
  }
  else
   break;
 }

 if (curp == NULL)
 {
  prev->next = node;
  node->next = NULL;
 }
 else
 {
  prev->next = node;
  node->next = curp;
 }

 return head;
}
/************************************************************************/
/* 链排序                                       */
/************************************************************************/
plist_node list_sort(plist_node head)
{
 plist_node prear = head->next;
 plist_node curp,nextp;
 if (!prear->next)
 {
  printf("the member of the list must be more than 1");
  return head;
 }
 curp = prear->next;
 prear->next = NULL;
 
 while (curp)
 {
  nextp = curp->next;
  list_insert(head,curp);
  curp= nextp;
 }
 //list_insert(head,curp);

 return head;
}
/************************************************************************/
/* 链反转                                   */
/************************************************************************/
plist_node list_reverse(plist_node head)
{
 plist_node prev = head->next;
 plist_node curp = prev->next;
 prev->next = NULL;
 plist_node pnext = curp->next;
 while (pnext)
  {
   plist_node nextl = pnext->next;
   pnext->next = curp;
   curp->next = prev;
   prev = curp;
   curp = pnext;
   pnext = nextl;
  }
  head->next = curp;
 return head;
}

void list_printf(plist_node head)
{
 plist_node curp = head->next;
 while (curp)
 {
  printf("%d ",curp->data);
  curp = curp->next;
 }
 printf("\n");
}

void main()
{
 plist_node head;
 head = create_list();
 list_printf(head);  
 head = list_sort(head);
 list_printf(head);
 head = list_reverse(head);
 list_printf(head);

 head = creat_list_sort();
 list_printf(head);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值