C语言 链表的一些操作

#include "stdafx.h"
#include "malloc.h"
struct NODE
{
 char data;
    struct NODE* next;
};
typedef NODE Node;
void CreateList(Node** head)
{
 Node *temp,*newNode;
 char ch;
    (*head) = (Node*)malloc(sizeof(Node));
 temp = (*head);
 
    while(ch!='#')
 {
  scanf("%c",&ch);
  fflush(stdin);
     newNode = (Node*)malloc(sizeof(Node));
  newNode->data = ch;
        temp->next = newNode;
     temp = newNode;
 }
 temp->next = NULL;//在链表输入完毕之后,最后一个元素的next要将其置为空
}
//显示链表元素
void DisplayLinklist(Node* head)
{
  Node* tmp=NULL;
     tmp = head->next;
     while(tmp!=NULL)
 {
       printf("%c",tmp->data);
    tmp = tmp ->next;
 }
}
//插入节点
void InsertNode(Node** head,char insertData,int index)
{
    Node* tmp = (*head)->next;
 Node* insertNode = NULL;
 int count = 0;
 while(tmp!=NULL)
 {
  if(count == index)
  {
            insertNode = (Node*)malloc(sizeof(Node));
   insertNode->data = insertData;
   insertNode->next = tmp->next;
   tmp->next = insertNode;
   break;
  }
  else
  {
      tmp = tmp->next;
   count++;
  }
 }
}
//合并有序链表
void MergeLinkList(Node** La,Node** Lb,Node** Lc)
{
    Node *pa,*pb,*pc;
 pa = (*La)->next;
 pb = (*Lb)->next;
   
    (*Lc) = pc = (*La);
 while(pa&&pb)
 {  
  if(pa->data <= pb->data )
  {
   pc->next = pa;
   pc = pa;
   pa=pa->next;
  }
  else
  {
   pc->next = pb;
   pc = pb;
   pb = pb->next;
  }
 }
 pc->next = pa?pa:pb;
}
int main(int argc, char* argv[])
{
 Node* La = NULL;
 Node* Lb = NULL;
 Node* Lc = (Node*)malloc(sizeof(Node));
 CreateList(&La);
// InsertNode(&La,'9',3);
// DisplayLinklist(La);

 CreateList(&Lb);
 MergeLinkList(&La,&Lb,&Lc);
 DisplayLinklist(Lc);
 free(La);
 free(Lb);
 La = NULL;
 Lb = NULL;
 return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值