xxxxxxxxxx
package il.ac.telhai.ds.stack;
public class DLinkedList<T> implements List<T> {
DNode head;
DNode cursor;
DNode tail;
public DLinkedList() {
head = null;
cursor = null;
tail = null;
}
//insert function inserts a new element
//the function checks if the list is empty and acts accordingly
public void insert(T newElement) {
if (newElement==null) throw new RuntimeException();
DNode mynote = new DNode(newElement);
//if is empty
if (head == null)
{
head = mynote;
head.next = null;
head.prev = null;
cursor = head;
tail = head;
return;
}
//if not empty
mynote.setPrev(cursor);
mynote.setNext(cursor.getNext());
if (mynote.getNext()!=null) mynote.getNext().setPrev(mynote);
cursor.setNext(mynote);
if(mynote.getNext()==null) tail=mynote;
cursor = mynote;
}
@Override
//removes an item
//when removing an item i distinguish beetwen 3 option:
//first option : cursor points to tail and and tail is not head
//second option : cursor points to head and its the only item
//third option : cursor point to head and it is not the only item
//fourth option: cursor points not to head and not to tail
public T remove() {
if (isEmpty()) return null;
T element = cursor.getElement();
//first
if (cursor==tail && cursor!=head){
cursor.getPrev().setNext(null);
cursor = cursor.getPrev();
tail = cursor;
return element;
}
//if cursor is on head
if (cursor==head )
{
//second
if ( cursor.getNext()==null)
{
clear();
return element;
}
//third
cursor.getNext().setPrev(null);
cursor =cursor.getNext();
head = cursor;
return element;
}
//fourth option
cursor.getPrev().setNext(cursor.getNext());
cursor.getNext().setPrev(cursor.getPrev());
cursor = cursor.getNext();
return element;
}
xxxxxxxxxx
// C++ program to convert a given Binary Tree to Doubly Linked List
#include <bits/stdc++.h>
// Structure for tree and linked list
struct Node {
int data;
Node *left, *right;
};
// Utility function for allocating node for Binary
// Tree.
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
// A simple recursive function to convert a given
// Binary tree to Doubly Linked List
// root --> Root of Binary Tree
// head --> Pointer to head node of created doubly linked list
void BToDLL(Node* root, Node*& head)
{
// Base cases
if (root == NULL)
return;
// Recursively convert right subtree
BToDLL(root->right, head);
// insert root into DLL
root->right = head;
// Change left pointer of previous head
if (head != NULL)
head->left = root;
// Change head of Doubly linked list
head = root;
// Recursively convert left subtree
BToDLL(root->left, head);
}
// Utility function for printing double linked list.
void printList(Node* head)
{
printf("Extracted Double Linked list is:\n");
while (head) {
printf("%d ", head->data);
head = head->right;
}
}
// Driver program to test above function
int main()
{
/* Constructing below tree
5
/ \
3 6
/ \ \
1 4 8
/ \ / \
0 2 7 9 */
Node* root = newNode(5);
root->left = newNode(3);
root->right = newNode(6);
root->left->left = newNode(1);
root->left->right = newNode(4);
root->right->right = newNode(8);
root->left->left->left = newNode(0);
root->left->left->right = newNode(2);
root->right->right->left = newNode(7);
root->right->right->right = newNode(9);
Node* head = NULL;
BToDLL(root, head);
printList(head);
return 0;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
class Node {
public:
string name;
string surName;
int age;
string gender;
Node* next;
};
void Insert(Node** head, Node* newNode)
{
Node* currentNode;
if (*head == NULL|| (*head)->age >= newNode->age) {
newNode->next = *head;
*head = newNode;
}
else {
currentNode = *head;
while (currentNode->next != NULL && currentNode->next->age< newNode->age) {
currentNode = currentNode->next;
}
newNode->next = currentNode->next;
currentNode->next = newNode;
}
}
Node* new_node(string name,string surName, int age, string gender)
{
Node* newNode = new Node();
newNode->name = name;
newNode->surName = surName;
newNode->age = age;
newNode->gender = gender;
newNode->next = NULL;
return newNode;
}
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->name << " "<<temp->surName<<" "<<temp->age<<" "<<temp->gender<<endl;
temp = temp->next;
}
}
void deleteNode(Node** head_ref, int key)
{
Node* temp = *head_ref;
Node* prev = NULL;
if (temp != NULL && temp->age == key)
{
*head_ref = temp->next;
delete temp;
return;
}
else
{
while (temp != NULL && temp->age != key)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;
}
}
int main()
{
Node* head = NULL;
Node* node = new_node("Donald", "Brown", 67, "M" );
Insert(&head, node);
node = new_node("Jason", "Travis", 90, "F");
Insert(&head, node);
node = new_node("Max", "Black", 27, "M");
Insert(&head, node);
node = new_node("Bobi", "Frank", 17, "F");
Insert(&head, node);
cout << "The list is sorted by gender in ascending order\n";
printList(head);
cout<<"After deleting a person who age is 17 the list becomes \n";
deleteNode(&head, 17);
printList(head);
cout << "When a person whose is 20 is inserted the linked list becomes\n";
node = new_node("Victoria", "Riberry", 20, "M");
Insert(&head, node);
printList(head);
return 0;
}
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
class node {
public:
int num;
node* next;
node(int val) {
num = val;
next = NULL;
}
};
void insertNode(node* head,int val) {
node* newNode = new node(val);
if(head == NULL) {
head = newNode;
return;
}
node* temp = head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
return;
}
node* reverse(node* ptr) {
node* pre=NULL;
node* nex=NULL;
while(ptr!=NULL) {
nex = ptr->next;
ptr->next = pre;
pre=ptr;
ptr=nex;
}
return pre;
}
bool isPalindrome(node* head) {
if(head==NULL||head->next==NULL) return true;
node* slow = head;
node* fast = head;
while(fast->next!=NULL&&fast->next->next!=NULL) {
slow = slow->next;
fast = fast->next->next;
}
slow->next = reverse(slow->next);
slow = slow->next;
node* dummy = head;
while(slow!=NULL) {
if(dummy->num != slow->num) return false;
dummy = dummy->next;
slow = slow->next;
}
return true;
}
int main() {
node* head = NULL;
insertNode(head,1);
insertNode(head,2);
insertNode(head,3);
insertNode(head,2);
insertNode(head,1);
isPalindrome(head)? cout<<"True" : cout<<"False";
return 0;
}
xxxxxxxxxx
# A simple Python program to introduce a linked list
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class contains a Node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Code execution starts here
if __name__=='__main__':
# Start with the empty list
llist = LinkedList()
llist.head = Node(1)
second = Node(2)
third = Node(3)
'''
Three nodes have been created.
We have references to these three blocks as head,
second and third
llist.head second third
| | |
| | |
+----+------+ +----+------+ +----+------+
| 1 | None | | 2 | None | | 3 | None |
+----+------+ +----+------+ +----+------+
'''
llist.head.next = second; # Link first node with second
'''
Now next of first Node refers to second. So they
both are linked.
llist.head second third
| | |
| | |
+----+------+ +----+------+ +----+------+
| 1 | o-------->| 2 | null | | 3 | null |
+----+------+ +----+------+ +----+------+
'''
second.next = third; # Link second node with the third node
'''
Now next of second Node refers to third. So all three
nodes are linked.
llist.head second third
| | |
| | |
+----+------+ +----+------+ +----+------+
| 1 | o-------->| 2 | o-------->| 3 | null |
+----+------+ +----+------+ +----+------+
'''
xxxxxxxxxx
node_t * head = NULL;
head = (node_t *) malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->val = 1;
head->next = NULL;
xxxxxxxxxx
package stack
// Node structure
type Node struct {
Val interface{}
Next *Node
}
// Stack has jost top of node and with length
type Stack struct {
top *Node
length int
}
// push add value to last index
func (ll *Stack) push(n interface{}) {
newStack := &Node{} // new node
newStack.Val = n
newStack.Next = ll.top
ll.top = newStack
ll.length++
}
// pop remove last item as first output
func (ll *Stack) pop() interface{} {
result := ll.top.Val
if ll.top.Next == nil {
ll.top = nil
} else {
ll.top.Val, ll.top.Next = ll.top.Next.Val, ll.top.Next.Next
}
ll.length--
return result
}
// isEmpty to check our array is empty or not
func (ll *Stack) isEmpty() bool {
return ll.length == 0
}
// len use to return length of our stack
func (ll *Stack) len() int {
return ll.length
}
// peak return last input value
func (ll *Stack) peak() interface{} {
return ll.top.Val
}
// show all value as an interface array
func (ll *Stack) show() (in []interface{}) {
current := ll.top
for current != nil {
in = append(in, current.Val)
current = current.Next
}
return
}