Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

1,

public Item get(int size){


if(head == null || size < 0){
throw new UnsupportedOperationException();
}
else{
Node n = head;
for(int i=0; i <=size;i++){
if(n == null){
throw new UnsupportedOperationException();
}
if(i != 0){
n = n.next();
}
}
return n.getItem();
}
return null;
}
Get this book in print▼
0 ReviewsWrite review

A Quick Reference to DATA STRUCTURES and COMPUTER ALGORITHMS:

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 class Percolation


{
public int N;
private boolean[][] grid;

public Percolation(int N) //
{
checkNegative(N);
grid = new boolean[N][N];
}

public void checkBounds(int i, int j)


{
if(( i > N )||( j > N))
{
throw new java.lang.IndexOutOfBoundsException("Index out of bounds- " + i + "," + j + " out of bounds");
}
else
return;
}

public void checkNegative(int N)


{
if( N <= 0)
throw new java.lang.IllegalArgumentException("Number of sites less than 1");
}

public Percolation(int N)
{

checkNegative(N);
grid = new boolean[N][N];
}

public void open(int i, int j)


{

checkBounds(i,j);

if (isOpen(i,j) == true)
return;
else
grid[i][j] = true;
}

public boolean isOpen(int i, int j) // is site (row i, column j) open?


{
checkBounds(i,j);

return grid[i][j];
}

public boolean isFull(int i, int j)


{
checkBounds(i,j);

return grid[i][j];
}

public boolean percolates() // does the system percolate?


{
return false;
}
public static void main(String[] args) // test client (optional)
{
int N;
System.out.println("Enter grid length");
Scanner in = new Scanner(System.in);
N = in.nextInt();

Percolation Perc = new Percolation(N);

System.out.println("N is " + N);


System.out.println(Perc.isOpen(2,3));
}
}/////main part
// A complete working Java program to demonstrate all insertion
methods
// on linked list
class LinkedList
{
    Node head;  // head of list
 
    /* Linked list Node*/
    class Node
    {
        int data;
        Node next;
        Node(int d) {data = d; next = null; }
    }
 
    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
    {
        /* 1 & 2: Allocate the Node &
                  Put in the data*/
        Node new_node = new Node(new_data);
 
        /* 3. Make next of new Node as head */
        new_node.next = head;
 
        /* 4. Move the head to point to new Node */
        head = new_node;
    }
 
    /* Inserts a new node after the given prev_node. */
    public void insertAfter(Node prev_node, int new_data)
    {
        /* 1. Check if the given Node is null */
        if (prev_node == null)
        {
            System.out.println("The given previous node cannot be
null");
            return;
        }
 
        /* 2 & 3: Allocate the Node &
                  Put in the data*/
        Node new_node = new Node(new_data);
 
        /* 4. Make next of new Node as next of prev_node */
        new_node.next = prev_node.next;
 
        /* 5. make next of prev_node as new_node */
        prev_node.next = new_node;
    }
    
    /* Appends a new node at the end.  This method is
       defined inside LinkedList class shown above */
    public void append(int new_data)
    {
        /* 1. Allocate the Node &
           2. Put in the data
           3. Set next as null */
        Node new_node = new Node(new_data);
 
        /* 4. If the Linked List is empty, then make the
              new node as head */
        if (head == null)
        {
            head = new Node(new_data);
            return;
        }
 
        /* 4. This new node is going to be the last node, so
              make next of it as null */
        new_node.next = null;
 
        /* 5. Else traverse till the last node */
        Node last = head;
        while (last.next != null)
            last = last.next;
 
        /* 6. Change the next of last node */
        last.next = new_node;
        return;
    }
 
    /* 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)
    {
        /* Start with the empty list */
        LinkedList llist = new LinkedList();
 
        // Insert 6.  So linked list becomes 6->NUllist
        llist.append(6);
 
        // Insert 7 at the beginning. So linked list becomes
        // 7->6->NUllist
        llist.push(7);
 
        // Insert 1 at the beginning. So linked list becomes
        // 1->7->6->NUllist
        llist.push(1);
 
        // Insert 4 at the end. So linked list becomes
        // 1->7->6->4->NUllist
        llist.append(4);
 
        // Insert 8, after 7. So linked list becomes
        // 1->7->8->6->4->NUllist
        llist.insertAfter(llist.head.next, 8);
 
        System.out.println("\nCreated Linked list is: ");
        llist.printList();
    }
}
// Th

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

You might also like