혁신 공장 필기시험 문제 - 두 개의 질서정연한 체인 테이블을 하나의 새로운 체인 테이블로 통합(크기가 같아도 모든 결점을 보존)

4821 단어 listnullmerge
직접 부호:
//LinkSort.cpp: 컨트롤러 프로그램의 입구점을 정의합니다.//
#include "stdafx.h"#include using namespace std;
struct Link {  int data;  struct Link *next; };
Link* createLink(int a[],int len) {  Link *head,*p,*q;  p=new Link();  p->data=a[0];  head=p;  for (int i=1;idata=a[i];   p->next=q;   p=q;  }  p->next=NULL;  return head; }
void PrintLink(Link *head) {  Link* temp=head;  while(temp!=NULL)  {   cout<data<<"";   temp=temp->next;  }  cout<next;  }  return ans; }
void Swap_Node(Link*p,Link* q) {  int temp=0;  temp=p->data;  p->data=q->data;  q->data=temp; }
Link* LinkSort(Link* head) {  Link* p,*q;  int len=getLinkLength(head);  for (int i=0;idata>p->next->data)    {     Swap_Node(p,p->next);    }    p=p->next;   }  }  return head; }  Link * list_merge(  Link *head1,  Link *head2 ) {   Link *res;
 if (head1 == NULL) return head2;  if (head2 == NULL) return head1;
 if (head1->data< head2->data)  {   res = head1;   printf("res1 %d",res->data);   res->next = list_merge( head1->next, head2);  } else {   res = head2;   printf("res2 %d",res->data);   res->next = list_merge( head1, head2->next);  }
 return res; }
 
 
int _tmain(int argc, _TCHAR* argv[]) {  //int a[]={5,4,8,9,1,2,-1};  int a[]={1,3,5,6,7,9};  int b[]={2,4,6};  int lena=sizeof(a)/sizeof(int);  int lenb=sizeof(b)/sizeof(int);    Link* aLink=createLink(a,lena);  Link* bLink=createLink(b,lenb);  Link*cLink = list_merge(aLink,bLink);    PrintLink(cLink);
 //int len =sizeof(a)/sizeof(int);  //Link* head=createLink(a,len);  //PrintLink(head);  /*head=LinkSort(head);  PrintLink(head);*/ system("pause");  return 0; }
----------------------------------------------------------------------------
// LinkSort.cpp :              。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

struct Link
{
	int data;
	struct Link *next;
};

Link* createLink(int a[],int len)
{
	Link *head,*p,*q;
	p=new Link();
	p->data=a[0];
	head=p;
	for (int i=1;i<len;i++)
	{
		q=new Link();
		q->data=a[i];
		p->next=q;
		p=q;
	}
	p->next=NULL;
	return head;
}

void PrintLink(Link *head)
{
	Link* temp=head;
	while(temp!=NULL)
	{
		cout<<temp->data<<" ";
		temp=temp->next;
	}
	cout<<endl;
}
int getLinkLength(Link* head)
{
	int ans = 0;
	while (head!=NULL)
	{
		ans++;
		head=head->next;
	}
	return ans;
}

void Swap_Node(Link*p,Link* q)
{
	int temp=0;
	temp=p->data;
	p->data=q->data;
	q->data=temp;
}

Link* LinkSort(Link* head)
{
	Link* p,*q;
	int len=getLinkLength(head);
	for (int i=0;i<len-1;i++)
	{
		p=head;
		for (int j=0;j<len-i-1;j++)
		{
			if (p->data>p->next->data)
			{
				Swap_Node(p,p->next);
			}
			p=p->next;
		}
	}
	return head;
}
 Link * list_merge(  Link *head1,  Link *head2 )
{
	 Link *res;

	if (head1 == NULL) return head2;
	if (head2 == NULL) return head1;

	if (head1->data< head2->data)
	{
		res = head1;
		printf("res1 %d
",res->data); res->next = list_merge( head1->next, head2); } else { res = head2; printf("res2 %d
",res->data); res->next = list_merge( head1, head2->next); } return res; } int _tmain(int argc, _TCHAR* argv[]) { //int a[]={5,4,8,9,1,2,-1}; int a[]={1,3,5,6,7,9}; int b[]={2,4,6}; int lena=sizeof(a)/sizeof(int); int lenb=sizeof(b)/sizeof(int); Link* aLink=createLink(a,lena); Link* bLink=createLink(b,lenb); Link*cLink = list_merge(aLink,bLink); PrintLink(cLink); //int len =sizeof(a)/sizeof(int); //Link* head=createLink(a,len); //PrintLink(head); /*head=LinkSort(head); PrintLink(head);*/ system("pause"); return 0; }

좋은 웹페이지 즐겨찾기