Dynamic allocation problem with linked lists

Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation of a data structure that consists of three linked lists connected to a single root node. Participants are examining the provided C code to identify issues related to dynamic memory allocation and linked list management.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests help with their code, specifically asking for clues about potential errors.
  • Another participant points out a specific line in the function form_root where the initialization of linked list pointers is questioned, suggesting that the pointers should not be set to zero but rather initialized to new nodes.
  • A different participant agrees with the previous comment but clarifies that the original poster (OP) is correctly initializing the pointers to NULL, even if the approach may not be optimal.
  • There is a call for the OP to provide additional information regarding compilation or runtime errors to facilitate further assistance.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of the OP's initialization method for the linked list pointers. While some agree that the initialization is not ideal, others defend the OP's approach as valid. The discussion remains unresolved as participants await more information from the OP.

Contextual Notes

There are indications of potential misunderstandings regarding the initialization of linked list nodes and the use of dynamic memory allocation. Specific assumptions about the intended functionality of the code are not fully clarified.

gruba
Messages
203
Reaction score
1

Homework Statement


I need to create data structure which contains three linked lists that are connected to one root node.

Homework Equations


-Data structures

The Attempt at a Solution


I can't find what is wrong with the following code:
Code:
#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
  struct node *next;
  int y;
}NODE;

typedef struct root
{
  struct node *first;
  struct node *middle;
  struct node *last;
  int x;
}ROOT;

ROOT *form_root(int x)
{
  ROOT *new_root=(ROOT *)malloc(sizeof(ROOT));
  new_root->first=new_root->middle=new_root->last=0;
  new_root->x=x;
  return new_root;
}

void add_in_list(NODE *list,int y)
{
  NODE *new_node=(NODE *)malloc(sizeof(NODE));
  new_node->y=y;
  new_node->next=NULL;
  if(list == 0)
  list=new_node;
  else
  return ;
}

ROOT *add(ROOT *root,int x)
{
  if(root==0)
  return form_root(x);
  if(x < root->x)
  add_in_list(root->first,x);
  else
  if(x > root->x)
  add_in_list(root->last,x);
  else
  add_in_list(root->middle,x);
  return root;
}

int erase_from_list_(NODE *node,int y)
{
  NODE *p=node->next;
  if(p==0)
  return 0;
  node->y=p->y;
  node->next=p->next;
  free(p);
  return 1;
}

ROOT *erase(ROOT *root, int x)
{
  if(root==0)
  return 0;
  else if(x < root->x)
  root->first=erase_from_list_(root->first,x);
  else if(x > root->x)
  root->last=erase_from_list_(root->last,x);
  else if(x==root->x)
  root->middle=erase_from_list_(root->middle,x);
  else
  return 0;
  return root;
}

void sort_list(NODE *head)
{
  for(;head && head->next;head=head->next)
  {
  NODE *min=head,*p;
  for(p=head->next;p;p=p->next)
  if(min->y > p->y)
  min=p;
  if(min!=head)
  {
  int temp=head->y;
  head->y=min->y;
  min->y=temp;
  }
  }
}

void sort(ROOT *root)
{
  sort_list(root->first);
  sort_list(root->middle);
  sort_list(root->last);
}

void print_list(NODE *head)
{
  while(head)
  {
  printf("%d",head->y);
  head=head->next;
  }
}

void print(ROOT *root)
{
  print_list(root->first);
  print_list(root->middle);
  print_list(root->last);
}

int main()
{
  ROOT *root=0;
  int array[][3]={{0,1,2},{3,4,5},{12,7,8}};
  int i;
  for(i=0;i<3;i++)
  root=add(root,*array[i]);
  int num;
  printf("enter the number to be removed:");
  scanf("%d",&num);
  root=erase(root,num);
  sort(root);
  NODE *head=0;
  print(head);
  return 0;
}

Could someone help?
 

Attachments

  • image.PNG
    image.PNG
    1.4 KB · Views: 545
Physics news on Phys.org
gruba said:

Homework Statement


I need to create data structure which contains three linked lists that are connected to one root node.

Homework Equations


-Data structures

The Attempt at a Solution


I can't find what is wrong with the following code:
<omitted>

Could someone help?
A clue as to what's wrong would be helpful. Does your code compile? If not, what compiler and/or linker errors are you getting (including the line number where the error occurs)? Does your code compile, but produce run-time errors? Does your code run, but produce incorrect results?

We're happy to help, but please give us some information to go on.
 
Just looking at this function:

Code:
ROOT *form_root(int x)
{
  ROOT *new_root=(ROOT *)malloc(sizeof(ROOT));
  new_root->first=new_root->middle=new_root->last=0; ********* Really look at this line.
  new_root->x=x;
  return new_root;
}

Can you see why your code is wrong? It should read like so:

Code:
ROOT *new_root = (Insert a new root here);
new_root->first = (Insert a new node here);
new_root->second = (Insert a new node here);
new_root->third = (Insert a new node here);
new_root->x = x;

The first, second and third fields are not integers you can set to zero. They are actually nodes with a next and y field.

Look at your code more.
 
Zondrina said:
Just looking at this function:

Code:
ROOT *form_root(int x)
{
  ROOT *new_root=(ROOT *)malloc(sizeof(ROOT));
  new_root->first=new_root->middle=new_root->last=0; ********* Really look at this line.
  new_root->x=x;
  return new_root;
}

Can you see why your code is wrong? It should read like so:

Code:
ROOT *new_root = (Insert a new root here);
new_root->first = (Insert a new node here);
new_root->second = (Insert a new node here);
new_root->third = (Insert a new node here);
new_root->x = x;
The OP's first line of code uses malloc() to allocate heap memory for a new node. This is the appropriate thing to do, not "insert a new root here" as you wrote.
Zondrina said:
The first, second and third fields are not integers you can set to zero. They are actually nodes with a next and y field.
Correct, they are not integers, but the OP is initializing these pointers to NULL What the OP has done is not the best way to do things, but I get what the intent is.
Zondrina said:
Look at your code more.
This is also good advice for you, @Zondrina.

I'm waiting for the OP to give us more information about what is wrong with the posted code.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K