0% found this document useful (0 votes)
31 views3 pages

Htead

The document contains C code to copy a linked list. It defines a Node struct with data and next pointer fields. It includes functions to push nodes onto a list, print a list, and copy a list. The CopyList function iterates through the original list, pushes each node's data onto a new list using a dummy node, and returns the copy. It is tested by creating a sample linked list, copying it, and printing the copy.

Uploaded by

Leo Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Htead

The document contains C code to copy a linked list. It defines a Node struct with data and next pointer fields. It includes functions to push nodes onto a list, print a list, and copy a list. The CopyList function iterates through the original list, pushes each node's data onto a new list using a dummy node, and returns the copy. It is tested by creating a sample linked list, copying it, and printing the copy.

Uploaded by

Leo Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
yarp
hehesadasdasdasdasdasdsadsadasdsa
// Data Structure to store a linked list node
struct Node
{
int data;
struct Node* next;
};

// Helper function to print given linked list


void printList(struct Node* head)
{
struct Node* ptr = head;
while (ptr)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
}

printf("null");
}

// Helper function to insert new Node in the beginning of the linked list
void push(struct Node** head, int data)
{
// Allocate the new Node in the heap and set its data
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;

// Set the .next pointer of the new Node to point to the current
// first node of the list.
newNode->next = *head;

// Change the head pointer to point to the new Node, so it is


// now the first node in the list.
*head = newNode;
}

// Function that takes a linked list and returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)
// Function that takes a linked list and returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)// Function that takes a linked list and
returns a complete copy of that
// list using dummy node using push() function
struct Node* CopyList(struct Node* head)
{
struct Node* current = head; // used to iterate over original list
struct Node* newList = NULL; // head of the new list
struct Node* tail = NULL; // point to last node in new list

while (current != NULL)


{
// special case for the first new Node
if (newList == NULL)
{
push(&newList, current->data);
tail = newList;
}
else
{
push(&(tail->next), current->data); // add each node at tail
tail = tail->next; // advance the tail to new last
node
}
current = current->next;
}

return newList;
}

// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 3, 4};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

// copy linked list


struct Node* dup = CopyList(head);

// print duplicate linked list


printList(dup);

return 0;
}

You might also like