Please someone helps me with this question I must submit the answer tomorrow. The program is not working properly as is suppose to ; I am not getting the result shown below.


Write a program that accepts numbers from the user and stores them in a linked list called allnum until a negative number is entered, then after the input has been terminated print the elements stored in the linked list . then without allocation of new memory place all memory place all even numbers in allnum in the linked list even ,and all the odd numbers in the linked list odd. Finally print the two linked list with proper heading.

Example :

Enter the integer values, - to stop: 2 5 8 3 9 6 1 7 4 -9

The result will be:

The numbers in the linked list are: 2 5 8 3 9 6 1 7 4
The numbers in the even linked list are: 2 8 6 4
The numbers in the odd linked list are: 5 3 9 1 7

This is my program

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

struct list *make_node(int );
      
struct list *insert(struct list *,struct list *);

void prt_even_odd(struct list *);

struct list
{
       int num;
       struct list *next;
};

main()
{
	struct numbers *start, *ptr, *head=NULL;
      

      int count=0,input;
      
      
      
      printf("Enter a integer.\n");
      
      while (scanf("%d",&input));
      {                     
            ptr=make_node(input);
            if (count==0)
            {
                start=ptr;
                count++;
            }
            
            head=insert(head,ptr);
      }
      
            
}
      
      
struct list *make_node(int numb)
{
       struct list *pt;
       
       pt=(struct list *)malloc( sizeof(struct list) );
       
       if (pt==NULL)
       {
           printf("No more memory.\n");
           exit(0);
       }
       pt->num=numb;
            
       return pt;
}
       
struct list *insert(struct list *start,struct list *pt)
{ 
       struct list *a=start;
       
       pt->next=start;
	   start=pt;

       return start;
}

void prt_even_odd(struct list *begin)
{
     struct list *pt=begin, *temp;
     
     
     while (pt!=NULL)
     {
           printf("%d ",pt->num);
     
           temp=pt->next;
           pt=temp;
     }
return ;

}

Recommended Answers

All 8 Replies

You have never called this function
void prt_even_odd(struct list *begin)
Declare perhaps another variable to keep track of odd and even

A couple of things I notice right off is that you haven't checked for the negative input termination and when you are adding elements to your linked list you are adding them to the head when the output you are going for has the elements added to the tail. I don't want to go into to much detail as I don't want to do your homework for you however I will suggest that you use extra pointers to help with the even and odd portion. I hope this helps some. Good luck!

sorry I forgot to remove this function
void prt_even_odd(struct list *begin)
it was only for my own checking

A couple of things I notice right off is that you haven't checked for the negative input termination and when you are adding elements to your linked list you are adding them to the head when the output you are going for has the elements added to the tail. I don't want to go into to much detail as I don't want to do your homework for you however I will suggest that you use extra pointers to help with the even and odd portion. I hope this helps some. Good luck!

sorry I forgot to remove this function
void prt_even_odd(struct list *begin)
it was only for my own checking

The problem I don't have much time, so I can realy use the extra help. THANK YOU

Cut those brances of a tree on which you are hanging.

commented: what? are you STILL HERE? -2

general logic is fine, maybe there might be a few tweaks, but you never seperated odd and even terms. Include those in main()

In prt_even_odd can you not do away with the extra variable ?

he wanted to delete prt_even_odd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.