Uploaded by Mayank Sharma

DSA Lab 6 D1 Feb-15

advertisement
DSA Lab – 6
Linked List
Batch – D1
(February, 2024)
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
INDIAN INSTITUTE OF TECHNOLOGY DHARWAD
Feb, 2024
General Instructions
Before you start programming:
Open the terminal. Using suitable commands 1. Create a directory named “roll number” in the current directory.
2. Create another directory named “LAB number” within the roll number directory.
3. Within the “LAB_number” directory place your files. Follow the naming convention as
“RollNumber_PracticeQuestionNumber.c” (for example 23200100_p1.c) for practice
questions. Follow the naming convention as “RollNumber_TestQuestionNumber.c” (for
example 23200100 test1.c) for test questions.
After you complete programming and before leaving the lab:
1. Ensure your assigned TA has marked your attendance.
2. Ensure your assigned TA has checked your practice question and test question solutions
and you are evaluated for the same.
3. Ensure to upload your folder on Moodle after evaluation.
Difficulty: EASY
1. Array based linear list ADT
Write a program to implement the Array based linear list ADT and its functionalities.
The linear list strcuture is defined as follows:
typedef struct {
int Max_Size;
int *element;
// 1D dynamic array
int len;
} LinearList;
Implement the following functionalities:
LinearList createEmptyList(LinearList list, int size)
// Constructor for the sequential linear list – Create an empty list
int isEmpty(LinearList list)
// Function that checks whether the list is empty
int listLength(LinearList list)
// Function that returns the length of the list
LinearList insert(LinearList list, int k, int x)
// Insert x after k-th element.
int search(LinearList list, int x)
// Search for x and return the position if found, else return 0.
LinearList deleteElement(LinearList list, int k)
// Delete the k-th element.
void printContents(LinearList list)
// Print all the elements of the Linear List
The main function should be interactive asking user for the option of which operation to be
performed and take the appropriate action
void main()
{ //First Create an empty Linear List
//Then create an infinite loop where the user can do various operations as
// described in the sample output
}
Expected Outcome:
Empty Linear List created. Enter your choice from the following
options:
1. Check if the Linear List is empty
2. Length of the Linear List
3. Insert new element
4. Search an element
5. Delete an element
6. Print contents
7. Exit
Your choice :
1
The is list empty.
Enter your choice from the following options:
1. Check if the Linear List is empty
2. Length of the Linear List
3. Insert new element
4. Search an element
5. Delete an element
6. Print contents
7. Exit
Your choice :
3
Element to be inserted : 20
Position to be inserted : 0
Element Inserted.
Enter your choice from the following options:
1. Check if the Linear List is empty
2. Length of the Linear List
3. Insert new element
4. Search an element
5. Delete an element
6. Print contents
7. Exit
Your choice :
3
Element to be inserted : 30
Position to be inserted : 4
Invalid position.
Enter your choice from the following options:
1. Check if the Linear List is empty
2. Length of the Linear List
3. Insert new element
4. Search an element
5. Delete an element
6. Print contents
7. Exit
Your choice :
4
Element to search : 30
Element not Found.
Enter your choice from the following options:
1. Check if the Linear List is empty
2. Length of the Linear List
3. Insert new element
4. Search an element
5. Delete an element
6. Print contents
7. Exit
Your choice :
5
Enter position of the item to be deleted: 0
Element Deleted.
2. Linked List ADT
Write a program to implement the linked list ADT and its functionalities.
Every node of a linked list is defined by the following structure.
struct node{
int data;
struct node *link;
};
Implement the following functionalities:
void insertElement(struct node * LinkedList, int k, int x)
// Insert a node with element x after k-th element
int isMember(struct node * LinkedList, int x)
//Search for x and return the position if found, else return 0.
void DeleteNode(struct node * LinkedList, int k)
//Delete k-th node from the Linked list. Ensure that the memory of the deleted node is
released
void printContents(struct node * LinkedList)
//Print all the contents of the Linked List
void main()
{
//First Create an empty Linked List
//Then create an infinite loop where the user can do various operations as
// described in the sample output
}
Expected Outcome:
Empty Linear List created.
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 1
Element to be inserted: 10
Position to be inserted: 0
Invalid position. Position should be non-negative.
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 1
Element to be inserted: 10
Position to be inserted: 1
Element Inserted.
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 2
Element to be searched: 10
Element found at position 1
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 3
Position of the element to be deleted: 2
Invalid position. Position exceeds the length of the list.
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 3
Position of the element to be deleted: 1
Element Deleted.
Enter your choice from the following options:
1. Insert new element
2. Search an element
3. Delete an element
4. Print contents
5. Exit
Your choice: 4
List contents: (Empty)
Difficulty: Medium
3. Doubly Linked List ADT and Count Occurrences of an element
Write a program to implement the doubly linked list ADT and the following functions. (Bonus if
the function CountOccurrences is implemented using recursion)
struct Dnode{
int data;
struct Dnode *next;
struct Dnode *previous;
};
void insertElement(struct Dnode * DoublyLinkedList, int k, int x)
// Insert a node with element x after k-th element
int isMember(struct Dnode * DoublyLinkedList, int x)
//Search for x and return the position if found, else return 0.
void DeleteNode(struct Dnode * DoublyLinkedList, int k)
//Delete k-th node from the Linked list. Ensure that the memory
of the deleted node is released
void printContents(struct Dnode * DoublyLinkedList)
//Print all the contents of the Linked List
int CountOccurrences(struct Dnode * DoublyLinkedList, int x)
//Return the number of times x occurs in the Doubly Linked
List. Return 0 if x does not appear in it.
void main()
{ //First Create an empty Linked List
//Then create an
operations as
infinite loop where the user can do various
}
// described in the sample output
Expected Outcome:
Empty Doubly Linked List created.
Enter your choice from the following options:
1. Insert new element at a specified position
2. Search an element
3. Delete an element at a specified position
4. Print contents
5. Count the number of occurrences of a given element
6. Exit
Your choice: 1
Element to be inserted: 15
Enter position: 1
Element Inserted.
Enter your choice from the following options:
1. Insert new element at a specified position
2. Search an element
3. Delete an element at a specified position
4. Print contents
5. Count the number of occurrences of a given element
6. Exit
Your choice: 1
Element to be inserted: 20
Enter position: 0
Invalid position. Position should be greater than or equal to 1.
Enter your choice from the following options:
1. Insert new element at a specified position
2. Search an element
3. Delete an element at a specified position
4. Print contents
5. Count the number of occurrences of a given element
6. Exit
Your choice: 4
List contents: 15
Enter your choice from the following options:
1. Insert new element at a specified position
2. Search an element
3. Delete an element at a specified position
4. Print contents
5. Count the number of occurrences of a given element
6. Exit
Your choice: 5
Enter element to count occurrences: 20
Number of Occurrences of element 20: 0
Enter your choice from the following options:
1. Insert new element at a specified position
2. Search an element
3. Delete an element at a specified position
4. Print contents
5. Count the number of occurrences of a given element
6. Exit
Your choice: 6
4. Rotate a Linked List
Input a linked List and an integer k and insert the last k nodes to the
beginning of the List. You can use some of the functions from the previous
question if needed.
struct node{
int data;
struct node *link;
};
void Rotate(struct node * DoublyLinkedList, int k)
{ //Your code to rotate the list by adding last k nodes to the
first k positions
}
void main()
{ //Take input size of the lined list and the elements
//Take the value of k as input
//Call the rotate function and print the resulting List.
// Print the rotated result
}
Expected output :
Enter the size of the Linked List : 5
Enter the elements : 10 20 30 40 50
Enter the value of k : 2
The rotated Linked List is : 40 50 10 20 30
Difficulty: Hard
5.
Addition of two large numbers using Doubly Linked Lists
Add two numbers represented by two doubly linked lists. You can use the functions from the
previous questions if needed.
struct Dnode{
int data;
struct Dnode *next;
struct Dnode *previous;
};
struct Dnode* Add (struct Dnode * Number1, struct Dnode * Number2)
{ //Your code to add the two numbers represented as doubly linked list and return the result
// You can assume that both numbers are positive integers and the first number if always greater or
equal to the second number
}
void main()
{ //Take the number of digits in the two numbers s input
// Then input the two numbers and store them as two Doubly linked list
// Call the Add function and print the result
}
Expected Output:
Enter the number of digits in the two numbers : 15 10
Enter the first number : 100000000000000
Enter the second number : 1000000000
The Sum of the two numbers is : 100001000000000
Download