C语言-链表建立、合并、打印

  问题:已知用两个单链表分别存储的两个字符串,均按递增次序排列。编程实现将这两个单链表归并为一个按数据域值递减次序排列的单链表。  
  要求:
   1) 单链表中每个结点只存放一个字符  
  2) 利用原链表中的结点空间存储归并后的单链表,不另外生成新链表   
  3) 单链表的建立写一函数create实现   
  4) 两个链表归并过程写一函数sub实现
  5) 输出结果写一函数output实现
  6) 主函数调用这三个函数完成程序功能

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct str)
struct str
{
    char s;
    struct str *next;

};

typedef struct str STR;

STR *creat(char *string)
{
    STR *head=NULL;
    STR *prev,*current;
    char *p=string;
    while(*(string)!='\0')
    {
        current=(STR *)malloc(LEN);

        if(head==NULL)
            head=current;
        else
            prev->next=current;

        current->s=*(string);
        string++;

        current->next=NULL;
        prev=current;
    }
    return head;
}


void output(STR *head)
{   
    STR * p=head;
    printf("the string is:\n");
    if(head!=NULL)
        do
        {
            printf("%c",p->s);
            p=p->next;
        }while(p!=NULL);


}

void sub(STR *a,STR *b)
{
    STR *p1,*p2,*q;
    char temp;
    p1=a;p2=b;

    while(p1->next)
    {
        p1=p1->next;
    }
    p1->next=p2;

    p1=a;
    while(p1->next)
    {   
        q=p1->next;
        while(q)
        {
            if(q->s<p1->s)
            {
                temp=q->s;
                q->s=p1->s;
                p1->s=temp;
            }

            q=q->next;      
        }
        p1=p1->next;
    }


}
int main()
{  

    char a[]="I am a student!";
    char b[]="where are you form?";
    STR  *p1,*p2;

    p1=creat(a);
    p2=creat(b);

    sub(p1,p2);

    output(p1);
    putchar('\n');
    output(p2);
    putchar('\n');
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言建立两个递增有序单向链表,并将它们合并为一个递增有序链表的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; struct Node* next; }; // 创建新节点 struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; } // 合并两个有序链表 struct Node* mergeLists(struct Node* list1, struct Node* list2) { // 创建一个新的头节点 struct Node* head = createNode(0); struct Node* current = head; // 比较两个链表的节点值,将较小的节点插入到新链表中 while (list1 != NULL && list2 != NULL) { if (list1->data <= list2->data) { current->next = list1; list1 = list1->next; } else { current->next = list2; list2 = list2->next; } current = current->next; } // 将剩余的节点连接到新链表的末尾 if (list1 != NULL) { current->next = list1; } else { current->next = list2; } // 返回新链表的头节点 return head->next; } // 打印链表 void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { // 创建第一个有序链表 struct Node* list1 = createNode(1); list1->next = createNode(3); list1->next->next = createNode(5); // 创建第二个有序链表 struct Node* list2 = createNode(2); list2->next = createNode(4); list2->next->next = createNode(6); // 合并两个有序链表 struct Node* mergedList = mergeLists(list1, list2); // 打印合并后的链表 printList(mergedList); return 0; } ``` 输出结果为:1 2 3 4 5 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值