"linked list" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "linked list" answered properly. Developers are finding an appropriate answer about linked list related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like linked list. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on linked list.
linkedlist implementation in c++
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
return 0;
}
linked list in python
class Node:
def __init__(self, data = None, next_node = None):
self.data = data
self.nextNode = next_node
def get_data(self):
return self.data
def set_data(self, data):
self.data = data
def get_nextNode(self):
return self.nextNode
def set_nextNode(self, nextNode):
self.nextNode = nextNode
class LinkedList:
def __init__(self, head = None):
self.head = head
def add_Node(self, data):
# if empty
if self.head == None:
self.head = Node(data)
# not empty
else:
curr_Node = self.head
# if node added is at the start
if data < curr_Node.get_data():
self.head = Node(data, curr_Node)
# not at start
else:
while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
prev_Node = curr_Node
curr_Node = curr_Node.get_nextNode()
# if node added is at the middle
if data < curr_Node.get_data():
prev_Node.set_nextNode(Node(data, curr_Node))
# if node added is at the last
elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
curr_Node.set_nextNode(Node(data))
def search(self, data):
curr_Node = self.head
while curr_Node != None:
if data == curr_Node.get_data():
return True
else:
curr_Node = curr_Node.get_nextNode()
return False
def delete_Node(self, data):
if self.search(data):
# if data is found
curr_Node = self.head
#if node to be deleted is the first node
if curr_Node.get_data() == data:
self.head = curr_Node.get_nextNode()
else:
while curr_Node.get_data() != data:
prev_Node = curr_Node
curr_Node = curr_Node.get_nextNode()
#node to be deleted is middle
if curr_Node.get_nextNode() != None:
prev_Node.set_nextNode(curr_Node.get_nextNode())
# node to be deleted is at the end
elif curr_Node.get_nextNode() == None:
prev_Node.set_nextNode(None)
else:
return "Not found."
def return_as_lst(self):
lst = []
curr_Node = self.head
while curr_Node != None:
lst.append(curr_Node.get_data())
curr_Node = curr_Node.get_nextNode()
return lst
def size(self):
curr_Node = self.head
count = 0
while curr_Node:
count += 1
curr_Node = curr_Node.get_nextNode()
return count
## TEST CASES #
test1 = LinkedList()
test2 = LinkedList()
test1.add_Node(20)
test1.add_Node(15)
test1.add_Node(13)
test1.add_Node(14)
test1.delete_Node(17)
print(test1.return_as_lst())
print(test2.size())
Linked List implementation
public class LinkedList {
private Node head;
private int length = 0;
public LinkedList() {
this.head = new Node(null);
}
public int size() {
return length;
}
// Adds an element to the end of the list
public void add(Object data) {
Node node = new Node(data);
Node iterator = head;
while (iterator.getNext() != null){
iterator = iterator.getNext();
}
iterator.setNext(node);
length++;
}
// Obtains an element by index
public Object get(int index) {
if (head.getNext() == null || index >= length){
return null;
}
Node iterator = head.getNext();
int counter = 0;
while(counter < index){
iterator = iterator.getNext();
counter++;
}
return iterator.getData();
}
// Returns the index of the element in the list
public int indexOf(Object data) {
Node obj=head;
for (int i = 0; i < length; i++) {
obj = obj.getNext();
if (obj.getData().equals(data)) {
return i;
}
}
return -1;
//throw new Exception("Data not found");
}
// Removes an element from the list
public boolean remove(Object data) {
if (head.getNext() == null){
return false;
}
Node iterator = head;
while(iterator.getNext() != null){
if (iterator.getNext().getData().equals(data)){
iterator.setNext(iterator.getNext().getNext());
length--;
return true;
}
iterator = iterator.getNext();
}
return false;
}
private class Node {
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
next = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
linked lists
node_t * head = NULL;
head = (node_t *) malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->val = 1;
head->next = NULL;
Source: www.learn-c.org
All those coders who are working on the C++ based application and are stuck on linked list can get a collection of related answers to their query. Programmers need to enter their query on linked list related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about linked list for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of linked list while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "linked list". Visit this developer's friendly online web community, CodeProZone, and get your queries like linked list resolved professionally and stay updated to the latest C++ updates.