See Also:
Example Program To Reverse Singly Linked List:
C Program To Merge Two Arrays
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
Insertion, Deletion, Traversal, Reversal And Search Operation on Arrays
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
Example Program To Reverse Singly Linked List:
#include<stdlib.h>
struct sllNode {
int data;
struct sllNode *nextPtr;
};
struct sllNode *front = NULL;
struct sllNode *rear = NULL;
/*
* creates Node and fill the given data
*/
struct sllNode * createNode(int data) {
struct sllNode *ptr = (struct sllNode *) malloc(sizeof (struct sllNode));
ptr->data = data;
ptr->nextPtr = NULL;
return ptr;
}
/* inserting node at the end of the list */
void insertionOp(int data) {
struct sllNode *ptr = createNode(data);
if (rear == NULL) {
front = ptr;
rear = ptr;
}
rear->nextPtr = ptr;
rear = ptr;
}
/* delete all nodes in the list */
void deleteList() {
struct sllNode *ptr;
ptr = front;
while (ptr){
front = ptr->nextPtr;
free(ptr);
ptr = front;
}
}
/* traverse the list and print the data in each node */
int walkList() {
struct sllNode *ptr;
int i = 0;
ptr = front;
while (ptr) {
printf("%d ", ptr->data);
ptr = ptr->nextPtr;
i++;
}
return (i);
}
/* reverse the linked list */
void reverseList() {
struct sllNode *xPtr, *yPtr, *zPtr;
yPtr = zPtr = NULL;
xPtr = front;
while (xPtr) {
zPtr = yPtr;
yPtr = xPtr;
xPtr = xPtr->nextPtr;
yPtr->nextPtr = zPtr;
}
front = yPtr;
return;
}
int main (int argc, char *argv[]) {
int data, i, n;
FILE *fp;
fp = fopen(argv[1], "r");
if (!fp) {
printf("Unable to open the file\n");
exit(0);
}
/* scan the inputs from the file & insert it into the list */
while (fscanf(fp, "%d", &data) != EOF) {
insertionOp(data);
}
printf("\nData in Linked List:\n");
n = walkList();
printf("\nNo of elements in linked list: %d\n", n);
printf("\nData in Reversed Linked List:\n");
reverseList();
n = walkList();
printf("\nNo of elements in linked list: %d\n\n", n);
deleteList();
return 0;
}
Output: (C Program To Reverse Singly Linked List)
jp@jp-VirtualBox:~/$ cat input1.txt
6 5
7 4
8 2
jp@jp-VirtualBox:~/$ ./a.out input1.txt
Data in Linked List:
6 5 7 4 8 2
No of elements in linked list: 6
Data in Reversed Linked List:
2 8 4 7 5 6
No of elements in linked list: 6
6 5
7 4
8 2
jp@jp-VirtualBox:~/$ ./a.out input1.txt
Data in Linked List:
6 5 7 4 8 2
No of elements in linked list: 6
Data in Reversed Linked List:
2 8 4 7 5 6
No of elements in linked list: 6
No comments:
Post a Comment