A linked list is a data structure made up of nodes that are connected to each other. Each node contains data and a link to the next node. Linked lists can grow and shrink dynamically as nodes are added or removed. There are several types of linked lists including single linked lists where navigation is forward only, doubly linked lists where navigation is bidirectional, and circular linked lists where the last node links back to the first node. Common operations on linked lists include appending nodes to add to the end, inserting nodes in a sorted manner, and deleting nodes by value. These operations involve allocating memory for new nodes, linking nodes together, and removing nodes by adjusting pointers.
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
* Program to insert in a sorted list */
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* function to insert a new_node in a list. Note that this
function expects a pointer to head_ref as this can modify the
head of the input linked list (similar to push())*/
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* A utility function to create a new node */
struct node *newNode(int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
/* Drier program to test count function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
struct node *new_node = newNode(5);
sortedInsert(&head, new_node);
new_node = newNode(10);
sortedInsert(&head, new_node);
new_node = newNode(7);
sortedInsert(&head, new_node);
new_node = newNode(3);
sortedInsert(&head, new_node);
new_node = newNode(1);
sortedInsert(&head, new_node);
new_node = newNode(9);
sortedInsert(&head, new_node);
printf(\"\ Created Linked List\ \");
printList(head);
return 0;
}
public class Listnode {
//*** fields ***
private Object data;
private Listnode next;
//*** methods ***
// 2 constructors
public Listnode(Object d) {
this(d, null);
}
public Listnode(Object d, Listnode n) {
data = d;
next = n;
}
// access to fields
public Object getData() {
return data;
}
public Listnode getNext() {
return next;
}
// modify fields
public void setData(Object ob) {
data = ob;
}
public void setNext(Listnode n) {
next = n;
}
}
Thsi below program print out all the varibles in SLL
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int .
- Data structures allow for efficient handling of large volumes of data through logical organization and relationships between data elements. Common linear data structures include arrays, lists, stacks, and queues, while trees and graphs are examples of non-linear data structures.
- Abstract data types (ADTs) define a set of complex data objects and operations that can be performed on those objects without specifying their implementation. Data structures provide a way to implement the logical relationships and operations defined in an ADT.
- Linked lists provide an alternative to arrays for storing data by linking each data element to the next using pointers, rather than requiring contiguous memory locations. This allows for more flexible insertion and deletion compared to arrays.
A linked list is a data structure where each node contains a data field and a reference to the next node. The head node points to the first node, and nodes are connected through next references. Linked lists allow for efficient insertions and deletions compared to arrays. Common types include singly linked, doubly linked, circular singly linked, and circular doubly linked lists. Operations on linked lists include insertion, deletion, and searching.
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
This assignment and the next (#5) involve design and development of a sequential
non contiguous and dynamic datastructure called LinkedList. A linked list object is
a container consisting of connected ListNode objects. As before, we are not going
to use pre-fabricated classes from the c++ library, but construct the LinkedList
ADT from scratch.
The first step is construction and testing of the ListNode class. A ListNode object
contains a data field and a pointer to the next ListNode object (note the recursive
definition).
#This assignment requires you to
1. Read the Assignment 4 Notes
2. Watch the Assignment 4 Support video
3. Implement the following methods of the ListNode class
-custom constructor
-setters for next pointer and data
4. Implement the insert and remove method in the main program
5. Scan the given template to find the above //TODO and implement the code
needed
//TODO in ListNodecpp.h file
public: ListNode(T idata, ListNode<T> * newNext);
public: void setNext(ListNode<T> * newNext);
public: void setData(T newData);
// TODO in main program
void remove(ListNode<int> * &front,int value)
void insert(ListNode<int> * &front,int value)
# The driver is given ListNodeMain.cpp is given to you that does the following
tests
1. Declares a pointer called front to point to a ListNode of datatype integer
2. Constructs four ListNodes with data 1,2,4 and adds them to form a linked
list.
3. Inserts ListNode with data 3 to the list
4. Removes node 1 and adds it back to test removing and adding the first
element
5. Removes node 3 to test removing a middle node
6. Removes node 4 to test removing the last node
7. Attempt to remove a non existent node
8. Remove all existing nodes to empty the list
9. Insert node 4 and then node 1 to test if insertions preserve order
10.Print the list
Main.cpp
#include <iostream>
#include "ListNodecpp.h"
// REMEMBER each ListNode has two parts : a data field
// and an address field. The address is either null or points to the next node
//in the chain
//Requires: integer value for searching, address of front
//Effects: traverses the list node chain starting from front until the end comparing search value
with listnode getData. Returns the original search value if found, if not adds +1 to indicate not
found
//Modifies: Nothing
int search(ListNode<int> * front, int value);
//Requires: integer value for inserting, address of front
//Effects: creates a new ListNode with value and inserts in proper position (increasing order)in
the chain. If chain is empty, adds to the beginning
//Modifies: front, if node is added at the beginning.
//Also changes the next pointer of the previous node to point to the
//newly inserted list node. the next pointer of the newly inserted pointer
//points to what was the next of the previous node.
//This way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter -
// Why do you think this is needed ?**********
void insert(ListNode<int> * &fr.
This C code defines structures for nodes and linked lists. It provides functions for creating nodes, inserting nodes at the beginning or end of a linked list, deleting nodes with a specific value, searching for a node, and displaying the linked list. These functions are used in the main function to demonstrate manipulating a linked list, including inserting nodes, deleting nodes, and searching.
A linked list is a data structure made up of nodes that are connected to each other. Each node contains data and a link to the next node. Linked lists can grow and shrink dynamically as nodes are added or removed. There are several types of linked lists including single linked lists where navigation is forward only, doubly linked lists where navigation is bidirectional, and circular linked lists where the last node links back to the first node. Common operations on linked lists include appending nodes to add to the end, inserting nodes in a sorted manner, and deleting nodes by value. These operations involve allocating memory for new nodes, linking nodes together, and removing nodes by adjusting pointers.
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
* Program to insert in a sorted list */
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* function to insert a new_node in a list. Note that this
function expects a pointer to head_ref as this can modify the
head of the input linked list (similar to push())*/
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* A utility function to create a new node */
struct node *newNode(int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
/* Drier program to test count function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
struct node *new_node = newNode(5);
sortedInsert(&head, new_node);
new_node = newNode(10);
sortedInsert(&head, new_node);
new_node = newNode(7);
sortedInsert(&head, new_node);
new_node = newNode(3);
sortedInsert(&head, new_node);
new_node = newNode(1);
sortedInsert(&head, new_node);
new_node = newNode(9);
sortedInsert(&head, new_node);
printf(\"\ Created Linked List\ \");
printList(head);
return 0;
}
public class Listnode {
//*** fields ***
private Object data;
private Listnode next;
//*** methods ***
// 2 constructors
public Listnode(Object d) {
this(d, null);
}
public Listnode(Object d, Listnode n) {
data = d;
next = n;
}
// access to fields
public Object getData() {
return data;
}
public Listnode getNext() {
return next;
}
// modify fields
public void setData(Object ob) {
data = ob;
}
public void setNext(Listnode n) {
next = n;
}
}
Thsi below program print out all the varibles in SLL
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int .
- Data structures allow for efficient handling of large volumes of data through logical organization and relationships between data elements. Common linear data structures include arrays, lists, stacks, and queues, while trees and graphs are examples of non-linear data structures.
- Abstract data types (ADTs) define a set of complex data objects and operations that can be performed on those objects without specifying their implementation. Data structures provide a way to implement the logical relationships and operations defined in an ADT.
- Linked lists provide an alternative to arrays for storing data by linking each data element to the next using pointers, rather than requiring contiguous memory locations. This allows for more flexible insertion and deletion compared to arrays.
A linked list is a data structure where each node contains a data field and a reference to the next node. The head node points to the first node, and nodes are connected through next references. Linked lists allow for efficient insertions and deletions compared to arrays. Common types include singly linked, doubly linked, circular singly linked, and circular doubly linked lists. Operations on linked lists include insertion, deletion, and searching.
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
This assignment and the next (#5) involve design and development of a sequential
non contiguous and dynamic datastructure called LinkedList. A linked list object is
a container consisting of connected ListNode objects. As before, we are not going
to use pre-fabricated classes from the c++ library, but construct the LinkedList
ADT from scratch.
The first step is construction and testing of the ListNode class. A ListNode object
contains a data field and a pointer to the next ListNode object (note the recursive
definition).
#This assignment requires you to
1. Read the Assignment 4 Notes
2. Watch the Assignment 4 Support video
3. Implement the following methods of the ListNode class
-custom constructor
-setters for next pointer and data
4. Implement the insert and remove method in the main program
5. Scan the given template to find the above //TODO and implement the code
needed
//TODO in ListNodecpp.h file
public: ListNode(T idata, ListNode<T> * newNext);
public: void setNext(ListNode<T> * newNext);
public: void setData(T newData);
// TODO in main program
void remove(ListNode<int> * &front,int value)
void insert(ListNode<int> * &front,int value)
# The driver is given ListNodeMain.cpp is given to you that does the following
tests
1. Declares a pointer called front to point to a ListNode of datatype integer
2. Constructs four ListNodes with data 1,2,4 and adds them to form a linked
list.
3. Inserts ListNode with data 3 to the list
4. Removes node 1 and adds it back to test removing and adding the first
element
5. Removes node 3 to test removing a middle node
6. Removes node 4 to test removing the last node
7. Attempt to remove a non existent node
8. Remove all existing nodes to empty the list
9. Insert node 4 and then node 1 to test if insertions preserve order
10.Print the list
Main.cpp
#include <iostream>
#include "ListNodecpp.h"
// REMEMBER each ListNode has two parts : a data field
// and an address field. The address is either null or points to the next node
//in the chain
//Requires: integer value for searching, address of front
//Effects: traverses the list node chain starting from front until the end comparing search value
with listnode getData. Returns the original search value if found, if not adds +1 to indicate not
found
//Modifies: Nothing
int search(ListNode<int> * front, int value);
//Requires: integer value for inserting, address of front
//Effects: creates a new ListNode with value and inserts in proper position (increasing order)in
the chain. If chain is empty, adds to the beginning
//Modifies: front, if node is added at the beginning.
//Also changes the next pointer of the previous node to point to the
//newly inserted list node. the next pointer of the newly inserted pointer
//points to what was the next of the previous node.
//This way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter -
// Why do you think this is needed ?**********
void insert(ListNode<int> * &fr.
This C code defines structures for nodes and linked lists. It provides functions for creating nodes, inserting nodes at the beginning or end of a linked list, deleting nodes with a specific value, searching for a node, and displaying the linked list. These functions are used in the main function to demonstrate manipulating a linked list, including inserting nodes, deleting nodes, and searching.
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
C++ please put everthing after you answer it, thanks
Complete the stub file for the DoublyLinkedList implementation.
Please note: Any changes to the NodeType struct, not using the NodeType struct, or changing from template Type to using a specific data type will all be cause for deductions.
You are required to implement the methods:
insertFirst
insertLast
insertNode - (insert an item at a given index)
deleteNode - (remove a specific item)
The stub file contains the basic class format to implement the virtual methods from ListType. You should only need to complete the code for these four functions. Your code must maintain (or add) the node connections both forward and backward. In the double link list class these are called next and prev.
Your code must also maintain the count variable.
\} template Type ListType : : back ( ) const \{ assert(tail != NULL); //confirm tail exists return tail->data; //return the data of the tail node \} template void ListType >: :copyList (const ListType > \& otherList) \{ NodeType *newNode; //pointer to create a node NodeType current; //pointer to traverse the list if (head ! = NULL) //if the list is nonempty, make it empty destroyList(); if (otherList. head == NULL) //otherList is empty \{ head = NULL; tail = NULL; count = 0 ; \} else \{ current = otherList. head; // current points to the count = otherList . count; // list to be copied //copy the head node head = new NodeType < Type > ; / / create the node head->data = current > data; // copy the data head->next = NULL; // set the link field of tail = head; // the node to NULL // the next node //copy the remaining list while (current != NULL) \{ newNode = new NodeType < Type > ; // create a node newNode > data = current > data; // copy the data newNode->next = NULL; // set the link of // newNode to NULL tail->next = newNode; //attach newNode after tail tail = newNode; // make tail point to //the actual tail node current = current > next; 1/ make current point //to the next node \}/ /end while \}/lend else \}//end copylist
.
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
Data Structures in C++
I am really new to C++, so links are really hard topic for me. It would be nice if you can provide
explanations of what doubly linked lists are and of some of you steps... Thank you
In this assignment, you will implement a doubly-linked list class, together with some list
operations. To make things easier, you’ll implement a list of int, rather than a template class.
Solution
A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a.
Now, if we have collection of integers, we need some representation to identify them. We call it
array. For ex: int arr[5]
This array is nothing but a Data Structure.
So, a Data Structure is a way to group the data.
There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack,
Queue, etc.
Doubly-Linked list are the ones where you can traverse from the current node both in left and
right directions.
Why so many different types of Data Structures are required ?
Answer is very simple, grouping of data, storage of data and accessing the data is different.
For example, in case of Arrays we store all the data in contiguous locations.
What if we are not able to store the data in contiguous locations because we have huge data.
Answer is go for Linked List/Doubly-Linked list.
Here we can store the data anywhere and link the data through pointers.
I will try to provide comments for the code you have given. May be this can help you.
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include
class dlist {
public:
dlist() { }
// Here we are creating a NODE, it has a integer value and two pointers.
// One pointer is to move to next node and other to go back to previous node.
struct node {
int value;
node* next;
node* prev;
};
// To return head pointer, i.e. start of the Doubly-Linked list.
node* head() const { return _head; }
// To return Tail pointer, i.e. end of the Doubly-Linked list.
node* tail() const { return _tail; }
// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head).
node* at(int index){
int cnt = 0;
struct node* tmp = head();
while(tmp!=NULL)
{
if (cnt+1 == index)
return tmp;
tmp = tmp->next;
}
}
// Insert a new value, after an existing one
void insert(node *previous, int value){
// check if the given previous is NULL
if (previous == NULL)
{
printf(\"the given previous node cannot be NULL\");
return;
}
// allocate new node
struct node* new_node =(struct node*) malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// Make next of new node as next of previous
new_node->next = previous->next;
// Make the next of previous as new_node
previous->next = new_node;
// Make previous as previous of new_node
new_node->prev = previous;
// Change previous of new_node\'s next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Delete the given node
void del(node* which){
struct node* head_ref = head();
/* base case */
if(*head_ref == NUL.
C++Write a method Node Nodereverse() which reverses a list..pdfarjunenterprises1978
C++
Write a method Node *Node::reverse() which reverses a list.
Solution
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf(\"Given linked list\ \");
printList(head);
reverse(&head);
printf(\"\ Reversed Linked list \ \");
printList(head);
getchar();
}.
Linked lists are linear data structures where each node points to the next. Each node contains a data field and a pointer to the next node. There are three types: singly, doubly, and circular linked lists. Linked lists allow for constant-time insertions and deletions and do not require fixed size allocation. Common operations on linked lists include insertion, deletion, searching, and traversal. Linked lists are useful for implementations like stacks, queues, and dynamic data structures.
This C++ code defines a linked stack data structure using classes. The Lstack class implements a linked stack with methods to push values onto the stack, pop values off the stack, and display the stack. A main function tests the stack by pushing multiple values, displaying the stack, popping a value, and displaying the modified stack.
C program to insert a node in doubly linked listSourav Gayen
This C program implements functions to create and manipulate a doubly linked list. It allows the user to choose from a menu to create a list, insert nodes at the beginning, end, or a specified position, and display the list. Functions are defined to create a list by dynamically allocating nodes and linking them together, insert nodes at different positions by updating next and prev pointers, and display the list by traversing from head to tail.
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
Write a Java Class to Implement a Generic Linked List
Your list must be implemented as a singly-linked list of generic nodes, where each Node object
has two instance variables: an object of the “type variable” class, and a pointer to the next node
on the list.
Your class will contain separate methods to handle each of the operations read from a data file
(see II., below)
Your class will also override toString() to return the objects on the list in the order in which they
occur.
Write a Test Class for Your LinkedList Class
Your main method will read list operation instructions from a data file, until end-of-file and call
the appropriate LinkedList method to execute each one.
After each operation is executed, print out the operation and the updated list.
The data file to be used is on the class web site and the operations are:
APPEND X - Append object X to the end of the list
ADD N X - Insert object X as the new Nth element in the list, increasing the size of the list by 1
E.g. Suppose the list is:
head -> 1 -> 2 -> 3 -> 4->null
After ADD 3 7 it would be:
head -> 1 -> 2 -> 7 -> 3 -> 4->null
DELETE N – Remove the Nth object from the list
SWAP M N - Interchange the positions of the Mth and Nth
objects on the list
For credit, the two nodes must actually \"trade places\" in the list, and not merely swap their data
values
REVERSE - Reverse the order of the objects on the list
This must be done by reversing the order of the nodes themselves, rather than by swapping the
data stored
To get credit for your reverse() method, it must use either one of these 2 algorithms:
For each node on the list except the current “head”
node, delete the node and insert it as the new head
Use your swap() method
6. CLEAR – Clear the list (make it empty)
No credit will be given for programs that use any additional data structures – either from the Java
API or programmer defined -, other than your own LinkedList class
txt file:
Solution
//Java Program to Implement Singly Linked List
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/* Class Node */
class Nodes
{
protected Object data;
protected Nodes next;
/* Constructor */
public Nodes()
{
next = null;
data = null;
}
/* Constructor */
public Nodes(Object d,Nodes n)
{
data = d;
next = n;
}
/* Function to set link to next Node */
public void setLink(Nodes n)
{
next = n;
}
/* Function to set data to current Node */
public void setData(Object d)
{
data = d;
}
/* Function to get link to next node */
public Nodes getLink()
{
return next;
}
/* Function to get data from current Node */
public Object getData()
{
return data;
}
}
/* Class linkedList */
class linkList
{
protected Nodes start;
protected Nodes end ;
public int size ;
/* Constructor */
public linkList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return .
1
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int count = 0; /* the index of the node we\'re currently
looking at */
while (current != NULL)
{
if (count == index)
return(current->data);
count++;
current = current->next;
}
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
/* Check the count function */
printf(\"Element at index 3 is %d\", GetNth(head, 3));
getchar();
}
2
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int count = 0; /* the index of the node we\'re currently
looking at */
while (current != NULL)
{
if (count == index)
return(current->data);
count++;
current = current->next;
}
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
/* Check the count function */
printf(\"Element at index 3 is %d\", GetNth(head, 3));
getchar();
}
Solution
1
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node*.
coding in C- Create a function called reverseList that takes the head.docxtienlivick
coding in C;
Create a function called reverseList that takes the head of a linked list, reverses the order of all the nodes. For example, if the list contained 1, 2, 3, 4 in its nodes, the list will now contain 4, 3, 2, 1.
please add a main test.
Solution
Please find the required program below:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node** head, int new_element)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_element;
new_node->next = (*head);
(*head) = new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
void reverseList(struct node** head)
{
struct node* prev = NULL;
struct node* current = *head;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
int main()
{
struct node* head = NULL;
push(&head, 12);
push(&head, 14);
push(&head, 5);
push(&head, 8);
printf(\"\ Linked list Before Reversing :\ \");
printList(head);
reverseList(&head);
printf(\"\ Linked list After Reversing :\ \");
printList(head);
}
--------------------------------------------------------------------------------
OUTPUT:
Linked list Before Reversing :
8Â Â 5Â Â 14Â Â 12
Linked list After Reversing :
12Â Â 14Â Â 5Â Â 8
.
Note Given Code modified as required and required met.pdfAnkitchhabra28
Note:
Given Code modified as required and required methods added.
Code:
// class node
class Node
{
// declare the required variable
String data;
Node next;
// constructor
Node(){}
// constructor with arguments
Node(String data)
{
this.data = data;
next = null;
}
// method to get the data
public String getData()
{
return data;
}
// method to set the data
public void setData(String data)
{
this.data = data;
}
// method to get the next data
public Node getNext()
{
return next;
}
// method to set the next data
public void setNext(Node next)
{
this.next = next;
}
}
// class for a linked list
class MyLInkedList
{
Node head;alue into a list
// method to add the v
public void add(String num)
{
if (head == null)
{
head = new Node(num);
}
else{
Node temp = new Node(num);
Node current = head;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
}
}
}
// method to search a value from a list
public void search(String num)
{
Node current = head;
boolean flag=true;
if (current != null)
{
while (current != null)
{
if(current.getData() == num)
{
System.out.println(\"Search value \"+
current.getData()+\" is available\");
flag=false;
}
current = current.getNext();
}
if (flag==true)
{
System.out.println(\"Search value \"+
current.getData()+\" is not available\");
}
}
else
{
System.out.println(\"Linked list is empty\");
}
}
// method to delete a value from a list
public void delete(String num)
{
//Node current = head;
boolean flag=true;
if (head == null)
{
System.out.println(\"Can\'t Delete, Linked list is
empty\");
return;
}
if(head.getData() == num)
{
head=head.getNext();
return;
}
Node temp = new Node(num);
Node cr = head;
Node prev=null;
while ((cr != null) && !(cr.getData() == num))
{
prev=cr;
cr = cr.getNext();
}
prev.setNext(cr.getNext());
}
// method to print the list
public void print()
{
Node current = head;
if (current != null) {
while (current != null) {
System.out.println(current.getData());
current = current.getNext();
}
}
}
}
// method to test the class
public class MyLInkedListTester
{
// main method to perform the linked list operatrions
public static void main(String args[])
{
MyLInkedList llist = new MyLInkedList();
// insert the values into list
llist.add(\"1\");
llist.add(\"2\");
llist.add(\"3\");
llist.add(\"4\");
// print the list values
llist.print();
// sear the values in a list
llist.search(\"1\");
llist.print();
// delete the value from a list
llist.delete(\"4\");
System.out.println(\"List after deletion:\");
llist.print();
}
}
Result:
1
2
3
4
Search value 1 is available
1
2
3
4
List after deletion:
1
2
3
Solution
Note:
Given Code modified as required and required methods added.
Code:
// class node
class Node
{
// declare the required variable
String data;
Node next;
// constructor
Node(){}
// constructor with arguments
Node(String data)
{
this.data = data;
next = null;
}
// method to get the data
public String getData()
{
return data;
}
// method to set the data
public void se.
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
/**
* Exception to indicate that Singly LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException {
public LinkedListEmptyException() {
super();
}
public LinkedListEmptyException(String message) {
super(message);
}
}
/**
* Node class, which holds data and contains next which points to next Node.
*/
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
/**
* Constructor
*/
public Node(int data) {
this.data = data;
}
/**
* Display Node\'s data
*/
public void displayNode() {
System.out.print(data + \" \");
}
}
/**
* Singly LinkedList class
*/
class LinkedList {
private Node first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList() {
first = null;
}
/**
* Insert New Node at first position
*/
public void insertFirst(int data) {
Node newNode = new Node(data); // Creation of New Node.
newNode.next = first; // newLink ---> old first
first = newNode; // first ---> newNode
}
/**
* Method deletes specific Node from Singly LinkedList in java.
*/
public Node deleteSpecificNode(int deleteKey) {
// Case1: when there is no element in LinkedList
if (first == null) { // means LinkedList in empty, throw exception.
throw new LinkedListEmptyException(
\"LinkedList doesn\'t contain any Nodes.\");
}
// Case2: when there is only one element in LinkedList- check whether we
// have to delete that Node or not.
if (first.data == deleteKey) { // means LinkedList consists of only one
// element, delete that.
Node tempNode = first; // save reference to first Node in tempNode-
// so that we could return saved reference.
first = first.next;
System.out.println(\"Node with data=\" + tempNode.data
+ \" was found on first and has been deleted.\");
return tempNode; // return deleted Node.
}
// Case3: when there are atLeast two elements in LinkedList
Node previous = null;
Node current = first;
while (current != null) {
if (current.data == deleteKey) {
System.out.println(\"Node with data=\" + current.data
+ \" has been deleted.\");
previous.next = current.next; // make previous node\'s next point
// to current node\'s next.
return current; // return deleted Node.
} else {
if (current.next == null) { // Means Node wasn\'t found.
System.out.println(\"Node with data=\" + deleteKey
+ \" wasn\'t found for deletion.\");
return null;
}
previous = current;
current = current.next; // move to next node.
}
}
return null;
}
/**
* Display Singly LinkedList
*/
public void displayLinkedList() {
System.out.print(\"Displaying LinkedList [first--->last]: \");
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null) { // Executes until we don\'t find end of
// list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
System.out.print(\"-->\");
}
System.out.println();
}
}
/**
* Main class - To test LinkedList.
*/
public class SinglyLinkedListDeleteNodeExample {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); /.
Explore the world of C programming assignments with expert guidance and practical tips. Dive into a range of C assignment solutions designed for varying complexity levels. Get insights from industry experts passionate about sharing their C programming knowledge. Discover time-saving techniques to boost productivity and meet assignment deadlines. Access 24/7 customer support and budget-friendly pricing options.
Ready to excel in C programming assignments? Head over to https://www.programminghomeworkhelp.com/c-assignment/ to embark on a journey toward academic excellence. Don't let C assignments hold you back. Join us on the path to success today!
Problem- Describe an algorithm for concatenating two singly linked lis.pdfJamesPXNNewmanp
Problem: Describe an algorithm for concatenating two singly linked lists L and M, into a single
list L that contains all the nodes of L followed by all the nodes of M.
Modify the SinglyLinkedList class to contain the method:
public void concatenate(SinglyLinkedList other) { ... }
---------------Below is the code--------------------
public class SinglyLinkedList<E> implements Cloneable {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
/** The element stored at this node */
private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n reference to a node that should follow the new node
*/
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods
/**
* Returns the element stored at the node.
* @return the element stored at the node
*/
public E getElement() { return element; }
/**
* Returns the node that follows this one (or null if no such node).
* @return the following node
*/
public Node<E> getNext() { return next; }
// Modifier methods
/**
* Sets the node's next reference to point to Node n.
* @param n the node that should follow this one
*/
public void setNext(Node<E> n) { next = n; }
} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
/** The head node of the list */
private Node<E> head = null; // head node of the list (or null if empty)
/** The last node of the list */
private Node<E> tail = null; // last node of the list (or null if empty)
/** Number of nodes in the list */
private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */
public SinglyLinkedList() { } // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list
* @return element at the front of the list (or null if empty)
*/
public E first() { // returns (but does not remove) the first element
if (isEmpty()) return null;
return head.getElement();
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() { // returns (but does not remove) the last element
if (isEmpty()) return null;
return tail.getElement();
}
// update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
publ.
The document contains C program code to implement three data structures using linked lists:
1) A stack using a singly linked list with push, pop, and display functions.
2) A queue using a singly linked list with enqueue, dequeue, and display functions.
3) A program to merge two sorted linked lists by comparing nodes and appending to the merged list.
Pseudocode and sample test cases are provided for each problem.
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
How do you stop infinite loop? Because I believe that it is making an infinite circular list.
c++ code:
Here is the list class:
#ifndef LIN_J_LIST
#define LIN_J_LIST
typedef unsigned int uint;
#include
#include
using namespace std;
/**
* a simplified generic singly linked list class to illustrate C++ concepts
* @author Jerry Lin
* @version 2/17/17
*/
template< typename Object >
class List
{
private:
/**
* A class to store data and provide a link to the next node in the list
*/
class Node
{
public:
/**
* The constructor
* @param data the data to be stored in this node
*/
explicit Node( const Object & data )
: data{ data }, next{ nullptr } {}
Object data;
Node * next;
};
public:
/**
* The constructor for an empty list
*/
List()
: size{ 0 }, first{ nullptr }, last{ nullptr } {}
/**
* the copy constructor-creates and copy the list
*/
List( List && rhs ) = delete;
List( const List & rhs )
{
count = 0;
size = 0;
if(rhs.size != 0)
{
push_front(rhs.front());
Node * current = rhs.first;
Node * tempNode = first;
size++;
while(current->next != nullptr)
{
current = current->next;
Node *newNode = new Node(current->data); //transfer the data. basic op.
count++;
tempNode->next = newNode;
last = newNode;
tempNode = tempNode->next;
size++;
}
}
// you document and implement this method
}
/**
* the operator= method-copies the list
*/
List & operator=( List && rhs) = delete;
List & operator=( const List & rhs )
{
count = 0;
size = 0;
if( size != 0 )
{
Node * current = first;
Node * temp;
while( current != nullptr )
{
temp = current;
current = current->next;
delete temp;
}
}
if(rhs.size!= 0)
{
push_front(rhs.front());
Node * current = rhs.first;
Node * tempNode = first; //create a temporary to store
size++;
while(current -> next != nullptr)
{
current = current->next;
Node *newNode = new Node(current->data); //transfer the data. basic op
count++;
tempNode->next = newNode;
last = newNode;
tempNode = tempNode -> next;
size++;
}
}
return *this;
// you document and implement this method
}
/**
* accessor
* @return count
*/
int get_count() const
{
return count;
}
/**
* The destructor that gets rid of everything that\'s in the list and
* resets it to empty. If the list is already empty, do nothing.
*/
~List()
{
if( size != 0 )
{
Node * current = first;
Node * temp;
while( current != nullptr )
{
temp = current;
current = current->next;
delete temp;
}
}
}
/**
* Put a new element onto the beginning of the list
* @param item the data the new element will contain
*/
void push_front( const Object& item )
{
Node * new_node = new Node( item );//basic op.
if(is_empty())
{
last = new_node;
}
else
{
new_node->next = first;
}
first = new_node;
size++;
/* you complete the rest */
}
/**
* Remove the element that\'s at the front of the list. Causes an
* assertion error if the list is empty.
*/
void pop_front()
{
assert( !is_empty() );
Node * temp = first;
if( first == last )
{
first = last = nullptr;
}
else
{
first = first->next;
}
delete temp;
size--;
}
/**
* Accessor to return the da.
Problem- Describe an algorithm for concatenating two singly linked lis.pdfkingsandqueens3
Problem: Describe an algorithm for concatenating two singly linked lists L and M, into a single
list L that contains all the nodes of L followed by all the nodes of M. Modify the
SinglyLinkedList class to contain the method:
public void concatenate(SinglyLinkedList other) { ... }
PLEASE PLEASE USE THE CODE BELOW..... THANK YOU
public class SinglyLinkedList<E> implements Cloneable {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
/** The element stored at this node */
private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n reference to a node that should follow the new node
*/
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods
/**
* Returns the element stored at the node.
* @return the element stored at the node
*/
public E getElement() { return element; }
/**
* Returns the node that follows this one (or null if no such node).
* @return the following node
*/
public Node<E> getNext() { return next; }
// Modifier methods
/**
* Sets the node's next reference to point to Node n.
* @param n the node that should follow this one
*/
public void setNext(Node<E> n) { next = n; }
} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
/** The head node of the list */
private Node<E> head = null; // head node of the list (or null if empty)
/** The last node of the list */
private Node<E> tail = null; // last node of the list (or null if empty)
/** Number of nodes in the list */
private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */
public SinglyLinkedList() { } // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list
* @return element at the front of the list (or null if empty)
*/
public E first() { // returns (but does not remove) the first element
if (isEmpty()) return null;
return head.getElement();
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() { // returns (but does not remove) the last element
if (isEmpty()) return null;
return tail.getElement();
}
// update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
public vo.
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED
HELP FIXING THIS CODE?
THIS IS THE PROBLEM
Write a program that allows the user to enter two positive integers and outputs their sum. Sounds
simple? Here\'s the catch: The numbers may be any size! They may be way too large to store in a
single variable. You may not assume any particular maximum number of digits.
HERE IS THE CODE
#include
#include
struct node /* Linked list node*/
{
int data;
struct node* next;
};
struct node *newNode(int data) /* Function to create a new node with giving the data*/
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void push(struct node** head_ref, int new_data) /* Function to insert a node at the beginning of
the Doubly Linked List*/
{
struct node* new_node = newNode(new_data); /*allocate a node*/
new_node->next = (*head_ref); /* link the old list off to the new node*/
(*head_ref) = new_node; /*move the head to point to the new node*/
}
/* Adding the contents of two linked lists and return the head node of resultant list*/
struct node* addTwoLists (struct node* first, struct node* second)
{
struct node* res = NULL; /* res is head node of the resultant list*/
struct node *temp, *prev = NULL;
int carry = 0, sum;
while (first != NULL || second != NULL) /*while both the lists exists*/
{
/* Calculate value of next digit in resultant list.
// The next digit is sum of following things
// (i) Carry
// (ii) Next digit of first list (if there is a next digit)
// (ii) Next digit of second list (if there is a next digit)*/
sum = carry + (first? first->data: 0) + (second? second->data: 0);
carry = (sum >= 10)? 1 : 0; /* update carry for next calulation*/
sum = sum % 10; /*update sum if it is greater than 10*/
temp = newNode(sum); /*Create a new node with sum as data*/
if(res == NULL) /*if this is the first node then set it as head of the resultant list*/
res = temp;
else /* If this is not the first node then connect it to the rest.*/
prev->next = temp;
prev = temp; /* Set prev for next insertion*/
if (first) first = first->next; /* Move first pointers to next node*/
if (second) second = second->next; /*Move second pointer to the next node*/
}
if (carry > 0)
temp->next = newNode(carry);
return res; /* return head of the resultant list*/
}
void printList(struct node *node) /* function to print a linked list*/
{
while(node != NULL)
{
printf(\"%d \", node->data);
node = node->next;
}
printf(\"\ \");
}
int main(void) /* program to test above function*/
{
struct node* res = NULL;
struct node* first = NULL;
struct node* second = NULL;
printf(\"Enter first positive integer: \");
scanf(\"%d\",&first);
printList(first);
printf(\"Enter second positive integer: \");
scanf(\"%d\",&second);
printList(second);
/*Add the two lists and see result*/
res = addTwoLists(first, second);
printf(\"Sum is: %d\");
printList(res);
return 0;
}
Solution
#include
#include
typedef struct Node
{
int da.
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
C++ please put everthing after you answer it, thanks
Complete the stub file for the DoublyLinkedList implementation.
Please note: Any changes to the NodeType struct, not using the NodeType struct, or changing from template Type to using a specific data type will all be cause for deductions.
You are required to implement the methods:
insertFirst
insertLast
insertNode - (insert an item at a given index)
deleteNode - (remove a specific item)
The stub file contains the basic class format to implement the virtual methods from ListType. You should only need to complete the code for these four functions. Your code must maintain (or add) the node connections both forward and backward. In the double link list class these are called next and prev.
Your code must also maintain the count variable.
\} template Type ListType : : back ( ) const \{ assert(tail != NULL); //confirm tail exists return tail->data; //return the data of the tail node \} template void ListType >: :copyList (const ListType > \& otherList) \{ NodeType *newNode; //pointer to create a node NodeType current; //pointer to traverse the list if (head ! = NULL) //if the list is nonempty, make it empty destroyList(); if (otherList. head == NULL) //otherList is empty \{ head = NULL; tail = NULL; count = 0 ; \} else \{ current = otherList. head; // current points to the count = otherList . count; // list to be copied //copy the head node head = new NodeType < Type > ; / / create the node head->data = current > data; // copy the data head->next = NULL; // set the link field of tail = head; // the node to NULL // the next node //copy the remaining list while (current != NULL) \{ newNode = new NodeType < Type > ; // create a node newNode > data = current > data; // copy the data newNode->next = NULL; // set the link of // newNode to NULL tail->next = newNode; //attach newNode after tail tail = newNode; // make tail point to //the actual tail node current = current > next; 1/ make current point //to the next node \}/ /end while \}/lend else \}//end copylist
.
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
Data Structures in C++
I am really new to C++, so links are really hard topic for me. It would be nice if you can provide
explanations of what doubly linked lists are and of some of you steps... Thank you
In this assignment, you will implement a doubly-linked list class, together with some list
operations. To make things easier, you’ll implement a list of int, rather than a template class.
Solution
A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a.
Now, if we have collection of integers, we need some representation to identify them. We call it
array. For ex: int arr[5]
This array is nothing but a Data Structure.
So, a Data Structure is a way to group the data.
There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack,
Queue, etc.
Doubly-Linked list are the ones where you can traverse from the current node both in left and
right directions.
Why so many different types of Data Structures are required ?
Answer is very simple, grouping of data, storage of data and accessing the data is different.
For example, in case of Arrays we store all the data in contiguous locations.
What if we are not able to store the data in contiguous locations because we have huge data.
Answer is go for Linked List/Doubly-Linked list.
Here we can store the data anywhere and link the data through pointers.
I will try to provide comments for the code you have given. May be this can help you.
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include
class dlist {
public:
dlist() { }
// Here we are creating a NODE, it has a integer value and two pointers.
// One pointer is to move to next node and other to go back to previous node.
struct node {
int value;
node* next;
node* prev;
};
// To return head pointer, i.e. start of the Doubly-Linked list.
node* head() const { return _head; }
// To return Tail pointer, i.e. end of the Doubly-Linked list.
node* tail() const { return _tail; }
// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head).
node* at(int index){
int cnt = 0;
struct node* tmp = head();
while(tmp!=NULL)
{
if (cnt+1 == index)
return tmp;
tmp = tmp->next;
}
}
// Insert a new value, after an existing one
void insert(node *previous, int value){
// check if the given previous is NULL
if (previous == NULL)
{
printf(\"the given previous node cannot be NULL\");
return;
}
// allocate new node
struct node* new_node =(struct node*) malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// Make next of new node as next of previous
new_node->next = previous->next;
// Make the next of previous as new_node
previous->next = new_node;
// Make previous as previous of new_node
new_node->prev = previous;
// Change previous of new_node\'s next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Delete the given node
void del(node* which){
struct node* head_ref = head();
/* base case */
if(*head_ref == NUL.
C++Write a method Node Nodereverse() which reverses a list..pdfarjunenterprises1978
C++
Write a method Node *Node::reverse() which reverses a list.
Solution
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf(\"Given linked list\ \");
printList(head);
reverse(&head);
printf(\"\ Reversed Linked list \ \");
printList(head);
getchar();
}.
Linked lists are linear data structures where each node points to the next. Each node contains a data field and a pointer to the next node. There are three types: singly, doubly, and circular linked lists. Linked lists allow for constant-time insertions and deletions and do not require fixed size allocation. Common operations on linked lists include insertion, deletion, searching, and traversal. Linked lists are useful for implementations like stacks, queues, and dynamic data structures.
This C++ code defines a linked stack data structure using classes. The Lstack class implements a linked stack with methods to push values onto the stack, pop values off the stack, and display the stack. A main function tests the stack by pushing multiple values, displaying the stack, popping a value, and displaying the modified stack.
C program to insert a node in doubly linked listSourav Gayen
This C program implements functions to create and manipulate a doubly linked list. It allows the user to choose from a menu to create a list, insert nodes at the beginning, end, or a specified position, and display the list. Functions are defined to create a list by dynamically allocating nodes and linking them together, insert nodes at different positions by updating next and prev pointers, and display the list by traversing from head to tail.
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
Write a Java Class to Implement a Generic Linked List
Your list must be implemented as a singly-linked list of generic nodes, where each Node object
has two instance variables: an object of the “type variable” class, and a pointer to the next node
on the list.
Your class will contain separate methods to handle each of the operations read from a data file
(see II., below)
Your class will also override toString() to return the objects on the list in the order in which they
occur.
Write a Test Class for Your LinkedList Class
Your main method will read list operation instructions from a data file, until end-of-file and call
the appropriate LinkedList method to execute each one.
After each operation is executed, print out the operation and the updated list.
The data file to be used is on the class web site and the operations are:
APPEND X - Append object X to the end of the list
ADD N X - Insert object X as the new Nth element in the list, increasing the size of the list by 1
E.g. Suppose the list is:
head -> 1 -> 2 -> 3 -> 4->null
After ADD 3 7 it would be:
head -> 1 -> 2 -> 7 -> 3 -> 4->null
DELETE N – Remove the Nth object from the list
SWAP M N - Interchange the positions of the Mth and Nth
objects on the list
For credit, the two nodes must actually \"trade places\" in the list, and not merely swap their data
values
REVERSE - Reverse the order of the objects on the list
This must be done by reversing the order of the nodes themselves, rather than by swapping the
data stored
To get credit for your reverse() method, it must use either one of these 2 algorithms:
For each node on the list except the current “head”
node, delete the node and insert it as the new head
Use your swap() method
6. CLEAR – Clear the list (make it empty)
No credit will be given for programs that use any additional data structures – either from the Java
API or programmer defined -, other than your own LinkedList class
txt file:
Solution
//Java Program to Implement Singly Linked List
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/* Class Node */
class Nodes
{
protected Object data;
protected Nodes next;
/* Constructor */
public Nodes()
{
next = null;
data = null;
}
/* Constructor */
public Nodes(Object d,Nodes n)
{
data = d;
next = n;
}
/* Function to set link to next Node */
public void setLink(Nodes n)
{
next = n;
}
/* Function to set data to current Node */
public void setData(Object d)
{
data = d;
}
/* Function to get link to next node */
public Nodes getLink()
{
return next;
}
/* Function to get data from current Node */
public Object getData()
{
return data;
}
}
/* Class linkedList */
class linkList
{
protected Nodes start;
protected Nodes end ;
public int size ;
/* Constructor */
public linkList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return .
1
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int count = 0; /* the index of the node we\'re currently
looking at */
while (current != NULL)
{
if (count == index)
return(current->data);
count++;
current = current->next;
}
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
/* Check the count function */
printf(\"Element at index 3 is %d\", GetNth(head, 3));
getchar();
}
2
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int count = 0; /* the index of the node we\'re currently
looking at */
while (current != NULL)
{
if (count == index)
return(current->data);
count++;
current = current->next;
}
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
/* Check the count function */
printf(\"Element at index 3 is %d\", GetNth(head, 3));
getchar();
}
Solution
1
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node*.
coding in C- Create a function called reverseList that takes the head.docxtienlivick
coding in C;
Create a function called reverseList that takes the head of a linked list, reverses the order of all the nodes. For example, if the list contained 1, 2, 3, 4 in its nodes, the list will now contain 4, 3, 2, 1.
please add a main test.
Solution
Please find the required program below:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node** head, int new_element)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_element;
new_node->next = (*head);
(*head) = new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
void reverseList(struct node** head)
{
struct node* prev = NULL;
struct node* current = *head;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
int main()
{
struct node* head = NULL;
push(&head, 12);
push(&head, 14);
push(&head, 5);
push(&head, 8);
printf(\"\ Linked list Before Reversing :\ \");
printList(head);
reverseList(&head);
printf(\"\ Linked list After Reversing :\ \");
printList(head);
}
--------------------------------------------------------------------------------
OUTPUT:
Linked list Before Reversing :
8Â Â 5Â Â 14Â Â 12
Linked list After Reversing :
12Â Â 14Â Â 5Â Â 8
.
Note Given Code modified as required and required met.pdfAnkitchhabra28
Note:
Given Code modified as required and required methods added.
Code:
// class node
class Node
{
// declare the required variable
String data;
Node next;
// constructor
Node(){}
// constructor with arguments
Node(String data)
{
this.data = data;
next = null;
}
// method to get the data
public String getData()
{
return data;
}
// method to set the data
public void setData(String data)
{
this.data = data;
}
// method to get the next data
public Node getNext()
{
return next;
}
// method to set the next data
public void setNext(Node next)
{
this.next = next;
}
}
// class for a linked list
class MyLInkedList
{
Node head;alue into a list
// method to add the v
public void add(String num)
{
if (head == null)
{
head = new Node(num);
}
else{
Node temp = new Node(num);
Node current = head;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
}
}
}
// method to search a value from a list
public void search(String num)
{
Node current = head;
boolean flag=true;
if (current != null)
{
while (current != null)
{
if(current.getData() == num)
{
System.out.println(\"Search value \"+
current.getData()+\" is available\");
flag=false;
}
current = current.getNext();
}
if (flag==true)
{
System.out.println(\"Search value \"+
current.getData()+\" is not available\");
}
}
else
{
System.out.println(\"Linked list is empty\");
}
}
// method to delete a value from a list
public void delete(String num)
{
//Node current = head;
boolean flag=true;
if (head == null)
{
System.out.println(\"Can\'t Delete, Linked list is
empty\");
return;
}
if(head.getData() == num)
{
head=head.getNext();
return;
}
Node temp = new Node(num);
Node cr = head;
Node prev=null;
while ((cr != null) && !(cr.getData() == num))
{
prev=cr;
cr = cr.getNext();
}
prev.setNext(cr.getNext());
}
// method to print the list
public void print()
{
Node current = head;
if (current != null) {
while (current != null) {
System.out.println(current.getData());
current = current.getNext();
}
}
}
}
// method to test the class
public class MyLInkedListTester
{
// main method to perform the linked list operatrions
public static void main(String args[])
{
MyLInkedList llist = new MyLInkedList();
// insert the values into list
llist.add(\"1\");
llist.add(\"2\");
llist.add(\"3\");
llist.add(\"4\");
// print the list values
llist.print();
// sear the values in a list
llist.search(\"1\");
llist.print();
// delete the value from a list
llist.delete(\"4\");
System.out.println(\"List after deletion:\");
llist.print();
}
}
Result:
1
2
3
4
Search value 1 is available
1
2
3
4
List after deletion:
1
2
3
Solution
Note:
Given Code modified as required and required methods added.
Code:
// class node
class Node
{
// declare the required variable
String data;
Node next;
// constructor
Node(){}
// constructor with arguments
Node(String data)
{
this.data = data;
next = null;
}
// method to get the data
public String getData()
{
return data;
}
// method to set the data
public void se.
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
/**
* Exception to indicate that Singly LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException {
public LinkedListEmptyException() {
super();
}
public LinkedListEmptyException(String message) {
super(message);
}
}
/**
* Node class, which holds data and contains next which points to next Node.
*/
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
/**
* Constructor
*/
public Node(int data) {
this.data = data;
}
/**
* Display Node\'s data
*/
public void displayNode() {
System.out.print(data + \" \");
}
}
/**
* Singly LinkedList class
*/
class LinkedList {
private Node first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList() {
first = null;
}
/**
* Insert New Node at first position
*/
public void insertFirst(int data) {
Node newNode = new Node(data); // Creation of New Node.
newNode.next = first; // newLink ---> old first
first = newNode; // first ---> newNode
}
/**
* Method deletes specific Node from Singly LinkedList in java.
*/
public Node deleteSpecificNode(int deleteKey) {
// Case1: when there is no element in LinkedList
if (first == null) { // means LinkedList in empty, throw exception.
throw new LinkedListEmptyException(
\"LinkedList doesn\'t contain any Nodes.\");
}
// Case2: when there is only one element in LinkedList- check whether we
// have to delete that Node or not.
if (first.data == deleteKey) { // means LinkedList consists of only one
// element, delete that.
Node tempNode = first; // save reference to first Node in tempNode-
// so that we could return saved reference.
first = first.next;
System.out.println(\"Node with data=\" + tempNode.data
+ \" was found on first and has been deleted.\");
return tempNode; // return deleted Node.
}
// Case3: when there are atLeast two elements in LinkedList
Node previous = null;
Node current = first;
while (current != null) {
if (current.data == deleteKey) {
System.out.println(\"Node with data=\" + current.data
+ \" has been deleted.\");
previous.next = current.next; // make previous node\'s next point
// to current node\'s next.
return current; // return deleted Node.
} else {
if (current.next == null) { // Means Node wasn\'t found.
System.out.println(\"Node with data=\" + deleteKey
+ \" wasn\'t found for deletion.\");
return null;
}
previous = current;
current = current.next; // move to next node.
}
}
return null;
}
/**
* Display Singly LinkedList
*/
public void displayLinkedList() {
System.out.print(\"Displaying LinkedList [first--->last]: \");
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null) { // Executes until we don\'t find end of
// list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
System.out.print(\"-->\");
}
System.out.println();
}
}
/**
* Main class - To test LinkedList.
*/
public class SinglyLinkedListDeleteNodeExample {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); /.
Explore the world of C programming assignments with expert guidance and practical tips. Dive into a range of C assignment solutions designed for varying complexity levels. Get insights from industry experts passionate about sharing their C programming knowledge. Discover time-saving techniques to boost productivity and meet assignment deadlines. Access 24/7 customer support and budget-friendly pricing options.
Ready to excel in C programming assignments? Head over to https://www.programminghomeworkhelp.com/c-assignment/ to embark on a journey toward academic excellence. Don't let C assignments hold you back. Join us on the path to success today!
Problem- Describe an algorithm for concatenating two singly linked lis.pdfJamesPXNNewmanp
Problem: Describe an algorithm for concatenating two singly linked lists L and M, into a single
list L that contains all the nodes of L followed by all the nodes of M.
Modify the SinglyLinkedList class to contain the method:
public void concatenate(SinglyLinkedList other) { ... }
---------------Below is the code--------------------
public class SinglyLinkedList<E> implements Cloneable {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
/** The element stored at this node */
private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n reference to a node that should follow the new node
*/
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods
/**
* Returns the element stored at the node.
* @return the element stored at the node
*/
public E getElement() { return element; }
/**
* Returns the node that follows this one (or null if no such node).
* @return the following node
*/
public Node<E> getNext() { return next; }
// Modifier methods
/**
* Sets the node's next reference to point to Node n.
* @param n the node that should follow this one
*/
public void setNext(Node<E> n) { next = n; }
} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
/** The head node of the list */
private Node<E> head = null; // head node of the list (or null if empty)
/** The last node of the list */
private Node<E> tail = null; // last node of the list (or null if empty)
/** Number of nodes in the list */
private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */
public SinglyLinkedList() { } // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list
* @return element at the front of the list (or null if empty)
*/
public E first() { // returns (but does not remove) the first element
if (isEmpty()) return null;
return head.getElement();
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() { // returns (but does not remove) the last element
if (isEmpty()) return null;
return tail.getElement();
}
// update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
publ.
The document contains C program code to implement three data structures using linked lists:
1) A stack using a singly linked list with push, pop, and display functions.
2) A queue using a singly linked list with enqueue, dequeue, and display functions.
3) A program to merge two sorted linked lists by comparing nodes and appending to the merged list.
Pseudocode and sample test cases are provided for each problem.
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
How do you stop infinite loop? Because I believe that it is making an infinite circular list.
c++ code:
Here is the list class:
#ifndef LIN_J_LIST
#define LIN_J_LIST
typedef unsigned int uint;
#include
#include
using namespace std;
/**
* a simplified generic singly linked list class to illustrate C++ concepts
* @author Jerry Lin
* @version 2/17/17
*/
template< typename Object >
class List
{
private:
/**
* A class to store data and provide a link to the next node in the list
*/
class Node
{
public:
/**
* The constructor
* @param data the data to be stored in this node
*/
explicit Node( const Object & data )
: data{ data }, next{ nullptr } {}
Object data;
Node * next;
};
public:
/**
* The constructor for an empty list
*/
List()
: size{ 0 }, first{ nullptr }, last{ nullptr } {}
/**
* the copy constructor-creates and copy the list
*/
List( List && rhs ) = delete;
List( const List & rhs )
{
count = 0;
size = 0;
if(rhs.size != 0)
{
push_front(rhs.front());
Node * current = rhs.first;
Node * tempNode = first;
size++;
while(current->next != nullptr)
{
current = current->next;
Node *newNode = new Node(current->data); //transfer the data. basic op.
count++;
tempNode->next = newNode;
last = newNode;
tempNode = tempNode->next;
size++;
}
}
// you document and implement this method
}
/**
* the operator= method-copies the list
*/
List & operator=( List && rhs) = delete;
List & operator=( const List & rhs )
{
count = 0;
size = 0;
if( size != 0 )
{
Node * current = first;
Node * temp;
while( current != nullptr )
{
temp = current;
current = current->next;
delete temp;
}
}
if(rhs.size!= 0)
{
push_front(rhs.front());
Node * current = rhs.first;
Node * tempNode = first; //create a temporary to store
size++;
while(current -> next != nullptr)
{
current = current->next;
Node *newNode = new Node(current->data); //transfer the data. basic op
count++;
tempNode->next = newNode;
last = newNode;
tempNode = tempNode -> next;
size++;
}
}
return *this;
// you document and implement this method
}
/**
* accessor
* @return count
*/
int get_count() const
{
return count;
}
/**
* The destructor that gets rid of everything that\'s in the list and
* resets it to empty. If the list is already empty, do nothing.
*/
~List()
{
if( size != 0 )
{
Node * current = first;
Node * temp;
while( current != nullptr )
{
temp = current;
current = current->next;
delete temp;
}
}
}
/**
* Put a new element onto the beginning of the list
* @param item the data the new element will contain
*/
void push_front( const Object& item )
{
Node * new_node = new Node( item );//basic op.
if(is_empty())
{
last = new_node;
}
else
{
new_node->next = first;
}
first = new_node;
size++;
/* you complete the rest */
}
/**
* Remove the element that\'s at the front of the list. Causes an
* assertion error if the list is empty.
*/
void pop_front()
{
assert( !is_empty() );
Node * temp = first;
if( first == last )
{
first = last = nullptr;
}
else
{
first = first->next;
}
delete temp;
size--;
}
/**
* Accessor to return the da.
Problem- Describe an algorithm for concatenating two singly linked lis.pdfkingsandqueens3
Problem: Describe an algorithm for concatenating two singly linked lists L and M, into a single
list L that contains all the nodes of L followed by all the nodes of M. Modify the
SinglyLinkedList class to contain the method:
public void concatenate(SinglyLinkedList other) { ... }
PLEASE PLEASE USE THE CODE BELOW..... THANK YOU
public class SinglyLinkedList<E> implements Cloneable {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
/** The element stored at this node */
private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node<E> next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n reference to a node that should follow the new node
*/
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// Accessor methods
/**
* Returns the element stored at the node.
* @return the element stored at the node
*/
public E getElement() { return element; }
/**
* Returns the node that follows this one (or null if no such node).
* @return the following node
*/
public Node<E> getNext() { return next; }
// Modifier methods
/**
* Sets the node's next reference to point to Node n.
* @param n the node that should follow this one
*/
public void setNext(Node<E> n) { next = n; }
} //----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
/** The head node of the list */
private Node<E> head = null; // head node of the list (or null if empty)
/** The last node of the list */
private Node<E> tail = null; // last node of the list (or null if empty)
/** Number of nodes in the list */
private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */
public SinglyLinkedList() { } // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list
* @return element at the front of the list (or null if empty)
*/
public E first() { // returns (but does not remove) the first element
if (isEmpty()) return null;
return head.getElement();
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() { // returns (but does not remove) the last element
if (isEmpty()) return null;
return tail.getElement();
}
// update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
public vo.
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED
HELP FIXING THIS CODE?
THIS IS THE PROBLEM
Write a program that allows the user to enter two positive integers and outputs their sum. Sounds
simple? Here\'s the catch: The numbers may be any size! They may be way too large to store in a
single variable. You may not assume any particular maximum number of digits.
HERE IS THE CODE
#include
#include
struct node /* Linked list node*/
{
int data;
struct node* next;
};
struct node *newNode(int data) /* Function to create a new node with giving the data*/
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void push(struct node** head_ref, int new_data) /* Function to insert a node at the beginning of
the Doubly Linked List*/
{
struct node* new_node = newNode(new_data); /*allocate a node*/
new_node->next = (*head_ref); /* link the old list off to the new node*/
(*head_ref) = new_node; /*move the head to point to the new node*/
}
/* Adding the contents of two linked lists and return the head node of resultant list*/
struct node* addTwoLists (struct node* first, struct node* second)
{
struct node* res = NULL; /* res is head node of the resultant list*/
struct node *temp, *prev = NULL;
int carry = 0, sum;
while (first != NULL || second != NULL) /*while both the lists exists*/
{
/* Calculate value of next digit in resultant list.
// The next digit is sum of following things
// (i) Carry
// (ii) Next digit of first list (if there is a next digit)
// (ii) Next digit of second list (if there is a next digit)*/
sum = carry + (first? first->data: 0) + (second? second->data: 0);
carry = (sum >= 10)? 1 : 0; /* update carry for next calulation*/
sum = sum % 10; /*update sum if it is greater than 10*/
temp = newNode(sum); /*Create a new node with sum as data*/
if(res == NULL) /*if this is the first node then set it as head of the resultant list*/
res = temp;
else /* If this is not the first node then connect it to the rest.*/
prev->next = temp;
prev = temp; /* Set prev for next insertion*/
if (first) first = first->next; /* Move first pointers to next node*/
if (second) second = second->next; /*Move second pointer to the next node*/
}
if (carry > 0)
temp->next = newNode(carry);
return res; /* return head of the resultant list*/
}
void printList(struct node *node) /* function to print a linked list*/
{
while(node != NULL)
{
printf(\"%d \", node->data);
node = node->next;
}
printf(\"\ \");
}
int main(void) /* program to test above function*/
{
struct node* res = NULL;
struct node* first = NULL;
struct node* second = NULL;
printf(\"Enter first positive integer: \");
scanf(\"%d\",&first);
printList(first);
printf(\"Enter second positive integer: \");
scanf(\"%d\",&second);
printList(second);
/*Add the two lists and see result*/
res = addTwoLists(first, second);
printf(\"Sum is: %d\");
printList(res);
return 0;
}
Solution
#include
#include
typedef struct Node
{
int da.
Design and optimization of ion propulsion dronebjmsejournal
Electric propulsion technology is widely used in many kinds of vehicles in recent years, and aircrafts are no exception. Technically, UAVs are electrically propelled but tend to produce a significant amount of noise and vibrations. Ion propulsion technology for drones is a potential solution to this problem. Ion propulsion technology is proven to be feasible in the earth’s atmosphere. The study presented in this article shows the design of EHD thrusters and power supply for ion propulsion drones along with performance optimization of high-voltage power supply for endurance in earth’s atmosphere.
Batteries -Introduction – Types of Batteries – discharging and charging of battery - characteristics of battery –battery rating- various tests on battery- – Primary battery: silver button cell- Secondary battery :Ni-Cd battery-modern battery: lithium ion battery-maintenance of batteries-choices of batteries for electric vehicle applications.
Fuel Cells: Introduction- importance and classification of fuel cells - description, principle, components, applications of fuel cells: H2-O2 fuel cell, alkaline fuel cell, molten carbonate fuel cell and direct methanol fuel cells.
Comparative analysis between traditional aquaponics and reconstructed aquapon...bijceesjournal
The aquaponic system of planting is a method that does not require soil usage. It is a method that only needs water, fish, lava rocks (a substitute for soil), and plants. Aquaponic systems are sustainable and environmentally friendly. Its use not only helps to plant in small spaces but also helps reduce artificial chemical use and minimizes excess water use, as aquaponics consumes 90% less water than soil-based gardening. The study applied a descriptive and experimental design to assess and compare conventional and reconstructed aquaponic methods for reproducing tomatoes. The researchers created an observation checklist to determine the significant factors of the study. The study aims to determine the significant difference between traditional aquaponics and reconstructed aquaponics systems propagating tomatoes in terms of height, weight, girth, and number of fruits. The reconstructed aquaponics system’s higher growth yield results in a much more nourished crop than the traditional aquaponics system. It is superior in its number of fruits, height, weight, and girth measurement. Moreover, the reconstructed aquaponics system is proven to eliminate all the hindrances present in the traditional aquaponics system, which are overcrowding of fish, algae growth, pest problems, contaminated water, and dead fish.
Introduction- e - waste – definition - sources of e-waste– hazardous substances in e-waste - effects of e-waste on environment and human health- need for e-waste management– e-waste handling rules - waste minimization techniques for managing e-waste – recycling of e-waste - disposal treatment methods of e- waste – mechanism of extraction of precious metal from leaching solution-global Scenario of E-waste – E-waste in India- case studies.
Rainfall intensity duration frequency curve statistical analysis and modeling...bijceesjournal
Using data from 41 years in Patna’ India’ the study’s goal is to analyze the trends of how often it rains on a weekly, seasonal, and annual basis (1981−2020). First, utilizing the intensity-duration-frequency (IDF) curve and the relationship by statistically analyzing rainfall’ the historical rainfall data set for Patna’ India’ during a 41 year period (1981−2020), was evaluated for its quality. Changes in the hydrologic cycle as a result of increased greenhouse gas emissions are expected to induce variations in the intensity, length, and frequency of precipitation events. One strategy to lessen vulnerability is to quantify probable changes and adapt to them. Techniques such as log-normal, normal, and Gumbel are used (EV-I). Distributions were created with durations of 1, 2, 3, 6, and 24 h and return times of 2, 5, 10, 25, and 100 years. There were also mathematical correlations discovered between rainfall and recurrence interval.
Findings: Based on findings, the Gumbel approach produced the highest intensity values, whereas the other approaches produced values that were close to each other. The data indicates that 461.9 mm of rain fell during the monsoon season’s 301st week. However, it was found that the 29th week had the greatest average rainfall, 92.6 mm. With 952.6 mm on average, the monsoon season saw the highest rainfall. Calculations revealed that the yearly rainfall averaged 1171.1 mm. Using Weibull’s method, the study was subsequently expanded to examine rainfall distribution at different recurrence intervals of 2, 5, 10, and 25 years. Rainfall and recurrence interval mathematical correlations were also developed. Further regression analysis revealed that short wave irrigation, wind direction, wind speed, pressure, relative humidity, and temperature all had a substantial influence on rainfall.
Originality and value: The results of the rainfall IDF curves can provide useful information to policymakers in making appropriate decisions in managing and minimizing floods in the study area.
Data Structures and Agorithm: DS 04 Linked List.pptx
1. Data Structure
Lecture No. 4
Linked List
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
International Islamic University H-10, Islamabad, Pakistan
Video Lecture
2. Linked List
head
2 6 8 7
current
1 NULL
0x100 6
0x108
2
0x10C
0x110
0x114 7
0x118
0x11C 8
0x120
1
0x124
0x128 0x000
0x12C 0x11C
0x130
head
current
2
6
8 7 1
0x10C
0x100
0x11C
0x114
0x100
3. push_front (int Data){
node *newNode ;
// add new node
newNode = new node ;
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
Linked List Operations
head
2 6 1 NULL
head
NULL
NULL 9
newNode
newNode
9
Data = 9
4. void push_back ( int Data ){
node *current, *newNode ;
// list is empty then create first node
if ( head == NULL ){
head = new node ;
head -> data = Data ;
head -> next = NULL ;
}
else{ // go to last node
current = head ;
while ( current -> next != NULL )
current = current -> next ;
newNode = new node ;
newNode -> data = Data ;
newNode -> next = NULL ;
current -> next = newNode ;
}
}
Linked List Operations
head
current
2 6 1 NULL
head
newNode
NULL
NULL
9
9
NULL
Data = 9
5. void addafter(int loc, int Data){
node *current = head, *newNode ;
// skip to desired portion
for ( int i = 0 ; i < loc ; i++ ){
current = current -> next ;
// if reached at end of linked list
if ( current == NULL ){
cout << "nThere are less than "
<< loc << " elements in list"
<< endl ;
return ;
}
}
newNode = new node ; // insert new node
newNode -> data = Data ;
newNode -> next = current -> next ;
current -> next = newNode ;
}
Linked List Operations
current
2 6 1 NULL
head
9
newNode
i = 0
1
loc = 1 Data = 9
6. void del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
delete current ;
return ;
}
else{
previous = current ;
current = current -> next ;
}
}
cout << "nnElement " << Data << " not found" ;
}
Linked List Operations
head
NULL
1
current
previous
Data = 1
7. void del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
delete current ;
return ;
}
else{
previous = current ;
current = current -> next ;
}
}
cout << "nnElement " << Data << " not found" ;
}
Linked List Operations
current
2 6
head
9 NULL
1
previous
previous
Data = 1
8. #include <iostream>
using namespace std;
class Linked_List{
private :
struct node{
int data ;
node * next ;
} *head ;
public :
Linked_List ( ) ;
void push_back ( int Data ) ;
void push_front ( int Data ) ;
void addafter ( int loc, int Data ) ;
void display ( ) ;
int count ( ) ;
void del ( int num ) ;
~Linked_List( ) ;
} ;
8
Example 1: Linked List
Linked_List :: Linked_List( ){
head = NULL ;
}
// adds node at the end of linked list
void Linked_List :: push_back ( int Data ){
node *current, *newNode ;
// list is empty then create first node
if ( head == NULL ){
head = new node ;
head -> data = Data ;
head -> next = NULL ;
}
else{ // go to last node
current = head ;
while ( current -> next != NULL )
current = current -> next ;
// add node at the end
1 2
9. newNode = new node ;
newNode -> data = Data ;
newNode -> next = NULL ;
current -> next = newNode ;
}
}
// adds a node at the beginning
void Linked_List :: push_front (int Data){
node *newNode ;
// add new node
newNode = new node ;
newNode -> data = Data ;
newNode -> next = head ;
head = newNode ;
}
// adds node after numbers of nodes
void Linked_List::addafter(int loc,
int num){
9
Example 1: Linked List
node *current, *newNode ;
current = head ;
// skip to desired portion
for ( int i = 0 ; i < loc ; i++ ){
current = current -> next ;
// if reached at end of linked list
if ( current == NULL ){
cout << "nThere are less than "
<< loc << " elements in list"
<< endl ;
return ;
}
}
newNode = new node ; // insert new node
newNode -> data = num ;
newNode -> next = current -> next ;
current -> next = newNode ;
}
3 4
10. // displays the contents of the link list
void Linked_List :: display( ){
node *current = head ;
cout << endl ;
// traverse the entire linked list
while ( current != NULL ){
cout << current -> data << " " ;
current = current -> next ;
}
}
// counts the number of nodes present
int Linked_List :: count( ){
node *current = head ; int c = 0 ;
// traverse the entire linked list
while ( current != NULL ){
current = current -> next ;
c++ ;
}
return c ; }
10
Example 1: Linked List
// deletes the specified node
void Linked_List :: del ( int Data ) {
node *previous, *current ;
current = head ;
while ( current != NULL ){
if ( current -> data == Data ){
// if node to be deleted is the
// first node in the linked list
if ( current == head )
head = current -> next ;
else
previous->next = current->next;
// free memory occupied by node
delete current ;
return ;
}
// traverse the linked list till
// the last node is reached
5 6
11. else{
previous = current ;
// go to the next node
current = current -> next ;
}
}
cout << "nnElement " << Data
<< " not found" ;
}
// deallocates memory
Linked_List :: ~Linked_List( ){
while ( head != NULL ){
node *current = head;
head = head -> next ;
delete current ;
}
}
11
Example 1: Linked List
int main( ) {
Linked_List l ;
l.push_back (14) ; l.push_back (30) ;
l.push_back (25) ; l.push_back (42) ;
l.push_back (17) ;
cout <<"nData in the linked list: ";
l.display( ) ;
l.push_front (999) ; l.push_front (888) ;
l.push_front (777) ;
cout << "nnData in the linked list "
" after addition at the "
" beginning: " ;
l.display( ) ;
l.addafter (7, 0); l.addafter (2,1);
l.addafter (5,99);
cout << "nnData in the linked list"
" after addition at given"
" position: " ;
7 8
12. l.display( ) ;
cout <<"nNo. of elements in the "
" linked list " << l.count( );
l.del (99) ;
l.del ( 1) ;
l.del (10) ;
cout << "nnData in the linked list"
" after deletion: " ;
l.display( ) ;
cout <<"nNo. of elements in the"
" linked list: " << l.count();
system("PAUSE");
return 0;
}
12
Example 1: Linked List
9 10
14. push_front()
we simply insert the new node after the current node. So add is a one-step operation.
push_back()
we have to traverse the entire list to insert the new node at the end of Linked List.
del()
We first search the node to be deleted.
worst-case: may have to search the entire list
Moving forward in a singly-linked list is easy but moving backwards is not so
easy.
Moving backwards requires traversing the list from the start until the node
whose next pointer points to current node.
Analysis of Linked List
15. Dynamic memory allocation : We use linked list of free blocks.
Implementation of stacks and queues
Maintaining directory of names.
Performing arithmetic operations on long integers
Addition and Multiplication of Polynomials.
Implementation of Graphs. (Adjacency list representation of Graph).
Representing sparse matrices.
For separate chaining in Hashtables.
Undo functionality in Photoshop or Word . Linked list of states.
Graph Plotting Application.
Applications of Linked List
16. In single linked list, every node points to its next node in the sequence and the
last node points to NULL.
But in circular linked list, every node points to its next node in the sequence
but the last node points to the first node in the list.
Circular Linked List
17. The real life application where the circular linked list is used is our Personal
Computers, where multiple applications are running. All the running
applications are kept in a circular linked list and the OS gives a fixed time slot
to all for running. The Operating System keeps on iterating over the linked list
until all the applications are completed.
Another example can be Multiplayer games. All the Players are kept in a
Circular Linked List and the pointer keeps on moving forward as a player's
chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we
have to keep two pointers, FRONT and REAR in memory all the time, where as
in Circular Linked List, only one pointer is required.
Applications of Circular Linked List