0% found this document useful (0 votes)
5 views

Program C

Uploaded by

arslanerazen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Program C

Uploaded by

arslanerazen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program Overview

This program performs several operations on a doubly linked list:

1. Delete nodes with negative values.

2. Sort the list in ascending order.

3. Insert a value while maintaining the sorted order.

4. Reverse the list with and without additional memory allocation.

5. Release the memory allocated for the list.

Detailed Explanation

1. Node Structure

typedef struct Node {

int data;

struct Node* next;

struct Node* prev;

} Node;

• This defines a Node structure for a doubly linked list, where each node contains:

o data: The value stored in the node.

o next: A pointer to the next node in the list.

o prev: A pointer to the previous node in the list.

2. Delete Nodes with Negative Values

void deleteNegativeNodes(Node** head) {

Node* current = *head;

while (current != NULL) {

Node* next = current->next;

if (current->data < 0) {

if (current->prev != NULL) current->prev->next = current->next;

if (current->next != NULL) current->next->prev = current->prev;

if (current == *head) *head = current->next;

free(current);

current = next;

}
}

• This function deletes all nodes with negative values from the list.

• It iterates through the list, checking each node’s data.

• If a node’s data is negative, it adjusts the pointers of the previous and next nodes to bypass
the current node.

• If the node to be deleted is the head, it updates the head pointer.

• Finally, it frees the memory allocated for the node.

3. Sort the List

void sortList(Node** head) {

if (*head == NULL) return;

Node* i, *j;

int temp;

for (i = *head; i->next != NULL; i = i->next) {

for (j = i->next; j != NULL; j = j->next) {

if (i->data > j->data) {

temp = i->data;

i->data = j->data;

j->data = temp;

• This function sorts the list in ascending order using a simple bubble sort algorithm.

• It uses two nested loops to compare each pair of nodes and swaps their data if they are out
of order.

4. Insert a Value While Maintaining Sorted Order

void insertSorted(Node** head, int value) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = value;

if (*head == NULL || (*head)->data >= value) {

newNode->next = *head;

newNode->prev = NULL;
if (*head != NULL) (*head)->prev = newNode;

*head = newNode;

return;

Node* current = *head;

while (current->next != NULL && current->next->data < value) {

current = current->next;

newNode->next = current->next;

if (current->next != NULL) current->next->prev = newNode;

current->next = newNode;

newNode->prev = current;

• This function inserts a new node with the given value into the list while maintaining the
sorted order.

• It creates a new node and finds the correct position for it by traversing the list.

• It adjusts the pointers of the surrounding nodes to insert the new node in the correct
position.

5. Reverse the List with Allocations

Node* reverseWithAllocations(Node* head) {

Node* newHead = NULL;

Node* current = head;

while (current != NULL) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = current->data;

newNode->next = newHead;

newNode->prev = NULL;

if (newHead != NULL) newHead->prev = newNode;

newHead = newNode;

current = current->next;

return newHead;
}

• This function creates a new list that is the reverse of the original list.

• It iterates through the original list, creating new nodes and inserting them at the beginning of
the new list.

• This effectively reverses the order of the nodes.

6. Reverse the List without Allocations

void reverseWithoutAllocations(Node** head) {

Node* temp = NULL;

Node* current = *head;

while (current != NULL) {

temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

if (temp != NULL) *head = temp->prev;

• This function reverses the list in place without allocating new memory.

• It swaps the next and prev pointers of each node.

• After the loop, it updates the head pointer to the new head of the reversed list.

7. Release the List

void releaseList(Node** head) {

Node* current = *head;

Node* next;

while (current != NULL) {

next = current->next;

free(current);

current = next;

*head = NULL;

}
• This function frees the memory allocated for all nodes in the list.

• It iterates through the list, freeing each node and setting the head pointer to NULL.

8. Print the List

void printList(Node* head) {

Node* current = head;

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

• This function prints the data of each node in the list.

• It iterates through the list, printing each node’s data.

9. Main Function

int main() {

Node* head = NULL;

insertSorted(&head, 5);

insertSorted(&head, -3);

insertSorted(&head, 10);

insertSorted(&head, 1);

insertSorted(&head, -1);

printf("Original list: ");

printList(head);

deleteNegativeNodes(&head);

printf("After deleting negative nodes: ");

printList(head);

sortList(&head);
printf("After sorting: ");

printList(head);

insertSorted(&head, 7);

printf("After inserting 7: ");

printList(head);

Node* reversedHead = reverseWithAllocations(head);

printf("Reversed with allocations: ");

printList(reversedHead);

reverseWithoutAllocations(&head);

printf("Reversed without allocations: ");

printList(head);

releaseList(&head);

releaseList(&reversedHead);

return 0;

• The main function demonstrates the use of all the functions defined above.

• It creates a list, performs various operations on it, and prints the list after each operation.

• Finally, it releases the memory allocated for the list.

You might also like