任务11_4 链表合并
10
丁盟
开始于 2016-06-17 15:36
0 103 284
已截止

任务尚未发布或者你没有权限查看任务内容。

任务讨论
丁盟

任务已更新

/*
*任务说明:链表合并
*姓名:陈冲
*学号:2015015234
*班级:2班
*日期:2016/6/30
*/
程序无法调试  一直没弄好

#include<stdio.h>

#include<malloc.h>
typedef struct node
{
int data;
struct node * next;
} Node;
void printList(Node * head);
Node * mergeList(Node * h1, Node * h2);
int main(void)
{
struct node a,b,c,d,x,y,z;
Node *head1,*head2;
head1=&a;
head2=&x;
a.data=15;
a.next=&b;
b.data=20;
b.next=&c;
c.data=35;
c.next=&d;
d.data=40;
d.next=NULL;
x.data=25;
x.next=&y;
y.data=30;
y.next=&z;
z.data=45;
z.next=NULL;
printList(mergeList(head1,head2));
return 0;
}
void printList(Node * head)
{
while(head!=NULL)
{
printf("%d\n",head->data);
head=head->next;
}
}
Node * mergeList(Node * h1, Node * h2)
{
Node *h3,*h4,*h5;
h4=h1;
h3=h1->next;
h5=h2;
for(;h5!=NULL;h5=h5->next)
{
for(;h1!=NULL;h1=h1->next)
{
h3=h1->next;
if((h2->data>40)&&(h3==NULL))
{
h1->next=h2;
h2->next=NULL;

}
if(h2->data>h1->data&&h2->data<h3->data )
{
h2->next=h1->next;
h1->next=h2;
}
}
}
return h4;
}
/**
说明:任务11-4 
姓名:许磊 
班级:1 
学号:194 
日期:2016.6.26 
**/ 
#include<stdio.h>
typedef struct node
{
	int data;
	struct node * next;
}Node;
Node* mergeList(Node * h1, Node * h2);
int main(void)
{
	Node *head1,*head2,*head3,a,b,c,d,e,f,g;
	head1=&a;
	head2=&b;
	a.data=15;
	b.data=20;
	c.data=35;
	d.data=40;
	a.next=&b;
	b.next=&c;
	c.next=&d;
	d.next=NULL;
	e.data=25;
	f.data=30;
	g.data=45;
	e.next=&f;
	f.next=&g;
	g.next=NULL;
	head3=mergeList(head1,head2);
	while(head3!=NULL)
	{
		printf("%d   ",head3->data);
		head3=head3->next;
	}
	return 0;
}
Node* mergeList(Node * h1, Node * h2)
{
	Node *p=h1,*q=h2,*m;
	for(;q!=NULL;q=q->next)
	{
		for(p=h1;p!=NULL;p=p->next)
		{
			m=p->next;
		/*	if(q->data<p->data)
			{
				q->next=p;
				h1=q;
			}*/
			 if((q->data>40)&&(p->next==NULL))
			{
				q->next=NULL;
				q=p->next;
			}
			 if((q->data>p->data)&&(q->data<m->data))
			{
				q->next=p->next;
				p->next=q;
			}
		}
	}
	return h1;

姬娅宁
/*
说明:任务11_4 链表合并
姓名:姬娅宁
学号:2015015389
班级:5班
日期:2016/06/22
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node * next;
} Node;
void pushBackList(Node ** list, int data);
void printList(Node * head);
Node * mergeList(Node * h1, Node * h2);
int main(void)
{
    Node * head1 = NULL;
    Node * head2 = NULL;
    Node * n = NULL;
    pushBackList(&head1, 15);
    pushBackList(&head1, 20);
    pushBackList(&head1, 35);
    pushBackList(&head1, 40);

    pushBackList(&head2, 25);
    pushBackList(&head2, 30);
    pushBackList(&head2, 45);
    printList(head1);
    printList(head2);

    n = mergeList(head1, head2);
    printList(n);

    return 0;
}

Node * mergeList(Node * h1, Node * h2)
{
    Node * temp = NULL;
    Node * newNode = NULL;
    Node * head = NULL;
    int n=0;
    while(h1->next != NULL || h2->next != NULL)
    {
        newNode = (Node *)malloc(sizeof(Node));
        newNode->next = NULL;
        if(h1->data < h2->data)
        {
            newNode->data = h1->data;
            h1 = h1->next;
        }
        else
        {
            newNode->data = h2->data;
            h2 = h2->next;
        }
        if(n==0)
        {
            head = newNode;
            temp = head;
            n++;
        }
        else
        {
            temp->next = newNode;
            temp = temp->next;
        }
    }
    temp = head;
    while(temp->next!=NULL)
        temp = temp->next;

    if(h1==NULL)
    {
        while(h2!=NULL)
        {
            newNode = (Node *)malloc(sizeof(Node));
            newNode->data = h2->data;
            h2 = h2->next;
            newNode->next = NULL;
            temp->next = newNode;
            temp = temp->next;
        }
    }
    else
    {
        while(h1!=NULL)
        {
            newNode = (Node *)malloc(sizeof(Node));
            newNode->data = h1->data;
            h1 = h1->next;
            newNode->next = NULL;
            temp->next = newNode;
            temp = temp->next;
        }
    }

    return head;
}

void printList(Node * head)
{
    Node * temp = head;
    for(; temp != NULL; temp=temp->next)
        printf("%d ", temp->data);
    printf("\n");
}


void pushBackList(Node ** list, int data)
{
    Node * head = *list;
    Node * newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    if(*list == NULL)
        *list = newNode;
    else
    {
        while(head ->next != NULL)
            head = head->next;
        head->next = newNode;
    }
}

丁盟

任务已更新

丁盟

任务已更新