C language-linked list building, merging, printing

Source: Internet
Author: User
Tags prev

Problem: Two strings that are known to be stored separately with two single linked lists are sorted in ascending order.   The programming implementation merges these two single linked lists into a single linked list in descending order of data field values.   
Requirements:
1 Each node in a single linked list holds only one character
2) use the node space in the original linked list to store the merged single linked list, not to generate a new list
3. Create a function Create implementation
4) Two list merge process write one function sub implement
5) output result write one function outputs implementation
6 The main function calls these three functions to complete the program function

#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)!= ' I ') {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 ');

 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.