Question

In: Computer Science

Method in C language for inserting in descending order in a single linked list. It has...

Method in C language for inserting in descending order in a single linked list. It has a head and tail variable.

Solutions

Expert Solution

#include<stdio.h>
#include<stdlib.h>
//structure for node of single linkedlist
typedef struct node
{
   int data;
   struct node* next;
}node;

//method to insert in descending order
//takes, head,tail and value as parameters
void insert_descending(node **head,node **tail,int d)
{
   struct node *n =(struct node*)malloc(sizeof(struct node));
   n->data=d;
   n->next=NULL;
   if(*head == NULL)//if initially list is empty
   {
       *head = *tail = n;
   }
   else//if it is not empty
   {
       node *temp = *head,*prev=NULL;
       while(temp!=NULL && d<temp->data)//finding position to insert
       {
           prev=temp;
           temp=temp->next;
       }
       if(prev==NULL)
       {
           n->next=*head;
           *head=n;  
       }
       else if(temp==NULL)
       {
           (*tail)->next=n;
           *tail = n;  
       }
       else
       {
           n->next= prev->next;
           prev->next   =n;
       }
   }
}
//method to print linked list
void print(node *n)
{
   while(n!=NULL){
   printf("%d ",n->data);
   n=n->next;
   }
   printf("\n");
}
int main()
{
   //testing above method
   node *head=NULL,*tail = NULL;
   insert_descending(&head,&tail,2);
   insert_descending(&head,&tail,4);
   insert_descending(&head,&tail,3);
   insert_descending(&head,&tail,1);
   //printing list
   print(head);
   return 0;
}

output:

4 3 2 1


Process exited normally.
Press any key to continue . . .


Related Solutions

9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT