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 Node:
2 def __init__(self, data = None, next_node = None):
3 self.data = data
4 self.nextNode = next_node
5
6 def get_data(self):
7 return self.data
8
9 def set_data(self, data):
10 self.data = data
11
12 def get_nextNode(self):
13 return self.nextNode
14
15 def set_nextNode(self, nextNode):
16 self.nextNode = nextNode
17
18
19class LinkedList:
20 def __init__(self, head = None):
21 self.head = head
22
23
24 def add_Node(self, data):
25 # if empty
26 if self.head == None:
27 self.head = Node(data)
28
29
30 # not empty
31 else:
32 curr_Node = self.head
33
34 # if node added is at the start
35 if data < curr_Node.get_data():
36 self.head = Node(data, curr_Node)
37
38 # not at start
39 else:
40 while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
41 prev_Node = curr_Node
42 curr_Node = curr_Node.get_nextNode()
43
44 # if node added is at the middle
45 if data < curr_Node.get_data():
46 prev_Node.set_nextNode(Node(data, curr_Node))
47
48
49 # if node added is at the last
50 elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
51 curr_Node.set_nextNode(Node(data))
52
53
54
55 def search(self, data):
56 curr_Node = self.head
57 while curr_Node != None:
58 if data == curr_Node.get_data():
59 return True
60
61 else:
62 curr_Node = curr_Node.get_nextNode()
63
64 return False
65
66
67 def delete_Node(self, data):
68 if self.search(data):
69 # if data is found
70
71 curr_Node = self.head
72 #if node to be deleted is the first node
73 if curr_Node.get_data() == data:
74 self.head = curr_Node.get_nextNode()
75
76 else:
77 while curr_Node.get_data() != data:
78 prev_Node = curr_Node
79 curr_Node = curr_Node.get_nextNode()
80
81 #node to be deleted is middle
82 if curr_Node.get_nextNode() != None:
83 prev_Node.set_nextNode(curr_Node.get_nextNode())
84
85 # node to be deleted is at the end
86 elif curr_Node.get_nextNode() == None:
87 prev_Node.set_nextNode(None)
88
89 else:
90 return "Not found."
91
92 def return_as_lst(self):
93 lst = []
94 curr_Node = self.head
95 while curr_Node != None:
96 lst.append(curr_Node.get_data())
97 curr_Node = curr_Node.get_nextNode()
98
99 return lst
100
101 def size(self):
102 curr_Node = self.head
103 count = 0
104 while curr_Node:
105 count += 1
106 curr_Node = curr_Node.get_nextNode()
107 return count
108
109
110## TEST CASES #
111test1 = LinkedList()
112test2 = LinkedList()
113test1.add_Node(20)
114test1.add_Node(15)
115test1.add_Node(13)
116test1.add_Node(14)
117test1.delete_Node(17)
118print(test1.return_as_lst())
119print(test2.size())
1public class LinkedList {
2
3 private Node head;
4 private int length = 0;
5
6 public LinkedList() {
7 this.head = new Node(null);
8 }
9
10 public int size() {
11 return length;
12 }
13
14
15 // Adds an element to the end of the list
16 public void add(Object data) {
17
18 Node node = new Node(data);
19 Node iterator = head;
20 while (iterator.getNext() != null){
21 iterator = iterator.getNext();
22 }
23 iterator.setNext(node);
24 length++;
25 }
26
27
28 // Obtains an element by index
29 public Object get(int index) {
30
31 if (head.getNext() == null || index >= length){
32 return null;
33 }
34
35 Node iterator = head.getNext();
36 int counter = 0;
37
38 while(counter < index){
39
40 iterator = iterator.getNext();
41 counter++;
42 }
43 return iterator.getData();
44
45 }
46
47
48 // Returns the index of the element in the list
49 public int indexOf(Object data) {
50 Node obj=head;
51 for (int i = 0; i < length; i++) {
52 obj = obj.getNext();
53 if (obj.getData().equals(data)) {
54 return i;
55 }
56 }
57 return -1;
58 //throw new Exception("Data not found");
59 }
60
61
62 // Removes an element from the list
63 public boolean remove(Object data) {
64
65 if (head.getNext() == null){
66 return false;
67 }
68
69 Node iterator = head;
70
71 while(iterator.getNext() != null){
72
73 if (iterator.getNext().getData().equals(data)){
74 iterator.setNext(iterator.getNext().getNext());
75 length--;
76 return true;
77 }
78
79 iterator = iterator.getNext();
80 }
81
82 return false;
83 }
84
85 private class Node {
86
87 private Object data;
88 private Node next;
89
90 public Node(Object data) {
91 this.data = data;
92 next = null;
93 }
94
95 public Object getData() {
96 return data;
97 }
98
99 public void setData(Object data) {
100 this.data = data;
101 }
102
103 public Node getNext() {
104 return next;
105 }
106
107 public void setNext(Node next) {
108 this.next = next;
109 }
110 }
111
112}
1class Node:
2 def __init__(self, data):
3 self.data = data
4 self.next = None
5
6 def __repr__(self):
7 return self.data
8
9class LinkedList:
10 def __init__(self):
11 self.head = None
12
13 def __repr__(self):
14 node = self.head
15 nodes = []
16 while node is not None:
17 nodes.append(node.data)
18 node = node.next
19 nodes.append("None")
20 return " -> ".join(nodes)
21
1
2
3#include <iostream>
4using namespace std;
5
6
7
8class Node {
9public:
10 string name;
11 string surName;
12 int age;
13 string gender;
14 Node* next;
15};
16
17void Insert(Node** head, Node* newNode)
18{
19 Node* currentNode;
20
21 if (*head == NULL|| (*head)->age >= newNode->age) {
22 newNode->next = *head;
23 *head = newNode;
24 }
25 else {
26
27 currentNode = *head;
28 while (currentNode->next != NULL && currentNode->next->age< newNode->age) {
29 currentNode = currentNode->next;
30 }
31 newNode->next = currentNode->next;
32 currentNode->next = newNode;
33 }
34}
35
36Node* new_node(string name,string surName, int age, string gender)
37{
38
39 Node* newNode = new Node();
40
41
42 newNode->name = name;
43 newNode->surName = surName;
44 newNode->age = age;
45 newNode->gender = gender;
46 newNode->next = NULL;
47
48 return newNode;
49}
50
51
52
53void printList(Node* head)
54{
55 Node* temp = head;
56 while (temp != NULL) {
57 cout << temp->name << " "<<temp->surName<<" "<<temp->age<<" "<<temp->gender<<endl;
58 temp = temp->next;
59 }
60}
61
62
63void deleteNode(Node** head_ref, int key)
64{
65
66
67 Node* temp = *head_ref;
68 Node* prev = NULL;
69
70
71 if (temp != NULL && temp->age == key)
72 {
73 *head_ref = temp->next;
74 delete temp;
75 return;
76 }
77
78
79 else
80 {
81 while (temp != NULL && temp->age != key)
82 {
83 prev = temp;
84 temp = temp->next;
85 }
86
87
88 if (temp == NULL)
89 return;
90
91
92 prev->next = temp->next;
93
94
95 delete temp;
96 }
97}
98
99
100int main()
101{
102
103 Node* head = NULL;
104 Node* node = new_node("Donald", "Brown", 67, "M" );
105 Insert(&head, node);
106 node = new_node("Jason", "Travis", 90, "F");
107 Insert(&head, node);
108 node = new_node("Max", "Black", 27, "M");
109 Insert(&head, node);
110 node = new_node("Bobi", "Frank", 17, "F");
111 Insert(&head, node);
112
113 cout << "The list is sorted by gender in ascending order\n";
114 printList(head);
115 cout<<"After deleting a person who age is 17 the list becomes \n";
116 deleteNode(&head, 17);
117 printList(head);
118
119 cout << "When a person whose is 20 is inserted the linked list becomes\n";
120 node = new_node("Victoria", "Riberry", 20, "M");
121 Insert(&head, node);
122 printList(head);
123 return 0;
124}
1node_t * head = NULL;
2head = (node_t *) malloc(sizeof(node_t));
3if (head == NULL) {
4 return 1;
5}
6
7head->val = 1;
8head->next = NULL;
9