编写一个函数,它将链表作为参数,并返回在原始列表中偶数位置找到的节点

时间:2014-09-15 21:21:54

标签: c

我无法让我的程序正常工作。我根本无法打印偶数节点。我对链表很新。我得到初始正常列表工作和打印,但甚至不能让其他函数调用/工作。

#include <stdio.h>
#include <stdlib.h>

typedef struct {

int data;
 struct node* next;

}node;

struct node* head;
struct node* evenHead;

node* copyEven(node *mylist)
 {
    node* newHead = NULL;
    node* temptr = NULL;
    int count = 1;

    for(temptr = mylist;  temptr->next != NULL; temptr = temptr->next){
        if ( count % 2 == 0){
            node* newNode = malloc(sizeof(node));
            newNode->data = temptr->data;
            if(newHead == NULL)
            {
                newHead = newNode;
            }
            for (temptr = newHead; temptr->next != NULL; temptr = temptr->next);
                node *ptr = (node*)malloc(sizeof(node));
                temptr->next = ptr;
                ptr->data = temptr->data;
                ptr->next = NULL;
        }
        count++;
    }
return newHead;
}

void Insert(int x)
{
node* temp = (node*)malloc(sizeof(node));
temp->data = x;
temp->next = head;
head = temp;
 }
void print()
 {
     node* temp = head;
    printf("List is : ");

    while(temp != NULL)
    {
        printf(" %d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

void printEven()
{
    node* temp = evenHead;
    printf("even list is: ");

    while(temp != NULL)
    {
        printf(" %d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main()
{

    head = NULL;
    evenHead= NULL;

    Insert(1);
    Insert(2);
    Insert(3);
    Insert(4);

    print();

    evenHead = copyEven(head);

     printEven();

    return 0;
 }

3 个答案:

答案 0 :(得分:1)

void printEverySecondNode(node *in)
{
    while (in && in->next)
    {
        printf(" %d", in->next->data);
        in = in->next->next;
    }
    return;
}

node *copyReverseHalfList(node *in)
{
    node *out = NULL;
    node *cur = NULL;
    while (in && in->next)
    {
        cur = malloc(sizeof(node));
        cur->data = in->next->data;
        cur->next = out;
        in = in->next->next;
        out = cur;
    }
    return out;
}

答案 1 :(得分:1)

typedef struct node {
    int data;
    struct node* next;
} node;

node *copyEven(node *mylist){
    node *newHead = NULL, *temptr = NULL;
    int count = 1;

    while(mylist){
        if ( count % 2 == 0){
            node* newNode = malloc(sizeof(node));
            newNode->data = mylist->data;
            newNode->next = NULL;
            if(newHead == NULL){
                temptr = newHead = newNode;
            } else {
                temptr = temptr->next = newNode;
            }
        }
        count++;
        mylist = mylist->next;
    }
    return newHead;
}

答案 2 :(得分:0)

随着OP和我讨论解决方案,这个答案逐步发展。 OP得到了他想要的半交互式答案,但我不确定如果您正在阅读这些单词,那么存档的,非交互式的答案将对您有多大帮助。不过,这是归档的。

原始回答

开始:

typedef struct {
    int data;
    node* next;
} node;

node* head;
node* evenHead;

struct node在C中是合法的,但它不适用于typedef。如果您使用typedef,则只需输入node即可。但是,因为struct node包含指向同一类型对象的指针,所以此扩展有助于:

struct node {
    int data;
    struct node* next;
};
typedef struct node node;

node* head;
node* evenHead;

您应该稍后将代码中的任何其他struct node更改为node

通过上次更改,您的代码具有名为struct node的实际类型,以及名为node的该类型的缩写。

通过这些更改,代码至少可以编译(在我的机器上)。你的编译吗?