C Programming: Combining Unordered Segments with Two Nodes
{"title": "C Programming: Combining Unordered Segments with Two Nodes", "description": "Learn how to combine two unordered segments in C using linked lists. This code demonstrates creating segments, nodes, combining segments, and printing the result. ", "keywords": "C programming, linked list, segment, node, combine, unordered, data structure", "content": "To combine two unordered segments in C with two nodes, you can create a struct to represent a node and another struct to represent a segment. Here's an example:\n\nc\n#include <stdio.h>\n#include <stdlib.h>\n\ntypedef struct Node {\n int data;\n struct Node* next;\n} Node;\n\ntypedef struct Segment {\n struct Node* head;\n struct Node* tail;\n} Segment;\n\n// Function to create a new node\nNode* createNode(int data) {\n Node* newNode = (Node*)malloc(sizeof(Node));\n newNode->data = data;\n newNode->next = NULL;\n return newNode;\n}\n\n// Function to create a new segment with two nodes\nSegment* createSegment(int data1, int data2) {\n Segment* newSegment = (Segment*)malloc(sizeof(Segment));\n newSegment->head = createNode(data1);\n newSegment->tail = createNode(data2);\n newSegment->head->next = newSegment->tail;\n return newSegment;\n}\n\n// Function to combine two segments\nSegment* combineSegments(Segment* segment1, Segment* segment2) {\n segment1->tail->next = segment2->head;\n segment1->tail = segment2->tail;\n free(segment2);\n return segment1;\n}\n\n// Function to print a segment\nvoid printSegment(Segment* segment) {\n Node* current = segment->head;\n while (current != NULL) {\n printf("%d \", current->data);\n current = current->next;\n }\n printf("\n");\n}\n\nint main() {\n // Create two segments\n Segment* segment1 = createSegment(1, 2);\n Segment* segment2 = createSegment(3, 4);\n\n // Combine the segments\n Segment* combinedSegment = combineSegments(segment1, segment2);\n\n // Print the combined segment\n printSegment(combinedSegment);\n\n // Clean up memory\n free(segment1->tail);\n free(segment1);\n free(combinedSegment);\n\n return 0;\n}\n\n\nIn this example, the Node struct represents a node in the segment, with a data member to store the value and a next member to point to the next node. The Segment struct represents a segment, with head and tail members pointing to the first and last nodes of the segment, respectively.\n\nThe createNode function creates a new node with the given data, and the createSegment function creates a new segment with two nodes initialized with the given data. The combineSegments function combines two segments by updating the tail of the first segment to point to the head of the second segment, and then freeing the memory occupied by the second segment.\n\nFinally, the printSegment function is used to print the values of the nodes in a segment. In the main function, two segments are created, combined using combineSegments, and then printed using printSegment. Memory is freed at the end to avoid memory leaks.\n\n"\n
原文地址: https://www.cveoy.top/t/topic/pyyI 著作权归作者所有。请勿转载和采集!