Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 154 additions & 155 deletions data_structures/linked_list/circular_linked_list.c
Original file line number Diff line number Diff line change
@@ -1,155 +1,154 @@
/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define NULL 0

/* Assume that the data portion of each node consists of ONLY an integer.*/
struct node
{
int data ;
struct node *next ;
} ;

struct node *first=NULL ;
struct node *last=NULL ;
/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */

/* This function is responsible for creating the Circularly Linked List right from the BEGINNING. */
void create()
{
int i , n ;
struct node *pnode , *p ;

printf("Enter the number of nodes required:\n") ;
scanf("%d",&n) ;

printf("Enter the data value of each node:\n") ;
for(i=1 ; i<=n ; i++)
{
pnode=(struct node*)malloc(sizeof(struct node)) ;
if(pnode==NULL)
{
printf("Memory overflow. Unable to create.\n") ;
return ;
}

scanf("%d",&pnode->data) ;

if(first==NULL)
first=last=pnode ;
else
{
last->next=pnode ;
last=pnode ; /* last keeps track of last node */
}

last->next=first ;
}
}

/* This function will delete a node with value k from the Linked List if such a node exists */
void deletenode(int k)
{
struct node *p , *follow ;

/* searching the required node */
p=first ;
follow=NULL ;
while(follow!=last)
{
if(p->data==k)
break ;
follow=p ;
p=p->next ;
}

if(follow==last)
printf("Required node not found.\n") ;
else
{
if(p==first&&p==last) /* deleting the one and the only node */
first=last=NULL ;
else if(p==first) /* deleting the first node */
{
first=first->next ;
last->next=first ;
}
else if(p==last) /* deleting the last node */
{
last=follow ;
last->next=first ;
}
else /* deleting any other node */
follow->next=p->next ;

free(p) ;
}
}

/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
void traverse()
{
struct node *p , *follow ;
if(first==NULL)
printf("Circularly Linked List Empty") ;
else
{
printf("Circularly Linked List is as shown: \n") ;

p=first ;
follow = NULL ;
while(follow!=last)
{
printf("%d " , p->data) ;
follow=p ;
p=p->next ;
}

printf("\n") ;
}
}

void main()
{
int x , k , ch ;
clrscr() ;
do
{
printf("\n Menu: \n") ;
printf("1:Create Linked List \n") ;
printf("2:Delete Node \n") ;
printf("3:Traverse \n") ;
printf("4:Exit \n") ;

printf("\nEnter your choice: ") ;
scanf("%d",&ch) ;

switch(ch)
{
case 1:
create() ;
break ;

case 2:
printf("Enter the data value of the node to be deleted: ") ;
scanf("%d",&k) ;
deletenode(k) ;
break ;

case 3:
traverse() ;
break ;

case 4:
break ;
}
}
while(ch!=4) ;

getch() ;
}



/* Circularly Linked List (Basic Operations) - Program to create a Circularly linked list abstract data type and perform various operations on it (Variable first and last declared globally) */

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#define NULL 0

/* Assume that the data portion of each node consists of ONLY an integer.*/
struct node
{
int data ;
struct node *next ;
} ;

struct node *first=NULL ;
struct node *last=NULL ;
/* first and last are global variables and need not be passed to any function. Any changes made to variables first and last by any of the functions in the program will be reflected in the entire program */

/* This function is responsible for creating the Circularly Linked List right from the BEGINNING. */
void create()
{
int i , n ;
struct node *pnode , *p ;

printf("Enter the number of nodes required:\n") ;
scanf("%d",&n) ;

printf("Enter the data value of each node:\n") ;
for(i=1 ; i<=n ; i++)
{
pnode=(struct node*)malloc(sizeof(struct node)) ;
if(pnode==NULL)
{
printf("Memory overflow. Unable to create.\n") ;
return ;
}

scanf("%d",&pnode->data) ;

if(first==NULL)
first=last=pnode ;
else
{
last->next=pnode ;
last=pnode ; /* last keeps track of last node */
}

last->next=first ;
}
}

/* This function will delete a node with value k from the Linked List if such a node exists */
void deletenode(int k)
{
struct node *p , *follow ;

/* searching the required node */
p=first ;
follow=NULL ;
while(follow!=last)
{
if(p->data==k)
break ;
follow=p ;
p=p->next ;
}

if(follow==last)
printf("Required node not found.\n") ;
else
{
if(p==first&&p==last) /* deleting the one and the only node */
first=last=NULL ;
else if(p==first) /* deleting the first node */
{
first=first->next ;
last->next=first ;
}
else if(p==last) /* deleting the last node */
{
last=follow ;
last->next=first ;
}
else /* deleting any other node */
follow->next=p->next ;

free(p) ;
}
}

/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
void traverse()
{
struct node *p , *follow ;
if(first==NULL)
printf("Circularly Linked List Empty") ;
else
{
printf("Circularly Linked List is as shown: \n") ;

p=first ;
follow = NULL ;
while(follow!=last)
{
printf("%d " , p->data) ;
follow=p ;
p=p->next ;
}

printf("\n") ;
}
}

int main(void)
{
int x , k , ch ;
do
{
printf("\n Menu: \n") ;
printf("1:Create Linked List \n") ;
printf("2:Delete Node \n") ;
printf("3:Traverse \n") ;
printf("4:Exit \n") ;

printf("\nEnter your choice: ") ;
scanf("%d",&ch) ;

switch(ch)
{
case 1:
create() ;
break ;

case 2:
printf("Enter the data value of the node to be deleted: ") ;
scanf("%d",&k) ;
deletenode(k) ;
break ;

case 3:
traverse() ;
break ;

case 4:
break ;
}
}
while(ch!=4) ;

return 0;
}



1 change: 1 addition & 0 deletions exercism/acronym/acronym.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *abbreviate(const char *phrase)
{
Expand Down
1 change: 1 addition & 0 deletions leetcode/src/1.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
int i, j;
Expand Down
1 change: 1 addition & 0 deletions leetcode/src/10.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
/*
Prompt:

Expand Down
14 changes: 6 additions & 8 deletions leetcode/src/1008.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

struct TreeNode* bstFromPreorder(int* preorder, int preorderSize)
{
Expand Down
31 changes: 16 additions & 15 deletions leetcode/src/1009.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Bit manipulation.
// - Find the bit length of n using log2
// - Create bit mask of bit length of n
// - Retun ~n and bit of ones mask
// Runtime: O(log2(n))
// Space: O(1)

int bitwiseComplement(int n){
if (n == 0){
return 1;
}

int binary_number_length = ceil(log2(n));
return (~n) & ((1 << binary_number_length) - 1);
}
#include <math.h>
// Bit manipulation.
// - Find the bit length of n using log2
// - Create bit mask of bit length of n
// - Retun ~n and bit of ones mask
// Runtime: O(log2(n))
// Space: O(1)

int bitwiseComplement(int n){
if (n == 0){
return 1;
}

int binary_number_length = ceil(log2(n));
return (~n) & ((1 << binary_number_length) - 1);
}
2 changes: 2 additions & 0 deletions leetcode/src/101.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdbool.h>
#include <stdlib.h>
/**
* Definition for a binary tree node.
* struct TreeNode {
Expand Down
12 changes: 5 additions & 7 deletions leetcode/src/1019.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};

int* nextLargerNodes(struct ListNode* head, int* returnSize)
{
Expand Down
Loading
Loading