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

What is the cost of the course?What kind of questions are asked for interviews from C language??
Course fee depends on the depth of the course. Interview question are mainly from DS(Data Structure) importantly linked list, BST tree,array and except this pointer, string, function and recursion is important.
Chandana
What are the functions in C programming?
Functions in C are the basic building blocks of a C program. A function is a set of statements enclosed within curly brackets ({}) that take inputs, do the computation, and provide the resultant output....
Kiran
0 0
6
Can we pass arguments in the main function in C language?
Yes. it is possible to pass arguments into the main function.
Mukesh
0 0
7
Can a file other than a .h file be included with #include?
Yes. user defined header file ex:#include "nim.c"
Pankaj
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

C Program-String Comparison
// WAP to compare strings entered by the user //Header files #include<stdio.h>#include<conio.h>#include<string.h> //Main function void main(){ char str1; char str2; int comp; //Function...
S

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

C Program-Error Handling
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main function void main(){ int dividend=10; int divisor=0; int quotient; //Function for clearing screen clrscr(); ...
S

Program to swap the value of two variables without using third variable(simplest way)...
int main() { int a=10,b=20; printf("values of a before swap ="%d,a); printf("values of b before swap ="%d,b); a=a+b; b=a-b; a=a-b; printf("******************"); printf("values of a after swap...

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...

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