【发布时间】:2014-08-29 14:08:13
【问题描述】:
编写一个函数 AlternatingSplit(),它接受一个列表并将其节点划分为两个较小的列表“a”和“b”。子列表应由原始列表中的交替元素组成。因此,如果原始列表为 0->1->0->1->0->1,则一个子列表应为 0->0->0,另一个应为 1->1->1。
有关问题的更多详细信息 - http://www.geeksforgeeks.org/alternating-split-of-a-given-singly-linked-list/
现在我编写了这段代码,它运行成功了
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
node *next;
};
node *start1 = NULL, *start2 = NULL, *start3 = NULL;
void push()
{
node *temp = (node *)malloc(sizeof(node));
printf("Enter number = ");
scanf("%d", &temp->num);
temp -> next = start1;
start1 = temp;
}
void split()
{
while(start1 != NULL)
{
node *temp1 = (node *)malloc(sizeof(node));
temp1 ->num = start1 ->num;
temp1->next = start2;
start2 = temp1;
start1 = start1 -> next;
if(start1 != NULL)
{
node *temp2 = (node *)malloc(sizeof(node));
temp2 ->num = start1 ->num;
temp2->next = start3;
start3 = temp2;
start1 = start1 -> next;
}
}
}
int main()
{
int n;
scanf("%d", &n);
while(n--)
push();
split();
node *temp = start2;
while(temp != NULL)
{
printf("%d ", temp ->num);
temp = temp ->next;
}
printf("\n");
temp = start3;
while(temp != NULL)
{
printf("%d ", temp ->num);
temp = temp ->next;
}
return 0;
}
问题提供的代码是 -
/*Program to alternatively split a linked list into two halves */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* pull off the front node of the source and put it in dest */
void MoveNode(struct node** destRef, struct node** sourceRef) ;
/* Given the source list, split its nodes into two shorter lists.
If we number the elements 0, 1, 2, ... then all the even elements
should go in the first list, and all the odd elements in the second.
The elements in the new lists may be in any order. */
void AlternatingSplit(struct node* source, struct node** aRef, struct node** bRef)
{
/* split the nodes of source to these 'a' and 'b' lists */
struct node* a = NULL;
struct node* b = NULL;
struct node* current = source;
while (current != NULL)
{
MoveNode(&a, ¤t); /* Move a node to list 'a' */
if (current != NULL)
{
MoveNode(&b, ¤t); /* Move a node to list 'b' */
}
}
*aRef = a;
*bRef = b;
}
/* Take the node from the front of the source, and move it to the front of the dest.
It is an error to call this with the source list empty.
Before calling MoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
Affter calling MoveNode():
source == {2, 3}
dest == {1, 1, 2, 3}
*/
void MoveNode(struct node** destRef, struct node** sourceRef)
{
/* the front source node */
struct node* newNode = *sourceRef;
assert(newNode != NULL);
/* Advance the source pointer */
*sourceRef = newNode->next;
/* Link the old dest off the new node */
newNode->next = *destRef;
/* Move dest to point to the new node */
*destRef = newNode;
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginging of the linked list */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Drier program to test above functions*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
struct node* a = NULL;
struct node* b = NULL;
/* Let us create a sorted linked list to test the functions
Created linked list will be 0->1->2->3->4->5 */
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
push(&head, 0);
printf("\n Original linked List: ");
printList(head);
/* Remove duplicates from linked list */
AlternatingSplit(head, &a, &b);
printf("\n Resultant Linked List 'a' ");
printList(a);
printf("\n Resultant Linked List 'b' ");
printList(b);
getchar();
return 0;
}
我的问题是,考虑到时间复杂度、空间复杂度和其他所有因素,这两个代码中哪一个更有效、更正确? 为什么? 详细的解释会更有帮助。
【问题讨论】:
-
没有一个例子检查内存错误,这是我的第一反应:)
标签: c linked-list