mirror of
https://github.com/trailofbits/algo.git
synced 2025-08-15 17:23:07 +02:00
Create Linked List
This commit is contained in:
parent
3468d27e61
commit
25bc68bf34
1 changed files with 64 additions and 0 deletions
64
Linked List
Normal file
64
Linked List
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
/* structure for a node */
|
||||||
|
struct Node
|
||||||
|
{
|
||||||
|
int data;
|
||||||
|
struct Node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Function to insert a node at the begining of a Circular
|
||||||
|
linked list */
|
||||||
|
void push(struct Node **head_ref, int data)
|
||||||
|
{
|
||||||
|
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node));
|
||||||
|
struct Node *temp = *head_ref;
|
||||||
|
ptr1->data = data;
|
||||||
|
ptr1->next = *head_ref;
|
||||||
|
|
||||||
|
/* If linked list is not NULL then set the next of last node */
|
||||||
|
if (*head_ref != NULL)
|
||||||
|
{
|
||||||
|
while (temp->next != *head_ref)
|
||||||
|
temp = temp->next;
|
||||||
|
temp->next = ptr1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ptr1->next = ptr1; /*For the first node */
|
||||||
|
|
||||||
|
*head_ref = ptr1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Function to print nodes in a given Circular linked list */
|
||||||
|
void printList(struct Node *head)
|
||||||
|
{
|
||||||
|
struct Node *temp = head;
|
||||||
|
if (head != NULL)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
printf("%d ", temp->data);
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
while (temp != head);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Driver program to test above functions */
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* Initialize lists as empty */
|
||||||
|
struct Node *head = NULL;
|
||||||
|
|
||||||
|
/* Created linked list will be 11->2->56->12 */
|
||||||
|
push(&head, 12);
|
||||||
|
push(&head, 56);
|
||||||
|
push(&head, 2);
|
||||||
|
push(&head, 11);
|
||||||
|
|
||||||
|
printf("Contents of Circular Linked List\n ");
|
||||||
|
printList(head);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue