1#include <iostream>
2
3using namespace std;
4
5struct node
6{
7 int data;
8 node *next;
9};
10
11class linked_list
12{
13private:
14 node *head,*tail;
15public:
16 linked_list()
17 {
18 head = NULL;
19 tail = NULL;
20 }
21
22 void add_node(int n)
23 {
24 node *tmp = new node;
25 tmp->data = n;
26 tmp->next = NULL;
27
28 if(head == NULL)
29 {
30 head = tmp;
31 tail = tmp;
32 }
33 else
34 {
35 tail->next = tmp;
36 tail = tail->next;
37 }
38 }
39};
40
41int main()
42{
43 linked_list a;
44 a.add_node(1);
45 a.add_node(2);
46 return 0;
47}
1class ListNode:
2 def __init__(self, value, prev=None, next=None):
3 self.prev = prev
4 self.value = value
5 self.next = next
6
7class DoublyLinkedList:
8 def __init__(self, node=None):
9 self.head = node
10 self.tail = node
11 self.length = 1 if node is not None else 0
12
13 def __len__(self):
14 return self.length
15
16 def add_to_head(self, value):
17 new_node = ListNode(value, None, None)
18 self.length += 1
19 if not self.head and not self.tail:
20 self.head = new_node
21 self.tail = new_node
22 else:
23 new_node.next = self.head
24 self.head.prev = new_node
25 self.head = new_node
26
27 def remove_from_head(self):
28 value = self.head.value
29 self.delete(self.head)
30 return value
31
32 def add_to_tail(self, value):
33 new_node = ListNode(value, None, None)
34 self.length += 1
35 if not self.tail and not self.head:
36 self.tail = new_node
37 self.head = new_node
38 else:
39 new_node.prev = self.tail
40 self.tail.next = new_node
41 self.tail = new_node
42
43
44 def remove_from_tail(self):
45 value = self.tail.value
46 self.delete(self.tail)
47 return value
48
49 def move_to_front(self, node):
50 if node is self.head:
51 return
52 value = node.value
53 if node is self.tail:
54 self.remove_from_tail()
55 else:
56 node.delete()
57 self.length -= 1
58 self.add_to_head(value)
59
60 def move_to_end(self, node):
61 if node is self.tail:
62 return
63 value = node.value
64 if node is self.head:
65 self.remove_from_head()
66 self.add_to_tail(value)
67 else:
68 node.delete()
69 self.length -= 1
70 self.add_to_tail(value)
71
72 def delete(self, node):
73 self.length -= 1
74 if not self.head and not self.tail:
75 return
76 if self.head == self.tail:
77 self.head = None
78 self.tail = None
79 elif self.head == node:
80 self.head = node.next
81 node.delete()
82 elif self.tail == node:
83 self.tail = node.prev
84 node.delete()
85 else:
86 node.delete()
87
88 def get_max(self):
89 if not self.head:
90 return None
91 max_val = self.head.value
92 current = self.head
93 while current:
94 if current.value > max_val:
95 max_val = current.value
96 current = current.next
97 return max_val
1// Java code to illustrate listIterator()
2import java.io.*;
3import java.util.LinkedList;
4import java.util.ListIterator;
5
6public class LinkedListDemo {
7 public static void main(String args[])
8 {
9 // Creating an empty LinkedList
10 LinkedList<String> list = new LinkedList<String>();
11
12 // Use add() method to add elements in the list
13 list.add("Geeks");
14 list.add("for");
15 list.add("Geeks");
16 list.add("10");
17 list.add("20");
18
19 // Displaying the linkedlist
20 System.out.println("LinkedList:" + list);
21
22 // Setting the ListIterator at a specified position
23 ListIterator list_Iter = list.listIterator(2);
24
25 // Iterating through the created list from the position
26 System.out.println("The list is as follows:");
27 while(list_Iter.hasNext()){
28 System.out.println(list_Iter.next());
29 }
30 }
31}
1
2#include <bits/stdc++.h>
3#include <iostream>
4#include <list>
5#include <iterator>
6
7#define ll long long
8
9using namespace std;
10
11//function to print all the elements of the linked list
12void showList(list <int> l){
13 list <int> :: iterator it; //create an iterator according to the data structure
14 for(it = l.begin(); it != l.end(); it++){
15 cout<<*it<<" ";
16 }
17
18}
19
20
21int main(){
22
23 list <int> l1;
24 list <int> l2;
25
26 for(int i=0; i<10; i++){
27 l1.push_back(i*2); //fill list 1 with multiples of 2
28 l2.push_back(i*3); //fill list 2 with multiples of 3
29 }
30
31 cout<<"content of list 1 is "<<endl;
32 showList(l1);
33 cout<<endl;
34
35 cout<<"content of list 2 is "<<endl;
36 showList(l2);
37 cout<<endl;
38
39 //reverse the first list
40 l1.reverse();
41 showList(l1);
42 cout<<endl;
43
44 //sort the first list
45 l1.sort();
46 showList(l1);
47 cout<<endl;
48
49 //removing an element from both sides
50 l2.pop_front();
51 l2.pop_back();
52
53 //adding an element from both sides
54 l2.push_back(10);
55 l2.push_front(20);
56
57
58 return 0;
59}
1/**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9
10void trimLeftTrailingSpaces(string &input) {
11 input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
12 return !isspace(ch);
13 }));
14}
15
16void trimRightTrailingSpaces(string &input) {
17 input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
18 return !isspace(ch);
19 }).base(), input.end());
20}
21
22vector<int> stringToIntegerVector(string input) {
23 vector<int> output;
24 trimLeftTrailingSpaces(input);
25 trimRightTrailingSpaces(input);
26 input = input.substr(1, input.length() - 2);
27 stringstream ss;
28 ss.str(input);
29 string item;
30 char delim = ',';
31 while (getline(ss, item, delim)) {
32 output.push_back(stoi(item));
33 }
34 return output;
35}
36
37ListNode* stringToListNode(string input) {
38 // Generate list from the input
39 vector<int> list = stringToIntegerVector(input);
40
41 // Now convert that list into linked list
42 ListNode* dummyRoot = new ListNode(0);
43 ListNode* ptr = dummyRoot;
44 for(int item : list) {
45 ptr->next = new ListNode(item);
46 ptr = ptr->next;
47 }
48 ptr = dummyRoot->next;
49 delete dummyRoot;
50 return ptr;
51}
52
53void prettyPrintLinkedList(ListNode* node) {
54 while (node && node->next) {
55 cout << node->val << "->";
56 node = node->next;
57 }
58
59 if (node) {
60 cout << node->val << endl;
61 } else {
62 cout << "Empty LinkedList" << endl;
63 }
64}
65
66int main() {
67 string line;
68 while (getline(cin, line)) {
69 ListNode* head = stringToListNode(line);
70 prettyPrintLinkedList(head);
71 }
72 return 0;
73}