Question

write a method which takes 3 arguments - a pointer to a linked list of integers,...

write a method which takes 3 arguments - a pointer to a linked list of integers, and two integers n and j - and inserts n after the jth. element of the list. if j is 0, n is inserted at the head of the list. If j is greater than the number of element in the list, n is inserted after the last one

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
   int a;
   struct node *next;
};
void insert(struct node **p, int n, int j)
{
   struct node *nn=(struct node*)malloc(sizeof(struct node));//creating memory for node
   nn->a=n;nn->next=NULL;//storing data
   if(j==0)
   {
       nn->next=*p;*p=nn;//new node is attached in front of original list
   }
   else
   {
       int i;struct node *t1=*p, *t2=*p;//2 pointers t1 and t2 to track positions
       for(i = 0; t1 != NULL; i++, t2 = t1, t1 = t1 -> next)//just before t1 is updated t2 takes its value
       {
           if(i == j) break;
       }
       if(t1==NULL)//if no of nodes are less than j
       {
           t2->next=nn;return;
       }
       nn->next=t1;
       t2->next=nn;
   }
}
void display(struct node **p)//displays list
{
   for(struct node *temp=*p;temp != NULL;temp=temp->next)
   {
       cout<<temp->a<<" ";
   }
   cout<<endl;
}
int main()
{
   struct node *p;
   p=(struct node *)malloc(sizeof(struct node));
   p->a=1;
   p->next=NULL;
   while(1)
   {
       cout<<"enter 0 to break, 1 to continue "<<endl;
       int ch;cin>>ch;
       if(ch==0) break;
       cout<<"enter integer and position"<<endl;
       int num, pos;cin>>num>>pos;
       insert(&p, num, pos);display(&p);
   }
   return 0;
}

Output:

enter to break, 1 to continue enter integer and position 20 2 1 enter to break, 1 to continue enter integer and position 31 2

Add a comment
Know the answer?
Add Answer to:
write a method which takes 3 arguments - a pointer to a linked list of integers,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.      Write a function called deleteNode, which accepts a pointer to a list, and a pointer...

    1.      Write a function called deleteNode, which accepts a pointer to a list, and a pointer to the node to be deleted from the list, eg a. void deleteNode(node *head, node *delNode ); 2.      Write a function called insertNode, which accepts a pointer to a list, a pointer to a new node to be inserted, and a pointer to the node after which the insertion takes place, eg a. void insertNode(node *head, node *newNode, node *prevNode );

  • Given the node structure and the head pointer (headptr) of a linked list write a code...

    Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node {    int value;    struct node *next; };

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • Write a method max() that takes a reference to the first node in a linked list...

    Write a method max() that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty. In java

  • 1. A linked list does not need to have a pointer that points to the first...

    1. A linked list does not need to have a pointer that points to the first node of the list. True or False? 2. A linked list needs to have a pointer to point to the last node of the list.  True or False? 3. If "head" is the only pointer that points to the first node of a linked list, to traverse the list, you should move the head pointer to each node one at a time.  True or False? Please...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT