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

in c : i want run time input: data=[name:raj,age:20] output: value[0]=raj value[1]=20 i want c code
Make use of Command line argument and access using argv,argc.
Vanaraj
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
How do I get 6 power 13 in c language?
By using pow() function #include <math.h>#include <stdio.h>int main() { double base = 6.0; double exp = 13.0; double result = pow(base, exp); printf("%.1lf^%.1lf = %.2lf", base, exp, result); return 0;}
Tanush
0 0
5
How can I avoid the error messages?
When there is a syntax error in the program the compiler indicates it by giving error messages. By following proper syntax we can avoid error messages. Syntax is like grammar rules of english language.
Yusufali
What are pointers in C language?
Hi Imran, Hope you are doing good. Are you looking for short answer or long answer? :) Pointers in C are variables that store memory addresses. They point to the location of another variable, allowing...
Imran
0 0
7

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

Ask a Question

Related Lessons

Working with C/C++ applications
Inorder to learn C and C++ programming languages one can work with various editors available.To name a few are the most popular one is turbo c++, DEV C++, Eclipse, NetBeans. Here are the screen shots...


C-Program Swapping Contents Of Variables Using Function [Call By Reference Method]
//Header Files #include#include // User defined functions swap with 2 pointer variables passed as an argument list void swap(int*i,int*j){ // Local variable temp int temp; // swapping contents using...

What Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...

C Language
To get help in C window (for keywords, functions) press Alt +F1.To delete a single line , use the shortcut key CTRL +Y.If you got error about the path when you execute a C pgm, check the Options menu =>Directories.
T

Thilagam S.

0 0
0

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