One of the main objectives of a programmer is to create programs and code which is efficient, elegant and properly readable by the programmer him/herself, his/her colleagues and the clients. For this, one has to focus more on the semantics of the program than on its implementation, which he/she does by creating classes for a certain data type and by defining various methods that could be carried out on the data type. This is where abstract data type comes in really handy.
An abstract data type (ADT) is a data type where we specify the kind and meaning behind the data type we store, and various methods we can carry out on that data type. I can see three major advantages to using an ADT to solve problems in computing than the normal ways in which we solved things back in CSC108. They are:
i) Using the concept of ADT definitely makes the program look a lot more elegant and readable for yourself, your peers and your clients.
ii) Since you don't have to focus on how the program is implemented, there is a lot more freedom in thinking about algorithms or new methods for the program.
iii) Lastly, through the use of ADT, one can easily carry out tasks like debugging, maintenance, updating the program, etc. in an efficient and organized manner, without disrupting the client code.
There are two main examples of ADTs that I would like to talk about. They are Stack and Queue. They are both similar in the sense that they are both implemented as a list in Python in order to pile up objects and remove them. However, the way they add and remove objects are different, and that's what I will be covering.
Stack:
A Stack is a linear sequence of data types. In a Stack, new items are stored on the top (end of the list), and items at the top are the ones that are removed first. Think of it as a stack of new notebooks when you go to to the shop. Usually, shopkeepers add new notebooks to the top of the stack, and consumers take notebooks that are on the top of the stack.
Some operations that can be carried out on the Stack ADT are:
i) Reverse the sequence of the Stack.
ii) Pop: Remove and return top item, provided the stack is non-empty.
iii) Push(item): Store item on top of stack.
iv) is_empty(): Return whether the stack is empty or not.
Queue:
Just like Stack, A Queue is also a linear sequence of data types. However, new items are stored at the back of the queue (end of the list) and items at the front (beginning of the list) are removed first. This means that in order to remove an item you just added in a queue, you need to remove everything you added before it.
Some operations that can be carried out on the Queue ADT are:
i) enqueue(A): add object A at the back of the queue (end of the list).
ii) dequeue(A): remove and return object A from the front of the queue(beginning of the list).
iii) Reverse the sequence of the Queue.
iv) is_empty(): Return whether the Queue is empty or not.
I'd recommend checking out the week 5 blog in the link http://nigerianmahogany.blogspot.ca/. Here, the person has a visual representation of what happens in a stack and in a queue.
I'd recommend checking out the week 5 blog in the link http://nigerianmahogany.blogspot.ca/. Here, the person has a visual representation of what happens in a stack and in a queue.
And that's about all I have regarding ADTs. I'm really enthusiastic towards learning about more ADTs, and I'll update you all if I learn more. Any questions are welcome and you can leave them in the comments below. Till then, keep on coding and stay awesome. Bye.