0

下面是我在c中的单链表代码。谁能帮我这个?

这是我的主要 c 文件:

#include <stdio.h>
#include <stdlib.h>
#include "myclib.c"


struct mydata
{
    int num;
    char name;
    struct mydata *next;
};

int main()
{
    struct mydata *head, *newnode, *temp;

    head = (struct mydata*)malloc(sizeof(struct mydata));
    newnode = (struct mydata*)malloc(sizeof(struct mydata));
    temp = (struct mydata*)malloc(sizeof(struct mydata));

    head -> num = 123;
    head -> name = 'k';
    head -> next = NULL;

    newnode -> num = 456;
    newnode -> name = 'd';
    newnode -> next = NULL;

    printf("before.app.head = %p\n",head);
    printf("before.app.newnode = %p\n",newnode);
    printf("before.app.head->next = %p\n",head -> next);    
    printf("before.app.newnode->next = %p\n",newnode -> next);

    head = (struct mydata*)addNodeAtHead(head, newnode, (newnode -> next));

    printf("after.app.head = %p\n",head);
    printf("after.app.newnode = %p\n",newnode);
    printf("after.app.head->next = %p\n",head -> next); 
    printf("after.app.node->next = %p\n",newnode -> next);

    temp = head;

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

    free(temp);
    free(head);

    return 0;
}

这是 myclib.c 文件:

#include <stdio.h>


    void * addNodeAtHead(void *head, void *node, void *nodenext)
    {
        printf("\nbefore.head = %p\n",head);
        printf("before.node = %p\n",node);
        printf("before.nodenext = %p\n",nodenext);
        nodenext = head;
        head = node;
        printf("after.head = %p\n",head);
        printf("after.node = %p\n",node);
        printf("after.nodenext = %p\n\n",nodenext);

        return head;

    }

我正在尝试在 head 前面添加 newnode,而不是将 head 指针更改为 newnode。

4

6 回答 6

1

当你传递(newnode -> next)给函数时addNodeAtHead。的值(newnode -> next)被复制到node函数中的变量中。您正在node使用新值更新该变量head。函数变量执行后被node销毁,与(newnode -> next). 所以(newnode -> next)保持不变。

要克服它,只需更改您的addNodeAtHead喜欢如下:

void * addNodeAtHead(void *head, void *node)
{
    printf("\nbefore.head = %p\n",head);
    printf("before.node = %p\n",node);
    ((mydata *)node)-> next = (mydata *) head;
    printf("after.head = %p\n",head);
    printf("after.node = %p\n",node);

    return node;

}

并简单地称之为:

  head = (struct mydata*)addNodeAtHead(head, newnode);

现在一切都应该没问题了。

于 2013-07-02T13:00:58.750 回答
1
#include <stdio.h>
#include <stdlib.h>
//#include "myclib.c"

struct mydata
{
    int num;
    char name;
    struct mydata *next;
};

struct mydata* addNodeAtHead(struct mydata* head, struct mydata* node)
{
#ifdef DEBUG
    printf("\nbefore.head = %p\n",head);
    printf("before.node = %p\n",node);
//  printf("before.nodenext = %p\n",nodenext);
#endif
    if(node){
        node->next = head;
        head = node;
    }
#ifdef DEBUG
    printf("after.head = %p\n",head);
    printf("after.node = %p\n",node);
//  printf("after.nodenext = %p\n\n",nodenext);
#endif

    return head;

}

int main()
{
    struct mydata *head, *newnode, *temp;

    head = (struct mydata*)malloc(sizeof(struct mydata));
    newnode = (struct mydata*)malloc(sizeof(struct mydata));
    //temp = (struct mydata*)malloc(sizeof(struct mydata));//unused and rewrite to other pointer

    head -> num = 123;
    head -> name = 'k';
    head -> next = NULL;

    newnode -> num = 456;
    newnode -> name = 'd';
    newnode -> next = NULL;

#ifdef DEBUG
    printf("before.app.head = %p\n",head);
    printf("before.app.newnode = %p\n",newnode);
    printf("before.app.head->next = %p\n",head -> next);    
    printf("before.app.newnode->next = %p\n",newnode -> next);
#endif

    head = (struct mydata*)addNodeAtHead(head, newnode);

#ifdef DEBUG
    printf("after.app.head = %p\n",head);
    printf("after.app.newnode = %p\n",newnode);
    printf("after.app.head->next = %p\n",head -> next); 
    printf("after.app.node->next = %p\n",newnode -> next);
#endif

    temp = head;

    while(temp != NULL)
    {
        printf("num : %d\n",temp -> num);
        printf("name : %c\n",temp -> name);
        temp = temp -> next;
    }
/*
    free(temp);//NULL
    free(newnode);//...
    free(head);//already changed
*/
    temp=head;
    while(temp != NULL){
        struct mydata *prev = temp;
        temp=temp->next;
        free(prev);
    }
    return 0;
}
于 2013-07-02T13:03:29.937 回答
0

