A linked list in C++ is a data structure consisting of nodes that contain data and a pointer to the next node, allowing for dynamic memory allocation. There are two types of linked lists: singly linked lists and doubly linked lists, with operations for inserting and deleting nodes at various positions. Stacks and queues are also discussed, with stacks following a last-in-first-out principle and queues following a first-in-first-out principle, each having specific functions for data manipulation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views9 pages
Assgnment 3rd Semester
A linked list in C++ is a data structure consisting of nodes that contain data and a pointer to the next node, allowing for dynamic memory allocation. There are two types of linked lists: singly linked lists and doubly linked lists, with operations for inserting and deleting nodes at various positions. Stacks and queues are also discussed, with stacks following a last-in-first-out principle and queues following a first-in-first-out principle, each having specific functions for data manipulation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
What is a link list in c++?
A link list is way to store data in computer memory at
different locations. It is used to represent data in the form of nodes. Link lists consists of a data part and the pointer to the next node. The link list always starts with a head pointer with points to the first node. The address stored in the list node is null. In link list, we can add a node at the beginning, between two nodes and at the end of a node even we can delete a node from the beginning end and between two nodes. Examples of link list: In playing audio, where we want to move the song forward and backward. It is used to forward and backward the web pages of the websites. Types of link list: Link list are of two types singly link list and doubly link list. Singly link list: A normal link list is called singly link list. In this list the node contains data part and address to next node. Doubly link list: A doubly link list is a type of link list which contain the address of its next node and its previous node. Inserting a node in the beginning of link list: In order to insert a node in the beginning of a link list, we should create a node first which would point to the address of the first node of the link list and the head pointer should point to the newly created node. Inserting a node in the middle of the link list: In this case, we will ask the position where you want to create a node. After creating the node at that position join newly created node to its next and previous node. Inserting a node at the end of the link list: In order to insert a node in the end, we will first transverse to the end of the link list, than create a new node and than its previous node should point to the newly created node. Deleting a node from the beginning of the link list: In order to delete a node from the beginning of a link list, you only have to point the head pointer to the next node. Deleting a node from any location from the link list: In order to delete a node from any position from the link list, we should destroy the connection from its next node and its previous node. Deleting a node from the end of the link list: In order to delete a node from the end of the link list, you should destroy the connection of the last node from its previous node and set the pointer of the previous node to null. Algorithm of link list 1. START 2. Create a node to store the data 3. Check if the list is empty 4. If the list is empty, add the data to the node and assign the head pointer to it. 5 If the list is not empty, add the data to a node and link to the current head. Assign the head to the newly added node. 6. END What is stack in c++? A stack is a way to represent data in the computer memory. It follows the first in last out for mate. It uses functions like push, pop, peek and display to enter, remove, to find the largest value and to see the values respectively. Examples of stack function: It is used in applications like Microsoft word where we want to undo or redo a task. It is used to access the value from end to the beginning. Push function: Push function is used to enter a value in a stack. First it checks whether the stack is empty or full. If the stack is empty than enter a value else display that stack is full. Pop function: Pop function is used to pop out a value from the stack. First we checks whether the stack is no value or any value. If the stack contains any value than pop it out from the stack else display that stack is empty. Peek function: Peek function is used to find the largest value without popping it out. First we checks whether the stack contains any value or not. If there is any value in the stack than display it else displays that the stack is empty. Display function: Display function is used display the values of the stack. Algorithm of stack: N=5 Stack[n] Top=-1 Push(int x) If(top<n) Top++ Stack[top] = x Else Cout<< “stack is full” Pop() If(top=-1) Cout<< “Stack is empty” Else Stack[top] Top— Display () For (int I=0;i<n ; I++) Stack[I] What is queue in c++? Queue is also used to represent data in the computer memory. It follows first in first out for mate. It uses enqueue and dequeue functions to enter or remove the value. It contains two variables to enter and remove a value. Examples of queue: In the banks, we see that the person who gets enter first get out first. It is used in data communication when the transmission medium cannot take any data to transfer. Enqueue function: Enqueue function is used to enter value in a queue. First it checks whether the queue is empty, full or can accept any value. If queue is empty than first increment those variables and then enter the value. Else if the queue is full then display that queue is full, otherwise select one variable to enter the value, increment it with one and enter the value. Dequeue function: Dequeue function is used to remove the value. First checks that if queue is empty, if the value of those two variables are equal or not. If the queue is empty then display that queue is empty, else if the value of two variables are equal then first print out the value of queue and then allocate minus one to both variables. Else increment the other variable by one. Algorithm for queue: Queue [5] Front=-1 Rear =-1 Enqueue (int x) If (front=-1&&rear=-1) Front = front +1 Rear = rear +1 Queue [front] = x Else if (front=4) Cout<< “Queue is full” Else Front++ Queue [front] = x Dequeue () If (front=-1&&rear=-1) Cout<< “Queue is empty” Else if (front=rear) Front=rear=-1 Else Rear++ Int main () Enqueue(5) Enqueue(15) Enqueue(53) Enqueue(34) Dequeue() Dequeue()