UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I write a C program to search for a particular record, add a new record, and sort lexicographically for a data-set using a linked list?

Asked by Last Modified  

Follow 2
Answer

Please enter your answer

Advance Excel And VBA Training

How To Reverse a Linked List (3 Different Ways) Introduction There are a couple of ways to reverse a linked list. One of them requires knowledge of pointers and one of them is pretty straight forward. In this article, 3 different methods of reversing a linked list are demonstrated. All the linked list...
read more
How To Reverse a Linked List (3 Different Ways) Introduction There are a couple of ways to reverse a linked list. One of them requires knowledge of pointers and one of them is pretty straight forward. In this article, 3 different methods of reversing a linked list are demonstrated. All the linked list reversing algorithms assume that the given linked list is a double linked list. Refer Image 1. Technique 1 In this way, a new linked list will be created and all the items of the first linked list will be added to the new linked list in reverse order. public void ReverseLinkedList (LinkedList linkedList) { // ------------------------------------------------------------ // Create a new linked list and add all items of given // linked list to the copy linked list in reverse order // ------------------------------------------------------------ LinkedList copyList = new LinkedList(); // ------------------------------------------------------------ // Start from the latest node // ------------------------------------------------------------ LinkedListNode start = linkedList.Tail; // ------------------------------------------------------------ // Traverse until the first node is found // ------------------------------------------------------------ while (start != null) { // ------------------------------------------------------------ // Add item to the new link list // ------------------------------------------------------------ copyList.Add (start.Item); start = start.Previous; } linkedList = copyList; // ------------------------------------------------------------ // That's it! // ------------------------------------------------------------ } This way is probably the most inefficient among the three. Even though objects of each node are not copied to memory, a new link list node is created for each call to “Add” property. A new link list node means at least 3 pointers (Next, Previous, Item) will be created for each item. This method not only wastes memory, but also wastes processing power. Technique 2 In this method, we will swap linked list node objects (references to the data). Swapping starts from the first node’s object and the first node’s object is swapped with the last node’s object. Then, the second node’s object is swapped with the one before the last node’s object. Refer Image 2. Assuming we have N nodes in the link list: • Swap: 1st node’s object with Nth node’s object • Swap: 2nd node’s object with (N-1)th node’s object • Swap: 3rd node’s object with (N-2)th node’s object After swapping: Refer Image 3. Swapping goes on until the middle of the linked list is found. public void ReverseLinkedList (LinkedList linkedList) { // ------------------------------------------------------------ // Create variables used in the swapping algorithm // ------------------------------------------------------------ LinkedListNode firstNode; // This node will be the first node in the swapping LinkedListNode secondNode; // This node will be the second node in the swapping int numberOfRun; // This variable will be used to determine the number of swapping runs // ------------------------------------------------------------ // Find the tail of the linked list // ------------------------------------------------------------ // Assume that our linked list doesn’t have a property to find the tail of it. // In this case, to find the tail, we need to traverse through each node. // This variable is used to find the tail of the linked list LinkedListNode tail = linkedList.Head; // Start from first link list node // and go all the way to the end while (tail.Next != null) tail = tail.Next; // ------------------------------------------------------------ // Assign variables // ------------------------------------------------------------ firstNode = linkedList.Head; secondNode = tail; numberOfRun = linkedList.Length / 2; // Division will always be integer since the numberOfRun variable is an integer // ------------------------------------------------------------ // Swap node’s objects // ------------------------------------------------------------ object tempObject; // This will be used in the swapping 2 objects for (int i=0; i < numberOfRun; i++) { // Swap the objects by using a 3rd temporary variable tempObject = firstNode.Item; firstNode.Item = secondNode.Item; secondNode.Item = tempObject; // Hop to the next node from the beginning and previous node from the end firstNode = firstNode.Next; secondNode = secondNode.Previous; } // ------------------------------------------------------------ // That's it! // ------------------------------------------------------------ } This way of reversing a linked list is optimized and pretty fast. The only overhead of this algorithm is finding the end of the linked list. Technique 3 In this way, the head of the linked list will point to the last node of the linked list. Also, each node’s “Next” and “Previous” properties need to be swapped too. Next (red arrow) and Previous (blue arrow) are swapped (Image 4): public void ReverseLinkedList (LinkedList linkedList) { LinkedListNode start = linkedList.Head; LinkedListNode temp = null; // ------------------------------------------------------------ // Loop through until null node (next node of the latest node) is found // ------------------------------------------------------------ while (start != null) { // ------------------------------------------------------------ // Swap the "Next" and "Previous" node properties // ------------------------------------------------------------ temp = start.Next; start.Next = start.Previous; start.Previous = temp; // ------------------------------------------------------------ // Head property needs to point to the latest node // ------------------------------------------------------------ if (start.Previous == null) { linkedList.Head = start; } // ------------------------------------------------------------ // Move on to the next node (since we just swapped // "Next" and "Previous" // "Next" is actually the "Previous" // ------------------------------------------------------------ start = start.Previous; } // ------------------------------------------------------------ // That's it! // ------------------------------------------------------------ } This method of reversing the linked list is performed in a single traverse through the link list. This way is more optimized than the second method. If the reversing requires to keep the link list nodes at their same position, technique 2 will fail. Conclusion Method 1 is the most straight forward and it can be implemented quickly. However, it uses lots of system resources. Every time a new node is added, a new memory in the heap will be reserved. Method 2 is pretty professional but if link list doesn’t keep track of its tail, link list will be traversed twice. In this case, it will be slower than method 3. Method 3 is the most professional and the fastest one. read less
Comments

Professionally Trainer

By using linked list you can insert search particular record by using front and rear elements as a pointer variables.
Comments

Related Questions

How do I get 6 power 13 in c language?
#include <stdio.h> #include <math.h> int main() { double result = pow(6, 13); printf("6 to the power of 13 is: %f ", result); return 0; }
Raju
0 0
6
What are the toughest topics in C language?
Pointers and memory management
Kavidassan
0 0
6
What is a local block in C programming?
the statements within the braces { } is known as local block
Rakhi
0 0
6
Can the last case of a switch statement skip including the break?
Last case if a switch should be DEFAULT.. Good to have this case always. Executed only when there's no other matching cases found.
Sanjeev

What is void main () into main and what it does?

Void main () is the entry point for execution in C program. The void is a keyword that represents function will not return anything but a void value. Main is the name of the function and () represents...
Noreen

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Datatypes in C Language
Data types in C Language Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that...

Some interview questions for freshers on C language
Q1. What are the two forms of #include? There are two variants of #include. The one is #include and the other one is #include”file”. In general the first form that is #include is used to...

Difference Between C Language and C Program
C Language: C Language is structured, high level and machine independent language. C Program: C Program is the collection of functions that are supported by C library.
S

Shashwat Kumar

0 0
0

Set 0 To Kth Bit In A Variable
The following code snippet Set 0 to Kth Bit in an variable #include int UnsetBitValue(int n, int k);int main(){ printf("%d\n\n",UnsetBitValue(255,6)); return 0;}// Set kth bit to zeroint UnsetBitValue(int...

Why Indexing Should Start From Zero In Array ?
Why numbering should start at zero? To denote the subsequence of natural numbers 2, 3, ..., 12 without the pernicious three dots, fourconventions are open to usa) 2 ≤ i < 13b) 1 < i ≤ 12c)...

Recommended Articles

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more