-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linkedlist.cpp: Remove space inconsistencies
This commit removes the space inconsistencies detected by coala Closes #369
- Loading branch information
Rajeev R Menon
committed
Oct 7, 2018
1 parent
9ed6fc4
commit abafb42
Showing
1 changed file
with
60 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,82 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// A linked list node | ||
|
||
struct Node | ||
{ | ||
int data; | ||
struct Node *next; | ||
struct Node{ | ||
int data; | ||
struct Node *next; | ||
}; | ||
/* | ||
Given a reference (pointer to pointer) to the head of a list | ||
and an int, inserts a new node on the front of the list. | ||
*/ | ||
|
||
void push(struct Node** head_ref, int new_data) | ||
{ | ||
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); | ||
new_node->data = new_data; | ||
new_node->next = (*head_ref); | ||
(*head_ref) = new_node; | ||
*/ | ||
|
||
void push(struct Node** head_ref, int new_data){ | ||
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); | ||
new_node->data = new_data; | ||
new_node->next = (*head_ref); | ||
(*head_ref) = new_node; | ||
} | ||
|
||
/* | ||
Given a reference (pointer to pointer) to the head of a list | ||
and a position, deletes the node at the given position | ||
*/ | ||
|
||
void deleteNode(struct Node **head_ref, int position) | ||
{ | ||
// If linked list is empty | ||
if (*head_ref == NULL) | ||
return; | ||
// Store head node | ||
struct Node* temp = *head_ref; | ||
// If head needs to be removed | ||
if (position == 0) | ||
{ | ||
*head_ref = temp->next; | ||
free(temp); | ||
return; | ||
} | ||
|
||
// Find previous node of the node to be deleted | ||
for (int i = 0; temp != NULL && i < position - 1; i++) | ||
temp = temp->next; | ||
// If position is more than number of ndoes | ||
if (temp == NULL || temp->next == NULL) | ||
return; | ||
// Node temp->next is the node to be deleted | ||
// Store pointer to the next of node to be deleted | ||
struct Node *next = temp->next->next; | ||
|
||
// Unlink the node from linked list | ||
free(temp->next); | ||
temp->next = next; | ||
void deleteNode(struct Node **head_ref, int position){ | ||
// If linked list is empty | ||
if (*head_ref == NULL) | ||
return; | ||
// Store head node | ||
struct Node* temp = *head_ref; | ||
// If head needs to be removed | ||
if (position == 0){ | ||
*head_ref = temp->next; | ||
free(temp); | ||
return; | ||
} | ||
|
||
// Find previous node of the node to be deleted | ||
for (int i = 0; temp != NULL && i < position - 1; i++) | ||
temp = temp->next; | ||
|
||
// If position is more than number of ndoes | ||
if (temp == NULL || temp->next == NULL) | ||
return; | ||
|
||
// Node temp->next is the node to be deleted | ||
// Store pointer to the next of node to be deleted | ||
struct Node *next = temp->next->next; | ||
|
||
// Unlink the node from linked list | ||
free(temp->next); | ||
temp->next = next; | ||
} | ||
|
||
// This function prints contents of linked list starting from | ||
// the given node | ||
void printList(struct Node *node) | ||
{ | ||
while (node != NULL) | ||
{ | ||
printf(" %d ", node->data); | ||
node = node->next; | ||
} | ||
void printList(struct Node *node){ | ||
while (node != NULL){ | ||
printf(" %d ", node->data); | ||
node = node->next; | ||
} | ||
} | ||
|
||
/* Drier program to test above functions*/ | ||
int main() | ||
{ | ||
/* Start with the empty list */ | ||
struct Node* head = NULL; | ||
|
||
push(&head, 7); | ||
push(&head, 1); | ||
push(&head, 3); | ||
push(&head, 2); | ||
push(&head, 8); | ||
|
||
puts("Created Linked List: "); | ||
printList(head); | ||
deleteNode(&head, 4); | ||
puts("\nLinked List after Deletion at position 4: "); | ||
printList(head); | ||
return 0; | ||
int main(){ | ||
/* Start with the empty list */ | ||
struct Node* head = NULL; | ||
|
||
push(&head, 7); | ||
push(&head, 1); | ||
push(&head, 3); | ||
push(&head, 2); | ||
push(&head, 8); | ||
|
||
puts("Created Linked List: "); | ||
printList(head); | ||
deleteNode(&head, 4); | ||
puts("\nLinked List after Deletion at position 4: "); | ||
printList(head); | ||
return 0; | ||
} |
abafb42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment on abafb42.
There are 31 results for the section all.cpplint. They have been shortened and will not be shown inline because they are more than 10.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.