Data structures using C,
C Program to reverse a Linked List. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. In this program, We reverse the linked list, but without sorting the linked list.Read more about C Programming Language .
To browse more C Programs visit this link
C Program to reverse a Linked List. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. In this program, We reverse the linked list, but without sorting the linked list.Read more about C Programming Language .
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct leftnode {
int num;
struct leftnode *next;
};
void linklist_add(struct leftnode **n, int val);
void linklist_reverse(struct leftnode **n);
void linklist_display(struct leftnode *n);
int main(void) {
struct leftnode *new = NULL;
int i = 0;
for(i = 0; i <= MAX; i++)
linklist_add(&new, i);
printf("Linked list before reversal:n");
linklist_display(new);
linklist_reverse(&new);
printf("Linked list after reversal:n");
linklist_display(new);
return 0;
}
void linklist_add(struct leftnode **n, int val) {
struct leftnode *temp = NULL;
temp = malloc(sizeof(struct leftnode));
temp->num = val;
temp->next = *n;
*n = temp;
}
void linklist_reverse(struct leftnode **n) {
struct leftnode *a = NULL;
struct leftnode *b = NULL;
struct leftnode *c = NULL;
a = *n, b = NULL;
while(a != NULL) {
c = b, b = a, a = a->next;
b->next = c;
}
*n = b;
}
void linklist_display(struct leftnode *n) {
while(n != NULL)
printf(" %d", n->num), n = n->next;
printf("n");
}
Read more Similar C Programs
Data Structures
Learn C Programming
You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,
our page!
Share this program with your Facebook friends now! by liking it
(you can send this program to your friend using this button)
Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com