Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
1. (a) Creating a dynamic linked list that is sorted online: Write a program that allows the user to enter a set of non-negative integers one by one via the keyboard (until the user enters a negative integer) and then ‘dynamically create a linked list’ of these integers which is sorted (in ascending order) ‘online’. By asking to ‘dynamically’ create a linked list, we mean that the memory/space for each new element of the list is allotted during runtime as and when the user inputs that element. Moreover, by asking to sort the list ‘online’, we mean that each new element given as input by the user is placed at an appropriate place in the list so that the list thus obtained is sorted. Hence, the list is always in a sorted state.

(b) Once the above list has been created, the program shall display a menu with the following options (these options are to be implemented using separate functions):-
i. Add a new element to the list such that the resultant list is again in the sorted state.
ii. Delete a specific element such that the resultant list is again in the sorted state.
iii. Search for an element in the list.
iv. Display the list.
v. Exit.
THIS IS THE PROGRAM



C++
#include<stdio.h>
#include<stdlib.h>
typedef struct tag{
int a;
struct tag *next;
}data;
data *addele(data *head);
data *subele(data *head);
void search(data *head);
void display(data *head);
int main()
{
    int tempo;



   data *head,*temp,*prev,*next; head->next=NULL; int count=0;  head=(data*) malloc(sizeof(data));
   printf("enter the number you want to add in the list followed by a negative number to end this list \n");  scanf("%d",&tempo);
   do
   {  if(count==0)
      {
        scanf("%d",&tempo);
       if(tempo>0){ head->a=tempo;count++;} else break;}
    else scanf("%d",&temp);
    if(tempo<head->a&&tempo>0) {
        temp->next=head; head=temp;}
        else
          {

           for(prev=head,next=prev->next;next!=NULL;prev=next,next=next->next)
        { temp=(data*) malloc(sizeof(data));
            if(tempo>prev->a&&tempo<next->a)
            { temp->a=tempo;
                prev->next=temp; temp->next=next;
            }
            if(next->next=NULL&&next->a<tempo)
            {
                temp->a=tempo;
                next->next=temp; temp->next=NULL;
            }

        }
   }}while(temp>0);


while(1)
{







       printf("please enter any of the options by choosing from the menu:\n");
    printf("  1. Add a new element to the list such that the resultant list is again in the sorted state\n 2: Delete a specific element such that the resultant list is again in the sorted state\n 3. Search for an element in the list\n 4. Display the list.\n 5. Exit.");
   int tempor;
   scanf("%d",&tempor);
    switch(tempor)
{
case 1:
    {
        head=addele(head);


        break;
    }
case 2:
    {
        head=subele(head);
        break;

    }
case 3:
    {
        search(head);
        break;


    }

case 4:
    {
        display(head);
        break;


    }

case 5:
    {
        exit(1);
        break;
    }
}}
 return 0;
}
  data *addele(data *head)
  { data *temp,*prev,*next;
      int tempo;
      printf("enter the number you want to add:\n");
      scanf("%d",&temp);
    if(tempo<head->a&&tempo>0) {
        temp->next=head; head=temp;}
        else
          {

           for(prev=head,next=prev->next;next!=NULL;prev=next,next=next->next)
        { temp=(data*) malloc(sizeof(data));
            if(tempo>prev->a&&tempo<next->a)
            { temp->a=tempo;
                prev->next=temp; temp->next=next;
            }
            if(next->next=NULL&&next->a<tempo)
            {
                temp->a=tempo;
                next->next=temp; temp->next=NULL;
            }

        }
      }
      return head;

  }
  data *subele(data *head)
  {   int tempo; data *temp,*prev,*next;
      printf("enter the number which you want to subtract:\n");
      scanf("%d",&temp->a); temp=head;
      for(prev=head,prev->next=next;next->next!=NULL;prev=next,next->next=NULL)
      {
          if(temp->a==next->a)
          {
              prev->next=next->next;
              free(next);
          }
      }
      return head;
  }
  void search(data *head)
  {
      int tempo; data *temp;
      printf("enter the integer you want to search :\n");
      scanf("%d",&tempo);
     for(temp=head;temp->next!=NULL;temp=temp->next)
      {
          if(temp->a==tempo)
            printf("number found\n");
      }

  }
  void display(data *head)
  {
      data *temp;
      for(temp=head;temp->next!=NULL;temp=temp->next)
        printf("%d\n",temp->a);
      return;
  }


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 8-Apr-15 1:10am
v2
Comments
OriginalGriff 8-Apr-15 7:10am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
CHill60 8-Apr-15 7:11am    
What error?
Member 11324568 8-Apr-15 8:50am    
runtime error...
CHill60 8-Apr-15 9:26am    
There are several potential "runtime errors" - what is the error message?
Member 11324568 8-Apr-15 11:57am    
@chill60 codeblocks debugger.exe has stopped working this is the message..so I pasted the code in ideone and it displayed barely "runtime error"

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

So if it does something, but it's not what you wanted, then try debugging it. Set a breakpoint just before the code that doesn't work then single step your code, looking at each line before it executes, and working out what you expect it to do. Did it do that? if so, move on. If not, why not? What did it do? Why?
This is a skill, and a very useful one in the real world - so a trivial code exercise like this is a perfect place to learn and develop it.
 
Share this answer
 
Comments
Member 11324568 8-Apr-15 8:46am    
First of all this is not a homework..I wanted you to ask you where the runtime error comes from..I mean is it due to the breaks or due to use of more pointer variables ? If you explain me then i will avoid doing that, sir. thanks :) I am not an expert and this is my first program on linked list..
OriginalGriff 8-Apr-15 10:21am    
Not homework...yeah, right.
So you have a job where they pay you to write the solution to homework problems?
Member 11324568 8-Apr-15 8:49am    
This program is logically complete and i don't understand where the error is coming from ?
OriginalGriff 8-Apr-15 10:21am    
So use the debugger, and start finding out...
Member 11324568 8-Apr-15 12:02pm    
Sir this is my 3 rd month of c...and I have many other subjects too...so I don't have the time to only concentrate on programming for now...and I'm 18 now and I'm not doing any job of solving any homework problems ...we won't be given any homework and this problem I have taken from our campus web site..which was under part problems for practice ....this is not homework ..I'm here to learn programming and you have helped me many times now....and you suggested Me to use visual studio few months ago....I'm not an expert like you sir and I don't even know the source of runtime errors yet and if I knew I would myself correct them and I'm not that dumb to post the question on the internet just coz of my laze :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900