Professional Documents
Culture Documents
0 Reviewswrite Review: Void Int Void Int
0 Reviewswrite Review: Void Int Void Int
1. #include<stdio.h>
2. #include<stdlib.h>
3. void randominsert(int);
4. void create(int);
5. struct node
6. {
7. int data;
8. struct node *next;
9. };
10. struct node *head;
11. void main ()
12. {
13. int choice,item,loc;
14. do
15. {
16. printf("\nEnter the item which you want to insert?\n");
17. scanf("%d",&item);
18. if(head == NULL)
19. {
20. create(item);
21. }
22. else
23. {
24. randominsert(item);
25. }
26. printf("\nPress 0 to insert more ?\n");
27. scanf("%d",&choice);
28. }while(choice == 0);
29. }
30. void create(int item)
31. {
32.
33. struct node *ptr = (struct node *)malloc(sizeof(struct node *));
34. if(ptr == NULL)
35. {
36. printf("\nOVERFLOW\n");
37. }
38. else
39. {
40. ptr->data = item;
41. ptr->next = head;
42. head = ptr;
43. printf("\nNode inserted\n");
44. }
45. }
46. void randominsert(int item)
47. {
48. struct node *ptr = (struct node *) malloc (sizeof(struct node));
49. struct node *temp;
50. int i,loc;
51. if(ptr == NULL)
52. {
53. printf("\nOVERFLOW");
54. }
55. else
56. {
57.
58. printf("Enter the location");
59. scanf("%d",&loc);
60. ptr->data = item;
61. temp=head;
62. for(i=0;i<loc;i++)
63. {
64. temp = temp->next;
65. if(temp == NULL)
66. {
67. printf("\ncan't insert\n");
68. return;
69. }
70.
71. }
72. ptr ->next = temp ->next;
73. temp ->next = ptr;
74. printf("\nNode inserted");
75. }
76.
77. }
#include <stdio.h>
#include <stdlib.h>
// A Linked List Node
struct Node
{
int data;
struct Node* next;
};
// Helper function to print a 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 a new node at the beginning of the linked list
void push(struct Node** head, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// Helper function to return a new node of the linked list
struct Node* newNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a given node at its correct sorted position into
// a given list sorted in increasing order
void sortedInsert(struct Node** head, struct Node* newNode)
{
// special case for the head end
if (*head == NULL || (*head)->data >= newNode->data)
{
newNode->next = *head;
*head = newNode;
return;
}
// locate the node before the point of insertion
struct Node* current = *head;
while (current->next != NULL && current->next->data < newNode->data) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
int main(void)
{
// input keys
int keys[] = {2, 4, 6, 8};
int n = sizeof(keys)/sizeof(keys[0]);
// points to the head node of the linked list
struct Node* head = NULL;
// construct a linked list
for (int i = n-1; i >= 0; i--) {
push(&head, keys[i]);
}
sortedInsert(&head, newNode(5));
sortedInsert(&head, newNode(9));
sortedInsert(&head, newNode(1));
// print linked list
printList(head);
return 0;
}
public Percolation(int N) //
{
checkNegative(N);
grid = new boolean[N][N];
}
public Percolation(int N)
{
checkNegative(N);
grid = new boolean[N][N];
}
checkBounds(i,j);
if (isOpen(i,j) == true)
return;
else
grid[i][j] = true;
}
return grid[i][j];
}
return grid[i][j];
}
3333333333333333333.,,
class LinkedList {
Node head; // head of list
/* Linked list Node*/
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Given a key, deletes the first
occurrence of key in
* linked list */
void deleteNode(int key)
{
// Store head node
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null && temp.data == key) {
head = temp.next; // Changed head
return;
}
// Search for the key to be deleted, keep track of
// the previous node as we need to change temp.next
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null)
return;
// Unlink the node from linked list
prev.next = temp.next;
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
/* This function prints contents of linked list starting
from the given node */
public void printList()
{
Node tnode = head;
while (tnode != null) {
System.out.print(tnode.data + " ");
tnode = tnode.next;
}
}
/* Driver program to test above functions. Ideally this
function should be in a separate user class. It is kept
here to keep code compact */
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(7);
llist.push(1);
llist.push(3);
llist.push(2);
System.out.println("\nCreated Linked list is:");
llist.printList();
llist.deleteNode(1); // Delete node with data 1
System.out.println(
"\nLinked List after Deletion of 1:");
llist.printList();
}
}
Output
Created Linked List:
2 3 1 7
Linked List after Deletion of 1:
2 3 7