-->

Tuesday, March 4, 2014

Representing STACK using one-dimensional ARRAY in 'C'

Representing STACK using one-dimensional ARRAY 

             STACK can be represented either horizontally or vertically as consecutive memory cells if it is implemented by Array. A variable TOP is used to store the current index. Initially when the STACK is empty TOP contains a 0 index when the lower bound of array is 1, it can be stored with -1 if the lower bound is 0.
   
Horizontal representation:


When ITEM is to be PUSHed, TOP is incremented by 1 and at TOP position of Array the ITEM is placed. When TOP is equal to N, the size of STACK, and PUSH operation is done, then ‘Overflow’ occurs. If an ITEM is to be POPped from the STACK, the ITEM placed at TOP index is removed (deleted) and TOP is decremented by 1. Remember here when deletion is done, the items stored in array are not changed only the index is changed. When POP operation is applied to an empty STACK (STACK is empty when TOP is 0) ‘Underflow’ occurs. 

Example 1: Consider a STACK of size 6.
PUSH 6 on to STACK. TOP is incremented by 1 and 6 is placed in the STACK at TOP position. TOP<---TOP+1 and STACK [TOP]<---6.
PUSH 7and 3 on STACK. To PUSH 7 TOP is incremented by 1 and 7 is placed at TOP position that is at second position. To PUSH 3 again TOP is incremented by 1 and 3 is placed at TOP position that is at third position. After these two operations the STACK becomes:
POP the STACK. To POP the ITEM stored at TOP index is copied in a variable, ITEM<---STACK[TOP],  and TOP is decremented by 1.
Example 2 Consider a STACK of size 8. The string “INDIA” CAN BE reversed using STACK.
                                                                              
Now repeatedly POP till the STACK is empty and write the POPped characters you will get the reversed string, AIDNI
Example 3: Consider a STACK of size 8. Using the STACK convert a decimal number 52 to binary number.

½=0, STOP. STACK creation is over. Now repeatedly POP till STACK is empty and write the POPped number you will get the binary equivalent of 52 i.e. 110100. 

       STACK is also used to evaluate a POSTFIX expression value and to convert an INFIX expression to POSTFIX expression.

STACK for Data Structure in 'C' Programming

STACK

Stack is a special type of linear data structure, which can be implemented using either one-dimensional array or Linked List. If the Insertion operation and deletion operations are restricted to one end in one-dimensional array or linked list, then it becomes a specialized data structure STACK. The end, from which insertion and deletion are done, is called as TOP end. The insertion operation on the STACK is called as PUSH operation and deletion is called as POP operation. Remember that both the operations are done to the same end in the sense, for insertion and deletion operation is applied to the same index (top) in one-dimensional array and same pointer (top) in Linked List.
             As an ITEM (information) may be a number or character, depending on the type of array or node of linked list, is PUSHed on STACK from the TOP end and deletion (POPping) if any is to be done from the same end. The ITEM coming out after the POPping is the one just PUSHed, so the STACK is also called as LIFO, Last In First Out, structure . The ITEM PUSHed in Last is POPped out First.

       In your home you can find a STACK of cloths, plates, cassettes etc.

             STACK is a specialized linear data structure, which is having so many applications in computer field. STACK is used in Function calls, Recursion, Decimal number to binary number conversion and in expression evaluation.

XML: Presenting Data in Different Formats

Information pertaining to the way in which XML data must appear in a browser is specified using either CSS or XSLT style sheets. In certain situations, these two methods do not have the capability to display data in certain formats. For example, it is not feasible to display data in the form of tables using either of these documents. In such situations, it is both easier and less time-consuming to use other markup languages that are primarily created to display information in different formats in a browser.

Displaying Data in a Tabular Format

HTML is a markup language that is primarily used for data display. It provides a variety of predefined tags that can be used to display information in a web browser. You can combine the features of HTML and XSLT to format the data from an XML document for appropriate display.

Elements Required to Display Data in a Tabular Format

In order to use HTML tags in an XSLT style sheet, you first write the HTML code to display the data in the desired format. You then embed the HTML code in the XSLT document. The following table lists the HTML elements that are required to display data in a tabular format.

HTML Tag
Description
TABLE
Acts as a container for all other tags used to specify the appearance of data in a table. It has attributes, such as border, background color, cell-padding, cell-spacing, and width that enable you to specify the appearance of the table.
THEAD
Used to specify headings for a table.
TBODY
Used as a parent for the TR and TD elements.
TR
Used to represent a row in a table. This tag acts as a container for the TH and TD elements.
TH
Used to add column headings.
TD
Used to specify the data to be displayed in columns.

Monday, March 3, 2014

Increment and Decrement Operators in Java Programming

Earlier article was about the operators in java with some of the binary operators, example of each. This article is about plus (+) operator and increment/decrement operator in brief.

Operator + with strings

Programmer have used the operator ‘+’ with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them for example:

(5 + 6) results in to 11.
(“5” + “6”) results in to “56”.
(“17” + “A, V. Vihar”) result in to “17 A, V. Vihar”
(“abc” + “123”) results in to “abc 123”
(“” + 5 + “xyz”) results in to “5xyz”
(“” + 5) results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.

In other words,
a = a + 1;        same as   ++a ; or a++;
And
a = a – 1          same as --a ; or a --;

However, both the increment and decrement comes into two varieties: they may either precede of=r follow the operand. The prefix version comes before the operand (as in ++a or --a) and the postfix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression, that would be discussed in later articles.

About the Default Create Action Methods in MVC Controller

Earlier article was about the default index action of the controller, which in other words used to list the records from the database created in MVC application. Except this list (Index) action, all other actions are of two types i.e. Get and Post, having particular motive in the programming.

According to name, Get method is used to get a particular model and return to the view that will show that model. The below code will return the blank model of Student class (Controller). User will enter particular information in the textboxes shown on the form and click on submit button.

// GET: /Student/Create
public ActionResult Create()
{
return View();
}

After clicking on the submit button, compiler goes to the post method of the same action i.e. Post Create action, as written below:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}

The first two line of this code are attributes used to tell the compiler about this method i.e. this is post method and validates fields on submit method. The parameter is of type student means the view is of Student type (having all the fields of Student class) as shown some lines of the create view:

@model MvcApplication1.Models.Student
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
……………….
……………….
……………….

This view is strongly typed view because of the first line and have all the fields of student class (only name is shown here, remaining are something like this).

So, turn back on our previous discussion that was about the post Create action. This action will then check the model is valid or not (Validation). If valid then it will add this new student in the student table and then save the changes to the database. If not it will return to the same page with the errors generated.

The last line will return to the same action i.e. Create with the model passed as parameter and of course validation errors, you can check this to not enter some fields in the form and submit the data. The next will be to edit the student.

C program to read and display a polynomial using Linked List

C program to read and display a polynomial using Linked List:
#inculde<stdio.h>
#inculde<stdlib.h>
#inculde<conio.h>
struct node
{
int coeft;
int degree;
struct node *link;
};
typedef struct node poly;
main()
{
poly *root,*temp,*new;
int hdegree, coeft;
root=NULL;
clrscr();
printf(“Enter the highest degree of polynomial:”)
scanf(“%d”,&hdegree);
while(hdegree>=0)
{
printf(“Enter coefficient of variable with degree %d”,hdegree);
scanf(“%d”,&coeft);
if(coeft!=0)
{
new=(poly*)malloc(sizeof(poly));
if(new= =NULL)
{
printf(“Memory allocation error…”); exit(0);
}
new->coeft=coeft;
new->degree=hdegree;
new->link=NULL;
if(root= =NULL)
{
root=new;
temp=root;
}
else
{
temp->link=new;
temp=new;
}
}
hdegree--;
}
clrscr();
printf(“\n The Polynomial is:\n\n”);
temp=root;
while(temp!=NULL)
{
if(temp->coeft>0)
 printf(“+%dx%d”,temp->coeft,temp->degree);
else
  printf(“%dx%d”,temp->coeft,temp->degree);
temp=temp->link;
}
 getch();
}

Subtraction assignment operator in c# programming

This is type of binary operator, which means it works with more than one operand. In c#, this can be understood from given code
int a=10,b=5;
a -=b;
Console.WriteLine(a);
In this preceding code, we get 5 as a result. It means subtract operand b from a. This types of expression is equivalent to a=a-b. Lets take an simple example with output

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            b -= a;
            Console.WriteLine(b);
         
            Console.ReadKey();

        }
    }
}

Code generate the following output

Subtraction assignment operator in c# programming
© Copyright 2013 Computer Programming | All Right Reserved