Python Practice Linked List Graph

In this class, we do Python Practice Linked List Graph.

For Complete YouTube Video: Click Here

Graph Example

The reader should have the basics of Data structure and python. Please check our data structure course for Python Click here.

All these examples will help you to improve your coding skills and to crack the placement exams easily.

Try to solve the questions on your own for better practice.

Q1) Mahesh is maintaining a social networking website. In that social networking website, any user can follow any other user.

He wants to organize his followers effectively. He is considering only his immediate followers and followers of his immediate followers.

Which data structure is best suitable?

A1) Hash Table

A2) Queue

A3) Tree

A4) Graph

Solution

Take an example; user one is following user two. And user two is following user three.

User three is following user one.

The above scenario is graphically shown like this. U1->U2->U3->U1.

because it is forming loops. A graph data structure can easily manage the loops.

Linked List

Q2) Consider below input_stack
input_stack(top to bottom) 3,9,4,7,9,3,5,8

what will be the content of linked_list.
after executing mll function below with input_stack as parameter?

def mll(input_stack):
    linked_list=liskedlist()
    temp_stack=stack(10)
    temp_queue=queue(10)
    While not input_stack.isempty():
        temp_stack.push(input_stack.pop())
        temp_queue.enque(input_stack.pop())
    while not(temp_stack.isempty()) and not(temp_queue.isempty()):
        linked_list.add(temp_stack.pop()+temp_stack.pop())
        #add method will add new node with given data at end of list
        linked_list.add(temp_queue.dequeue()+temp_queue.dequeue())
    temp=linked_list.gethead() # gethead returns head node
    while temp.getnext() is not None:#getnext will return nextnode
        if (temp.getdata()+temp.getnext().getdata())%2==0:#getdata return data in the node
            temp.setdata(temp.getdata()*2)# setdata update data in the node
        else:
            temp.setdata(temp.getnext().getdata()*2)
        temp=temp.getnext()
    return linked_list

Output:
The content present in linked list after executing function mll
28->14->14->11

In the above program, input_stack is given. Meaning the program has created a stack object name input_stack.

They have given that stack object to the function mll.

The stack class is already available. And we can use the methods known in the stack class on the stack object input_stack.

In the same way linked list class is also available, and we can use the methods present in the linked list class.

The description of methods is given in the question.

To solve these types of questions, one should draw the stack and linked list diagrams. To avoid making mistakes.

The input_stack diagram is shown below.

Top->[3,9,4,7,9,3,5,8]-> bottom

In the same way, we have to take the linked list and queue. Then start analyzing the code.

A detailed explanation is provided in the video.