Search
 
SCRIPT & CODE EXAMPLE
 

C

reverse list in C

#include <stdio.h>

void main() {
	int List[5] = { 1, 2, 3, 4, 5 };
	int starting_index = 0;
	int end_index = 4;

	while (starting_index < end_index) {
		int temp = List[starting_index];
		List[starting_index] = List[end_index];
		List[end_index] = temp;
		starting_index += 1;
		end_index -= 1;
	}
	for (int i = 0; i < 5; i++) {
		printf("%d, ", List[i]);
	}

}
Comment

Reversing the List in c

// Iterative C program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>
 
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
 
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
    struct Node* prev = NULL;
    struct Node* current = *head_ref;
    struct Node* next = NULL;
    while (current != NULL) {
        // Store next
        next = current->next;
 
        // Reverse current node's pointer
        current->next = prev;
 
        // Move pointers one position ahead.
        prev = current;
        current = next;
    }
    *head_ref = prev;
}
 
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
 
/* Function to print linked list */
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}
 
/* Driver code*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
 
    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 85);
 
    printf("Given linked list
");
    printList(head);
    reverse(&head);
    printf("
Reversed linked list 
");
    printList(head);
    getchar();
}
Comment

PREVIOUS NEXT
Code Example
C :: print bool c 
C :: c how to check a palindrome string 
C :: how to empty string in c 
C :: function for quicksort in c 
C :: tkinter create_line 
C :: get last char string c 
C :: write a binary file c 
C :: c program strtok use 
C :: what is system function in c 
C :: go optional parameters 
C :: c strcat 
C :: bash while loop n times 
C :: remove axis numpy array 
C :: c substring 
C :: enable disable audio listener unity 
C :: The fscanf and fprintf functions 
C :: fwrite in c 
C :: malloc contiguous 2d array 
C :: how to make a check bigger 
C :: binary search tree of strings in c 
C :: mongo connect db 
C :: convert char number to int in c 
C :: open with overwrite c 
C :: wifi access point in esp8266 
C :: tuples in c 
C :: round c 
C :: C Syntax of realloc() 
C :: C #ifdef Directive 
C :: do a barrel roll 
C :: change base int in c 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =