Knowledge in Data Structures & algorithms

Queue Implementation Using Stacks

#include<stdio.h> #define size 100 int queue[size],x,y,z; int front=-1,rear=-1; int dequeue() {     if(rear==-1)     {         printf("underflow");         exit(1);     }     else{         front++;         if(front>rear)         {             front=rear=-1;         }     } } void enqueue(int x) {     if(rear==size-1)     {         printf("overflow\n");         exit(1);     }     if(front==-1)     {         front++;     }     rear++;     queue[rear]=x; } void display() {     if(front==rear==-1)         printf("queue empty\n");     else     {         int i;         for(i=front;i<=rear;i++)         {             printf("%d\n",queue[i]);         }     } } int main() {     printf("the queue is :");     enqueue(1);     enqueue(2);         enqueue(90);     dequeue();     display(); }

Python Program for Selection Sort

def selectionSort( theSeq ):     n = len( theSeq )     for i in range( n - 1 ):         smallNdx = i         for j in range( i + 1, n ):             if theSeq[j] < theSeq[smallNdx] :                 smallNdx = j     if smallNdx != i :         tmp = theSeq[i]         theSeq[i] = theSeq[smallNdx]         theSeq[smallNdx] = tmp seq=[5,78,12,9,4,5] a=selectionSort(seq) print(a)

Java programming

Java language" redirects here. For the natural language from the Indonesian island of Java, see Javanese language. This article is about a programming language. For the software platform, see Java (software platform). For the software package downloaded from java.com, see Java SE. Not to be confused with JavaScript. Java is a general-purpose computer-programming language that is concurrent, class-based, object-oriented,[15] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA),[16] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[17] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. As of 2016, Java is one of the most popular programming languages in use,[18][19][20][21] particularly for client-server web applications, with a reported 9 million developers.[22] Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them

Python

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.[26] In July 2018, Van Rossum stepped down as the leader in the language community.[27][28]

Delete A Value Linked List Program Using C

#include<stdio.h> #include<stdlib.h> int np; struct Node{     int data;     struct Node* next; }; void value_key(struct Node** head_ref,int value) {     int i;     struct Node* temp=*head_ref;     for(i=0;i<=value;i++)     {         if(i==value)         {             //printf("%d",i);             printf("\nvalue at provided posn is %d\n",temp->data);             np=temp->data;         }         else         temp=temp->next;         //printf("%d",main_key);     } } void delete1(struct Node **head_ref,int key) {     struct Node* temp=*head_ref,*prev;     if(temp!=NULL && temp->data==key)     {         *head_ref=temp->next;         free(temp);         return;     }     while(temp!=NULL && temp->data!=key)     {         prev=temp;         temp=temp->next;     }     if(temp==NULL)   //not in list     {         return;     }     prev->next=temp->next;     free(temp); } void push(struct Node **head_ref,int y) {     struct Node *new1=(struct Node*)malloc(sizeof(struct Node));     new1->data=y;     new1->next=*head_ref;     *head_ref=new1; } int main() {     struct Node *head=NULL;     push(&head,7);     push(&head,1);     push(&head,9);     push(&head,12);     value_key(&head,0);    // delete1(&head,1);    //struct Node *temp =head;     delete1(&head,np);     while(head!=NULL)     {         printf("%d\n",head->data);         head=head->next;     }     return 0; }

Linked List Length Using C Program

#include<stdio.h> struct node {     int data;     struct node* next; }; void getlength(struct node** head_ref) {     struct node *temp=*head_ref;     int count=0;     while(temp!=NULL)     {         temp=temp->next;         count++;     }     printf("total number of nodes are %d",count); } void push(struct node **head_ref,int x) {     struct node* new1;     new1=(struct node*)malloc(sizeof(struct node));     new1->data=x;     new1->next=*head_ref;     *head_ref=new1; } void delete1(struct node** head_ref,int key) {     struct node* temp=*head_ref;     struct node *prev;     while(temp!=NULL)     {         prev=temp;         temp=temp->next;         if(temp->data==key)         {             break;         }     }     prev->next=temp->next;     free(temp); } int main() {     struct node* head=NULL;     push(&head,89);     push(&head,65);     push(&head,23);     delete1(&head,89);     getlength(&head); }

How to find the length of an Array in C#

using System; namespace viden {    class GFG {        // Main Method     public static void Main()     {            // declares a 1D Array of string.         string[] weekDays;            // allocating memory for days.         weekDays = new string[] {"Sun", "Mon", "Tue", "Wed",                                        "Thu", "Fri", "Sat"};            // Displaying Elements of the array         foreach(string day in weekDays)             Console.Write(day + " ");            Console.Write("\nTotal Number of Elements: ");            // using Length property         Console.Write(weekDays.Length);     } } }

Logic Theory —Basic Notation

The origin of logic theory starts at the concept of an argument. The majority of logic textbooks contain an opening, central definition for an argument — one that likely sounds much like the following: An argument contains one or more special statements, called premises , offered as a reason to believe that a further statement, called the conclusion, is true Premises are the atoms of logic theory: everything is built up from them. A premise is a declarative statement that must strictly evaluate to only either true or false. A single premise is referred to as a primitive premise — there are 50 states in the U.S.A (which is true). Connecting multiple premises together form a compound premise — there are 50 states in the U.S.A and it snowed in Miami today (which is false). How does one connect multiple statements? Like you see in the previous example, with operators that you’re already familiar with but that require their own language & syntax. Connectives Similar to other branches of math, premises have their own set of fundamental operators (adding, subtracting, etc…). In logic theory, five basic logical connectors, collectively known as connectives, fill this role. They’re summarized in the table below, assume the letters P & Q represent two primitive premises: If you’ve been exposed to programming at any level, then it’s highly likely that the table above seems at least vaguely familiar. This is because connectives are at the very core of common language syntax & almost always have some special character designated for each connective (&& = and, | = or, etc…). Which of the five connectives is used as a logical connector between two premises determines the overall truth value of the compound statement based on the truth values of the premises being modified. An important principle here that might seem counter-intuitive at first is worth extracting: when analyzing compound statements it’s not necessary to know what parts P & Q actually say, only whether those parts are true or false. Implication Of the five connectives, one is immediately worthy of further inspection — the implication, aka, if-then statements. The implication is a connective with the standard form of P → Q where P is known as the hypothesis (or antecedent), & Q is known as the conclusion (or consequent). While the implication has a standard form defined above, there exists three other common types of conditionals that are worth reviewing. The following four conditionals are simple yet quite common & powerful compound statements made by combining conditionals with the core connectives introduced: A conditional is itself a compound premise, that is, it strictly evaluates to either true or false. For any implication, just like for any other connective, the truth value of the compound premise is determined by the truth values of its two independent premises. Corresponding with the definitions introduced above, for example, an implication is true either when the hypothesis is false, or when the conclusion is true; which leaves only one way for an implication to be false: when the hypothesis is true & the conclusion is false. If that seemed like a lot to track mentally, as it did for me, then breath easy & rest assure that powerful tools lay in the near future that makes analyzing complex conditionals as simple as following a blueprint. The main tool we’ll use is a nifty logic-101 tool by the name of truth tables. Before we get into truth tables, however, let’s make a quick detour to fill one last gap in our knowledge of basic logic theory notation. Inspect a peculiar scenario — is the following statement a premise? x is larger than one hundred Quantifiers Under our strict definition introduced in the opening paragraph, a premise must evaluate to either true or false — the statement cannot be ambiguous or left open-ended. Which means that variables, as we’re used to seeing them since algebra, is a no-no in logic theory; not at least without some modification. The bolded statement above is not considered a premise as x could be 5 or 25, making the statement true or false, but currently neither. This, however, doesn’t mean that we have to delete variables from our tool set altogether. There is a way to make use of variables; the process is called quantifying, a clever way of notating bounds on unknown variables in logic. Take a look at the following, updates statement — is this now a premise? for all x, x is larger than one hundred Now that we’ve defined the universe, or domain, of the variable, the statement is no longer ambigious — it’s now a premise as it evaluates to categorically false. The use of this “for all x” is known in logic theory as applying a quantifier. There are two main types of quantifiers. The first, which we’ve just seen, is aptly named the universal quantifier. Dictated by an upside down “A”, ∀, it’s easy to remember that it stands for All or every possible instance within the universe of the statement made. Inspect this second alteration: there exists an x larger than one hundred Once again without removing the variables we’ve found a way to convert a statement into a premise by applying a quantifier as the statement now evaluates strictly to true. This second type of quantifier is known as the existential quantifier. Notated by a backwards “E”, ∃, it usually reads as “there exists” or “there is.” Both quantifiers are summarized below:

Operations On List

list.append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x]. list.extend(iterable) Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable. list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). list.remove(x) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item. list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.) list.clear() Remove all items from the list. Equivalent to del a[:]. list.index(x[, start[, end]]) Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument. list.count(x) Return the number of times x appears in the list. list.sort(key=None, reverse=False) Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation). list.reverse() Reverse the elements of the list in place. list.copy() Return a shallow copy of the list. Equivalent to a[:].

Linked List Python Program - 1

class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None def listprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval list = SLinkedList() list.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") list.headval.nextval = e2 e2.nextval = e3 list.listprint()

Python Program for Inserting at the End of the Linked List

class Node: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None class SLinkedList: def __init__(self): self.headval = None # Function to add newnode def AtEnd(self, newdata): NewNode = Node(newdata) if self.headval is None: self.headval = NewNode return laste = self.headval while(laste.nextval): laste = laste.nextval laste.nextval=NewNode # Print the linked list def listprint(self): printval = self.headval while printval is not None: print (printval.dataval) printval = printval.nextval list = SLinkedList() list.headval = Node("Mon") e2 = Node("Tue") e3 = Node("Wed") list.headval.nextval = e2 e2.nextval = e3 list.AtEnd("Thu") list.listprint()

Numeric Array In PHP

<html> <body> <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Value is $value <br />"; } /* Second method to create array. */ $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; $numbers[3] = "four"; $numbers[4] = "five"; foreach( $numbers as $value ) { echo "Value is $value <br />"; } ?> </body> </html>