Skip to main content

Data Structures Linked List Reverse

simple linked list
Data Structures -- Linked List -- Reverse

/*
* Simple operations on linked list. If any problem
*/

#include

/*
* Data structure used, its simple :)
*/
Typedef struct linked_list_s
{
int value;
struct linked_list_s *next;
}linked_list_t;


/*
* Add a node at the end of the list.
*/

Linked_list_t* add_node(linked_list_t *head, int value)
{
linked_list_t *newNode = NULL;
linked_list_t *node = head;
linked_list_t *prev = head;

newNode = (linked_list_t *)calloc(1, sizeof(linked_list_t));
newNode->value = value;

while(node)
{
prev = node;
node = node->next;
}
if(prev == node)
return newNode;

prev->next = newNode;

return head;
}

/*
* delete a specified node from the list.
*/

Linked_list_t* delete_node(linked_list_t *head, int value)
{
linked_list_t *node = NULL;
linked_list_t *prev = NULL;

for(node = head; node != NULL; prev = node, node = node->next)
{
if(node->value == value)
{
//Check for head node modification
if(prev == NULL)
{
head = head->next;
free(node);
return head;
}

prev->next = node->next;
free(node);
return head;
}
}
return head;
}

/*
* display all the nodes of the list.
*/

Void list_nodes(linked_list_t *head)
{
linked_list_t *node = head;
printf("\nHead");
while(node)
{
printf("->%d ",node->value);
node = node->next;
}
printf("->NULL\and");
}


/*
* Display all the nodes in reverse order withoout modifying list.
*/

Void list_nodes_in_reverse_order(linked_list_t *head)
{
linked_list_t *end = NULL;
linked_list_t *node = NULL;

printf("\nReverse Head");
while(head != end)
{
node = head;
while(node->next != end)
node = node->next;

printf("->%d ",node->value);
end = node;
}
printf("->NULL\and");
}


/*
* Reversing the linked list with recursion; Asked in Qualcomm interview questions..
*/

Linked_list_t* reverse_with_recursion_anotherway(linked_list_t* current, linked_list_t* parent)
{
linked_list_t* revhead = NULL;

if(current == NULL)
revhead = parent;
else
{
revhead = reverse_with_recursion_anotherway(current->next, current);
current->next = parent;
}
return revhead;
}

/*
* Reversing the linked list;
*/

Linked_list_t* reverse_with_recursion(linked_list_t* node)
{
linked_list_t* temp = NULL;

if(node->next == NULL)
return node;

temp = reverse_with_recursion(node->next);
temp->next =node;
return node;
}

/*
* reversing linked list without recursion.
*/

Linked_list_t* reverse_without_recursion(linked_list_t* head)
{
linked_list_t* prevNode = NULL;
linked_list_t* currNode = head;
linked_list_t* nextNode = head->next;

while(currNode)
{
currNode->next = prevNode;
prevNode = currNode;
if(nextNode == NULL)
break;
currNode = nextNode;
nextNode = nextNode->next;
}
return currNode;
}

/*
* main program, which displays menu for maitaining linked list.
*/

Main()
{
int choice = -1;
int value = 0;
linked_list_t *head = NULL;
linked_list_t *node = NULL;

do
{
printf("\and what you wanna do?\and");
printf("1. Add a node \and");
printf("2. Delete a node \and");
printf("3. List all nodes \and");
printf("4. Reverse (without recursion) the list \and");
printf("5. Reverse (recursively) the list \and");
printf("6. Reverse (recursively) another way\and");
printf("7. Just display in reverse order\and");
printf("8. Exit \and");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter value: ");
scanf("%d",&value);
head = add_node(head, value);
break;

case 2:
printf("\nEnter value: ");
scanf("%d",&value);
head = delete_node(head, value);
break;

case 3:
list_nodes(head);
break;

case 4:
node = head;
head = reverse_without_recursion(head);
break;

case 5:
node = head;
while(node->next)
node = node->next;
head = reverse_with_recursion(head);
head->next = NULL;
head = node;
break;

case 6:
head = reverse_with_recursion_anotherway(head, NULL);
break;

case 7:
list_nodes_in_reverse_order(head);
break;

case 8:
exit(0);
break;
}
}while(1);
}

Comments

Popular posts from this blog

Rajasthan Board 10th Results | rajedubord.nic.in | RBSE

Raj Board of Secondary Education, Rajasthan Results at rajeduboard.nic.in Board of Secondary Education, Rajasthan Ajmer declared result of secondary examination today at 4PM official site is rajedubord.nic.in check Secondary Examination- 2008 Result (To be announced on July 1st, 2008 at 4:00 PM) source http:// rajresults.nic.in / Declared result Praveshika Examination-2008 Result (Announced on June 25, 2008 at 4:00 PM) The history of the Board of Secondary Education Rajasthan (BSER) is a remarkable panorama of progressive record of the futurological vision for developing a dynamic system of various sub-systems of examinations and highlights of the academic excellence of the last four decades. The BSER took rapid strides for promotion and development of Secondary Education in Rajasthan, spread over 3,42,239 sq. km. and in more than 6000 schools located in 32 districts involving 8.5 lakhs students for Secondary and Senior Secondary Examination in the year 2000. At present the Board is ...

ahsec results 2008 | www.ahsec.nic.in

The official website of assam higher secondary education council Results of Assam is down seems they are uploading results , direct link to check ahsec results 2008 is http://resultsassam.nic.in/index.asp You can find results of AHSEC at the link above. current page The Website www.ahsec.nic.in Designed Developed & Hosted by NIC, Assam State Centre, Guwahati is down for result uploading. keep checking this page, I will update as soon as news comes. All the best to AHSEC - assam higher secondary education council students. Direct links http://assam.nic.in/ www.ahsec.nic.in http://resultsassam.nic.in/

Encyclopedia of Life - by the World's Leading Scientists

An epic effort to promote biodiversity and document all 1.8 million named species on the planet. When completed, http://www.eol.org will serve as a global biodiversity tool, providing scientists, policymakers, students, and citizens information they need to discover and protect the planet and encourage learning and conservation. The Encyclopedia of Life is a collaborative scientific effort led by the Field Museum of Natural History, Harvard University, Marine Biological Laboratory, Missouri Botanical Garden, Smithsonian Institution, and Biodiversity Heritage Library, a consortium including the core institutions and also the American Museum of Natural History (New York), Natural History Museum (London), New York Botanical Garden, and Royal Botanic Gardens (Kew). clipped from www.eol.org clipped from www.eol.org "Imagine an electronic page for each species of organism on Earth, available everywhere by single access on command." - Edward O. Wilson The Encyclopedia of Life (E...