您需要将next添加的节点上的指针设置为指向原始头节点。我更改了 addNodeAtHead 的签名:当您始终传递 mydata * 类型的指针时,您不应该传递 void *。我还将变量名称更改为更清楚(IMO)它们的用途

mydata * addNodeAtHead(mydata * original_head, mydata * new_node)
{
    new_node -> next = original_head;
    return new_node; // new_node is now the head of the list!
}
于 2013-07-02T13:00:34.320 回答
0
**PROGRAM ON SINGLY LINKER LIST ( CREATION AND TRAVERSING WITH COMMENTS )**
#include<stdio.h> //Header file for input and output operations.
#include<conio.h>  
struct node   // structure declaration .
{
int info;            // stores information.
struct node *link;   // stores the address of next link.
};
struct node *first; // used to point to the first node.
void create();      // function call |NOTE| This line is optional.
void traverse();    // function call |NOTE| This line is optional.
int main()          // main function.
{
create();      // function call for creation.
traverse();    // function call for traversing i.e nothing but display.
return 0;
}
void create()      // declaration of create function , creation of nodes.
{
char ch;       // variable to take an input from the user for decision.
struct node *ptr,*cpt; // these are used to create a node and referred to as 
//previous pointer and current pointer.
ptr=(struct node*)malloc(sizeof(struct node)); //dynamic declaration 
printf("Enter data into the first node \n");
scanf("%d",&ptr->info); // taking the input from the user.
first=ptr; //assigning the information taken from previous pointer to first for 
//future identification.
do  // loop which continuously generates and links new nodes to previous nodes.
{
   cpt=(struct node*)malloc(sizeof(struct node)); //dynamic declaration of current 
   //pointer.
   printf("Enter the data for another node \n");
   scanf("%d",&cpt->info); // getting input from the user of next node 
  // information.
   ptr->link=cpt;  // linking the previous pointer to the new pointer.
   ptr=cpt;   // transferring the previous node information to the current node 
   //i.e previous pointer to the current pointer.
   printf("Do you want to create another node <Y/N>:\n\n");
   ch=getch();  // getting the users decision.
   }
   while(ch=='y');  // checking the users decision.
   ptr->link=NULL;  // assigning the previous pointer link to null;
   }
   void traverse()  // function declaration of display or traversing.
   {
   int count=1;   // counting variable for naming the nodes.
   struct node *ptr;   // pointer variable used for traversing.
   ptr=first;  // assigning ptr to the first for starting traversing from the 
   //first node.
   printf("TRAVERSING OF A LINKED LIST \n");
   while(ptr!=NULL)  // checking whether the ptr is null or not, And if not 
   //executes the statements written in the body.
   {
    printf("The data stored in node %d is:%d \n",count,ptr->info);  //prints the 
   // node id and information present in the node.
    ptr=ptr->link;  // This statement is used to traverse to the next node.
    count++;  // count incrementation.
    } 
    }

    // f☺ll☺w ☺n instagram ♥ ---> @cyber_saviour 
于 2019-08-19T14:17:53.913 回答
0
//------ SINGLY LINKED LIST PROGRAM ( CREATION AND TRAVERSING ) -------X 
#include<stdio.h>
#include<conio.h>
struct node
{
  int info;
  struct node *link;
};
struct node *first;
int main()
{
  void create();
  void traverse();
  create();
  traverse();
  return 0;
}
void create()
{
  struct node *cpt,*ptr;
  char ch;
  ptr=(struct node*)malloc(sizeof(struct node));
  printf("enter the data \n");
  scanf("%d",&ptr->info);
  first=ptr;
  do
  {
    cpt=(struct node*)malloc(sizeof(struct node));
    printf("enter the data \n");
    scanf("%d",&cpt->info);
    ptr->link=cpt;
    ptr=cpt;
    printf("Do you want to create a new node <Y/N> :\n");
    ch=getch();
  }
  while(ch=='y');
  ptr->link=NULL;
}
void traverse()
{
  struct node *ptr;
  ptr=first;
  while(ptr!=NULL)
  {
    printf("The entered nodes are:%d \n",ptr->info);
    ptr=ptr->link;
  }
}
于 2019-08-19T13:42:46.537 回答
0
#include <stdio.h>

void addNodeAtHead(void **head, void *node){
        void *prH=*head;

        *head=node;
        node->next=prH;
}
于 2021-06-08T06:03:37.850 回答