0% found this document useful (0 votes)
14 views7 pages

A

The document contains code for implementing a doubly linked list in Python. It defines Node and DoublyLinkedList classes. The DoublyLinkedList class contains methods to add nodes to the beginning, middle, and end of the list, print the list, get the size, and remove a node. It also includes code to test the doubly linked list implementation by adding and removing nodes and printing the list.

Uploaded by

Anuradha Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

A

The document contains code for implementing a doubly linked list in Python. It defines Node and DoublyLinkedList classes. The DoublyLinkedList class contains methods to add nodes to the beginning, middle, and end of the list, print the list, get the size, and remove a node. It also includes code to test the doubly linked list implementation by adding and removing nodes and printing the list.

Uploaded by

Anuradha Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ANURADHA ANAND

IT-31152205004

PROGRAM:

class Node:

def __init__(self, data):

self.data = data

self.next=None

class LinkedList:

def __init_(self):

self.head=None

def push(self, new_data):

new_node=Node(new_data)

new_node.next=self.head

self.head=new_node

def insertAfter(self, prev_node, new_data):

if prev_node is None:

print("the given previous node must in linked list")

return

new_node=Node(new_data)

new_node.next=prev_node.next

prev_node.next=new_node

def append(self,new_data):

new_node=Node(new_data)

if self.head is None:

self.head=new_node

return

last=self.head

while(last.next):

last=last.next

last.next=new_node
ANURADHA ANAND
IT-31152205004

def printList(self):

temp=self.head

while(temp):

print(temp.data,end="")

temp=temp.next

llist = LinkedList()

llist.append(6)

llist.push(7)

llist.push(1)

llist.append(4)

llist.insertAfter(llist.head.next,8)

print("created linked list is:")

llist.printList()

OUTPUT:
ANURADHA ANAND
IT-31152205004

PROGRAM:

class node:

def __init__(self,data):

self.d=data

self.nxt=None

self.pre=None

class dlinkedkist:

def __init__(self):

self.head=None

def printf(self):

h=self.head

while h is not None:

print("data:",h.d)

h=h.nxt

print("==========")

def size(self):

r=0

while self.head is not None:

r+=1

self.head=self.head.nxt

return r

def atbegin(self,d):

new=node(d)

new.nxt=self.head

if self.head is not None:

self.head.pre=new

self.head=new

def mid(self,pre_pos,d):
ANURADHA ANAND
IT-31152205004

if pre_pos is None:

print("The given previous node cant be None")

return

new=node(d)

new.nxt=pre_pos.nxt

pre_pos.nxt=new

new.pre=pre_pos

if new.nxt:

new.nxt.pre=new

def atend(self,d):

new=node(d)

if self.head is None:

self.head=new

return

l=self.head

while l.nxt:

l=l.nxt

l.nxt=new

new.pre=l

return

def remove(self, dele):

if self.head is None or dele is None:

return

if self.head == dele:

self.head = dele.nxt

if dele.nxt is not None:

dele.nxt.pre = dele.pre

if dele.pre is not None:

dele.pre.nxt = dele.nxt

dl=dlinkedkist()

dl.atend(2)
ANURADHA ANAND
IT-31152205004

dl.printf()

dl.atbegin(45)

dl.printf()

dl.mid(dl.head.nxt,14)

dl.printf()

dl.remove(dl.head.nxt.nxt)

dl.printf()

output:
ANURADHA ANAND
IT-31152205004

PROGRAM:

import numpy as np

import array

A = np.array([[[1,2,3],[4,5,6],[7,8,9]]])

B = np.array([[[2,4,6], [8,10,12], [14,16,18]]])

print("The A matrix is:")

for a in A:

print (a)

print("The B matrix is:")

for b in B:

print(b)

R = np.array([[[0,0,0],[0,0,0],[0,0,0]]])

for i in range(0, len(A)):

for j in range(0, len(B)):

for k in range(len(B)):

R[i][j] = A[k][j]*B[i][k]

print("The resultant matrix is:")

for r in R:

print(r)
ANURADHA ANAND
IT-31152205004

OUTPUT:

You might also like