A linear data structure has data elements connected to each other so that elements are arranged in a sequential manner and each element is connected to the element in front of it and behind it. This way, the structure can be traversed in a single run.
Linear data structures can be implemented easily as computer memory is also arranged in a linear manner.
four types of linear data structures:
An array is a collection of the same data types stored at contiguous memory locations. Arrays can be used in Python with the
Below are various operations that can be done on arrays:
||Used to create an array with data type and value list specified in its arguments.|
||Used to add the value mentioned in its arguments at the end of the array.|
||Used to add the value(x) at ‘i’ position.|
||Removes the element at the position mentioned in its argument and returns it.|
||Used to remove the first occurrence of the value mentioned in its arguments.|
||Returns the index of the first occurrence of value mentioned in its arguments.|
||Reverses the array.|
import array arr=array.array('i',[1,2,3]) print(arr)
A linked list is a linear data structure in which elements are linked using pointers. It consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
#Crearting a Node class Node: def _init_(self,data): self.data=data self.ref=None # Creating Linked List class LinkedList: def _init_(self): self.head=None # printing or traversal Operation def Traversal(self): if self.head is None:# self.head==None print("Linked List is Empty!!!") else: h=self.head while h is not None: print(h.data,"----->") h=h.ref
A stack is a linear data structure that stores data elements in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) order. Here, a new element is added at one end and an element is removed from that end only. Stack in Python can be implemented in following ways:
The insert and delete operations are often called push and pop.
# Stack implementation using List stack= def push(): if len(stack)==size: # check wether the stack is full or not print("Stack is Full!!!!") else: element=input("Enter the element:") stack.append(element) print(stack) def pop_element(): if not stack:# or if len(stack)==0 print("Stack is Empty!!!") else: e=stack.pop() print("element removed!!:",e) print(stack) def display(): print(stack) size=int(input("Enter the size of Stack:")) while True: print("Select the Operation:\n 1.Push \n 2.Pop \n 3. Display \n 4. Quit") ch=int(input()) if ch==1: push() elif ch==2: pop_element() elif ch==3: display() elif ch==4: break else: print("Invalid Option!!!")
A queue is a linear data structure that stores data elements in First-In/First Out(FIFO) manner, i.e., the element that’s inserted first will be removed first. Queue in Python can be implemented by the following ways:
# implementing Queue using List : from collections import deque q=deque() q.append(10) q.append(100) q.append(1000) q.append(10000) print("Initial Queue is:",q) print(q.popleft()) print(q.popleft()) print("After Removing elements:",q)
View all Courses