From d681e0970bca7328dc03ccee0100b843427887b9 Mon Sep 17 00:00:00 2001 From: yashaswiadyalu Date: Sun, 18 Oct 2020 20:05:46 +0530 Subject: [PATCH 1/6] code for circulardoublylinkedlist --- .../linked-lists/circulardoublylinkedlist.py | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 data-structures/linked-lists/circulardoublylinkedlist.py diff --git a/data-structures/linked-lists/circulardoublylinkedlist.py b/data-structures/linked-lists/circulardoublylinkedlist.py new file mode 100644 index 0000000..eef2769 --- /dev/null +++ b/data-structures/linked-lists/circulardoublylinkedlist.py @@ -0,0 +1,109 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + self.prev = None + + +class CircularDoublyLinkedList: + def __init__(self): + self.first = None + + def get_node(self, index): + current = self.first + for i in range(index): + current = current.next + if current == self.first: + return None + return current + + def insert_after(self, ref_node, new_node): + new_node.prev = ref_node + new_node.next = ref_node.next + new_node.next.prev = new_node + ref_node.next = new_node + + def insert_before(self, ref_node, new_node): + self.insert_after(ref_node.prev, new_node) + + def insert_at_end(self, new_node): + if self.first is None: + self.first = new_node + new_node.next = new_node + new_node.prev = new_node + else: + self.insert_after(self.first.prev, new_node) + + def insert_at_beg(self, new_node): + self.insert_at_end(new_node) + self.first = new_node + + def remove(self, node): + if self.first.next == self.first: + self.first = None + else: + node.prev.next = node.next + node.next.prev = node.prev + if self.first == node: + self.first = node.next + + def display(self): + if self.first is None: + return + current = self.first + while True: + print(current.data, end = ' ') + current = current.next + if current == self.first: + break + + +a_cdllist = CircularDoublyLinkedList() + +print('Menu') +print('insert after ') +print('insert before ') +print('insert at beg') +print('insert at end') +print('remove ') +print('quit') + +while True: + print('The list: ', end = '') + a_cdllist.display() + print() + do = input('What would you like to do? ').split() + + operation = do[0].strip().lower() + + if operation == 'insert': + data = int(do[1]) + position = do[3].strip().lower() + new_node = Node(data) + suboperation = do[2].strip().lower() + if suboperation == 'at': + if position == 'beg': + a_cdllist.insert_at_beg(new_node) + elif position == 'end': + a_cdllist.insert_at_end(new_node) + else: + index = int(position) + ref_node = a_cdllist.get_node(index) + if ref_node is None: + print('No such index.') + continue + if suboperation == 'after': + a_cdllist.insert_after(ref_node, new_node) + elif suboperation == 'before': + a_cdllist.insert_before(ref_node, new_node) + + elif operation == 'remove': + index = int(do[1]) + node = a_cdllist.get_node(index) + if node is None: + print('No such index.') + continue + a_cdllist.remove(node) + + elif operation == 'quit': + break \ No newline at end of file From f05173438f6340e07af916fa77cf5a88ffcdda8e Mon Sep 17 00:00:00 2001 From: yashaswiadyalu Date: Sun, 18 Oct 2020 20:49:05 +0530 Subject: [PATCH 2/6] code for dequeue --- data-structures/dequeue.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 data-structures/dequeue.py diff --git a/data-structures/dequeue.py b/data-structures/dequeue.py new file mode 100644 index 0000000..884c698 --- /dev/null +++ b/data-structures/dequeue.py @@ -0,0 +1,48 @@ +class Dequeue: + def __init__(self): + self.items = [] + + def is_empty(self): + return self.items == [] + + def append(self, data): + self.items.append(data) + + def append_left(self, data): + self.items.insert(0, data) + + def pop(self): + return self.items.pop() + + def pop_left(self): + return self.items.pop(0) + + +q = Dequeue() +print('Menu') +print('append ') +print('appendleft ') +print('pop') +print('popleft') +print('quit') + +while True: + do = input('What would you like to do? ').split() + + operation = do[0].strip().lower() + if operation == 'append': + q.append(int(do[1])) + elif operation == 'appendleft': + q.append_left(int(do[1])) + elif operation == 'pop': + if q.is_empty(): + print('Dequeue is empty.') + else: + print('Popped value from right: ', q.pop()) + elif operation == 'popleft': + if q.is_empty(): + print('Dequeue is empty.') + else: + print('Popped value from left: ', q.pop_left()) + elif operation == 'quit': + break \ No newline at end of file From ad7fd094659999374d9688337d4f036d134b1874 Mon Sep 17 00:00:00 2001 From: yashaswiadyalu Date: Sun, 18 Oct 2020 22:42:14 +0530 Subject: [PATCH 3/6] Insertion and deletion in cicrcular single linked list --- .../linked-lists/circularsinglelinkedlist.py | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 data-structures/linked-lists/circularsinglelinkedlist.py diff --git a/data-structures/linked-lists/circularsinglelinkedlist.py b/data-structures/linked-lists/circularsinglelinkedlist.py new file mode 100644 index 0000000..45fd10e --- /dev/null +++ b/data-structures/linked-lists/circularsinglelinkedlist.py @@ -0,0 +1,116 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class CircularLinkedList: + def __init__(self): + self.head = None + + def get_node(self, index): + if self.head is None: + return None + current = self.head + for i in range(index): + current = current.next + if current == self.head: + return None + return current + + def get_prev_node(self, ref_node): + if self.head is None: + return None + current = self.head + while current.next != ref_node: + current = current.next + return current + + def insert_after(self, ref_node, new_node): + new_node.next = ref_node.next + ref_node.next = new_node + + def insert_before(self, ref_node, new_node): + prev_node = self.get_prev_node(ref_node) + self.insert_after(prev_node, new_node) + + def insert_at_end(self, new_node): + if self.head is None: + self.head = new_node + new_node.next = new_node + else: + self.insert_before(self.head, new_node) + + def insert_at_beg(self, new_node): + self.insert_at_end(new_node) + self.head = new_node + + def remove(self, node): + if self.head.next == self.head: + self.head = None + else: + prev_node = self.get_prev_node(node) + prev_node.next = node.next + if self.head == node: + self.head = node.next + + def display(self): + if self.head is None: + return + current = self.head + while True: + print(current.data, end = ' ') + current = current.next + if current == self.head: + break + + +a_cllist = CircularLinkedList() + +print('Menu') +print('insert after ') +print('insert before ') +print('insert at beg') +print('insert at end') +print('remove ') +print('quit') + +while True: + print('The list: ', end = '') + a_cllist.display() + print() + do = input('What would you like to do? ').split() + + operation = do[0].strip().lower() + + if operation == 'insert': + data = int(do[1]) + position = do[3].strip().lower() + new_node = Node(data) + suboperation = do[2].strip().lower() + if suboperation == 'at': + if position == 'beg': + a_cllist.insert_at_beg(new_node) + elif position == 'end': + a_cllist.insert_at_end(new_node) + else: + index = int(position) + ref_node = a_cllist.get_node(index) + if ref_node is None: + print('No such index.') + continue + if suboperation == 'after': + a_cllist.insert_after(ref_node, new_node) + elif suboperation == 'before': + a_cllist.insert_before(ref_node, new_node) + + elif operation == 'remove': + index = int(do[1]) + node = a_cllist.get_node(index) + if node is None: + print('No such index.') + continue + a_cllist.remove(node) + + elif operation == 'quit': + break \ No newline at end of file From f12050990c064f20d27947084edd3ce57863efb6 Mon Sep 17 00:00:00 2001 From: Yashaswi A <59396277+yashaswiadyalu@users.noreply.github.com> Date: Sun, 18 Oct 2020 22:57:35 +0530 Subject: [PATCH 4/6] Delete circularsinglelinkedlist.py --- .../linked-lists/circularsinglelinkedlist.py | 116 ------------------ 1 file changed, 116 deletions(-) delete mode 100644 data-structures/linked-lists/circularsinglelinkedlist.py diff --git a/data-structures/linked-lists/circularsinglelinkedlist.py b/data-structures/linked-lists/circularsinglelinkedlist.py deleted file mode 100644 index 45fd10e..0000000 --- a/data-structures/linked-lists/circularsinglelinkedlist.py +++ /dev/null @@ -1,116 +0,0 @@ -class Node: - def __init__(self, data): - self.data = data - self.next = None - - -class CircularLinkedList: - def __init__(self): - self.head = None - - def get_node(self, index): - if self.head is None: - return None - current = self.head - for i in range(index): - current = current.next - if current == self.head: - return None - return current - - def get_prev_node(self, ref_node): - if self.head is None: - return None - current = self.head - while current.next != ref_node: - current = current.next - return current - - def insert_after(self, ref_node, new_node): - new_node.next = ref_node.next - ref_node.next = new_node - - def insert_before(self, ref_node, new_node): - prev_node = self.get_prev_node(ref_node) - self.insert_after(prev_node, new_node) - - def insert_at_end(self, new_node): - if self.head is None: - self.head = new_node - new_node.next = new_node - else: - self.insert_before(self.head, new_node) - - def insert_at_beg(self, new_node): - self.insert_at_end(new_node) - self.head = new_node - - def remove(self, node): - if self.head.next == self.head: - self.head = None - else: - prev_node = self.get_prev_node(node) - prev_node.next = node.next - if self.head == node: - self.head = node.next - - def display(self): - if self.head is None: - return - current = self.head - while True: - print(current.data, end = ' ') - current = current.next - if current == self.head: - break - - -a_cllist = CircularLinkedList() - -print('Menu') -print('insert after ') -print('insert before ') -print('insert at beg') -print('insert at end') -print('remove ') -print('quit') - -while True: - print('The list: ', end = '') - a_cllist.display() - print() - do = input('What would you like to do? ').split() - - operation = do[0].strip().lower() - - if operation == 'insert': - data = int(do[1]) - position = do[3].strip().lower() - new_node = Node(data) - suboperation = do[2].strip().lower() - if suboperation == 'at': - if position == 'beg': - a_cllist.insert_at_beg(new_node) - elif position == 'end': - a_cllist.insert_at_end(new_node) - else: - index = int(position) - ref_node = a_cllist.get_node(index) - if ref_node is None: - print('No such index.') - continue - if suboperation == 'after': - a_cllist.insert_after(ref_node, new_node) - elif suboperation == 'before': - a_cllist.insert_before(ref_node, new_node) - - elif operation == 'remove': - index = int(do[1]) - node = a_cllist.get_node(index) - if node is None: - print('No such index.') - continue - a_cllist.remove(node) - - elif operation == 'quit': - break \ No newline at end of file From ff3385a78f0ac242f87edea92712e94f620759b1 Mon Sep 17 00:00:00 2001 From: Yashaswi A <59396277+yashaswiadyalu@users.noreply.github.com> Date: Sun, 18 Oct 2020 23:02:24 +0530 Subject: [PATCH 5/6] insertion and deletion in circular SLL --- .../linked-lists/circularsinglelinkedlist.py | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 data-structures/linked-lists/circularsinglelinkedlist.py diff --git a/data-structures/linked-lists/circularsinglelinkedlist.py b/data-structures/linked-lists/circularsinglelinkedlist.py new file mode 100644 index 0000000..1ef3c6e --- /dev/null +++ b/data-structures/linked-lists/circularsinglelinkedlist.py @@ -0,0 +1,116 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class CircularLinkedList: + def __init__(self): + self.head = None + + def get_node(self, index): + if self.head is None: + return None + current = self.head + for i in range(index): + current = current.next + if current == self.head: + return None + return current + + def get_prev_node(self, ref_node): + if self.head is None: + return None + current = self.head + while current.next != ref_node: + current = current.next + return current + + def insert_after(self, ref_node, new_node): + new_node.next = ref_node.next + ref_node.next = new_node + + def insert_before(self, ref_node, new_node): + prev_node = self.get_prev_node(ref_node) + self.insert_after(prev_node, new_node) + + def insert_at_end(self, new_node): + if self.head is None: + self.head = new_node + new_node.next = new_node + else: + self.insert_before(self.head, new_node) + + def insert_at_beg(self, new_node): + self.insert_at_end(new_node) + self.head = new_node + + def remove(self, node): + if self.head.next == self.head: + self.head = None + else: + prev_node = self.get_prev_node(node) + prev_node.next = node.next + if self.head == node: + self.head = node.next + + def display(self): + if self.head is None: + return + current = self.head + while True: + print(current.data, end = ' ') + current = current.next + if current == self.head: + break + + +a_cllist = CircularLinkedList() + +print('Menu') +print('insert after ') +print('insert before ') +print('insert at beg') +print('insert at end') +print('remove ') +print('quit') + +while True: + print('The list: ', end = '') + a_cllist.display() + print() + do = input('What would you like to do? ').split() + + operation = do[0].strip().lower() + + if operation == 'insert': + data = int(do[1]) + position = do[3].strip().lower() + new_node = Node(data) + suboperation = do[2].strip().lower() + if suboperation == 'at': + if position == 'beg': + a_cllist.insert_at_beg(new_node) + elif position == 'end': + a_cllist.insert_at_end(new_node) + else: + index = int(position) + ref_node = a_cllist.get_node(index) + if ref_node is None: + print('No such index.') + continue + if suboperation == 'after': + a_cllist.insert_after(ref_node, new_node) + elif suboperation == 'before': + a_cllist.insert_before(ref_node, new_node) + + elif operation == 'remove': + index = int(do[1]) + node = a_cllist.get_node(index) + if node is None: + print('No such index.') + continue + a_cllist.remove(node) + + elif operation == 'quit': + break \ No newline at end of file From d57fbd5bd50940831ef04ab62544732b108d189e Mon Sep 17 00:00:00 2001 From: Yashaswi A <59396277+yashaswiadyalu@users.noreply.github.com> Date: Mon, 19 Oct 2020 12:33:09 +0530 Subject: [PATCH 6/6] Delete circulardoublylinkedlist.py --- .../linked-lists/circulardoublylinkedlist.py | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 data-structures/linked-lists/circulardoublylinkedlist.py diff --git a/data-structures/linked-lists/circulardoublylinkedlist.py b/data-structures/linked-lists/circulardoublylinkedlist.py deleted file mode 100644 index eef2769..0000000 --- a/data-structures/linked-lists/circulardoublylinkedlist.py +++ /dev/null @@ -1,109 +0,0 @@ -class Node: - def __init__(self, data): - self.data = data - self.next = None - self.prev = None - - -class CircularDoublyLinkedList: - def __init__(self): - self.first = None - - def get_node(self, index): - current = self.first - for i in range(index): - current = current.next - if current == self.first: - return None - return current - - def insert_after(self, ref_node, new_node): - new_node.prev = ref_node - new_node.next = ref_node.next - new_node.next.prev = new_node - ref_node.next = new_node - - def insert_before(self, ref_node, new_node): - self.insert_after(ref_node.prev, new_node) - - def insert_at_end(self, new_node): - if self.first is None: - self.first = new_node - new_node.next = new_node - new_node.prev = new_node - else: - self.insert_after(self.first.prev, new_node) - - def insert_at_beg(self, new_node): - self.insert_at_end(new_node) - self.first = new_node - - def remove(self, node): - if self.first.next == self.first: - self.first = None - else: - node.prev.next = node.next - node.next.prev = node.prev - if self.first == node: - self.first = node.next - - def display(self): - if self.first is None: - return - current = self.first - while True: - print(current.data, end = ' ') - current = current.next - if current == self.first: - break - - -a_cdllist = CircularDoublyLinkedList() - -print('Menu') -print('insert after ') -print('insert before ') -print('insert at beg') -print('insert at end') -print('remove ') -print('quit') - -while True: - print('The list: ', end = '') - a_cdllist.display() - print() - do = input('What would you like to do? ').split() - - operation = do[0].strip().lower() - - if operation == 'insert': - data = int(do[1]) - position = do[3].strip().lower() - new_node = Node(data) - suboperation = do[2].strip().lower() - if suboperation == 'at': - if position == 'beg': - a_cdllist.insert_at_beg(new_node) - elif position == 'end': - a_cdllist.insert_at_end(new_node) - else: - index = int(position) - ref_node = a_cdllist.get_node(index) - if ref_node is None: - print('No such index.') - continue - if suboperation == 'after': - a_cdllist.insert_after(ref_node, new_node) - elif suboperation == 'before': - a_cdllist.insert_before(ref_node, new_node) - - elif operation == 'remove': - index = int(do[1]) - node = a_cdllist.get_node(index) - if node is None: - print('No such index.') - continue - a_cdllist.remove(node) - - elif operation == 'quit': - break \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy