Question

In: Computer Science

Show the code how to add a node to the front of a list. Draw the...

Show the code how to add a node to the front of a list.

Draw the picture also that goes with the code.

NOW

Show the code how to add a node to the end of a list.

Draw the picture also that goes with the code.

EXTRA CREDIT

Show the code how to add a node to a specific spot in a list.

Draw the picture also that goes with the code.

Solutions

Expert Solution

Linked list definition

struct Node

{

char data;

Node *next;

};

Adding the new node in front of the list

void insert(Node *Head)

{

Node *y =new Node();

cout<<" Enter the character";

cin>>y->data;

y->next=NULL;

y->next=Head;

Head=y;

}

Adding the new node at the end of the list

void insert(Node *Head)

{

Node *y =new Node();

cout<<" Enter the character";

cin>>y->data;

y->next=NULL;

Node *a= Head;

while(a->next!= Null)

a=a->next;

a->next=y;

}

Adding the new node after a specific Node

void insertAfter(Node* prev_node)

{

if (prev_node == NULL)

{

cout << "the given previous node cannot be NULL";

return;

}

Node *y =new Node();

cout<<" Enter the character";

cin>>y->data;

y->next=NULL;

y->next = prev_node->next;

prev_node->next = y;

}


Related Solutions

1 Draw a stack diagram to show how the following code is executed and write the...
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10)) 2 Draw a stack diagram to show how the following code is executed...
Show the output of the following code. Assume the node is in the usual info-link form...
Show the output of the following code. Assume the node is in the usual info-link form with info of the type int. Also, list and ptr are reference variables of the type Node. list = new Node( ); list.info = 88; ptr = new Node( ); ptr.info = 41; ptr.link = null; list.link = ptr; ptr = new Node( ); ptr.info = 99; ptr.link = list; list = ptr; ptr = new Node( ); ptr.info = 19; ptr.link = list.link;...
need to add these functions to my code: • show only important tasks • show all...
need to add these functions to my code: • show only important tasks • show all completed tasks My code in java: import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[]...
draw the vertexes of a triangle and generate a list of the vertices code is in...
draw the vertexes of a triangle and generate a list of the vertices code is in python use turtle graphics
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
c++ code: prompt user to enter a number then add loop that checks a list of...
c++ code: prompt user to enter a number then add loop that checks a list of numbers then display numbers that are higher or equal to the user's input.
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT