Sunday, March 10, 2013

Write C program to create and print Single linked list without using function


Write C program to create and print linked list without using function

struct node
{
   int data;
   struct node *link;
};

main()
{
  struct node *first,*temp;
  char ch=’y’;
  int ct=0;
//Creates Linked List 
  while (ch==’y’)
  {
        if (ct==0)
        {
           first=(struct node *)malloc(sizeof(struct node));
           printf("Enter data into the node: ");
           scanf("%d", &first->data);
           first->link=NULL;
           temp=first;
           ct++;
        }
        else
        {
           temp=first;
              while(temp->link!=NULL)
              {
                 temp=temp->link;
              }
             t=(struct node *)malloc(sizeof(struct node));
             printf("Enter data into the node: ");
             scanf("%d", &t->data);
             t->link=NULL;
             temp->link=t;
        }
      printf("Do you want to continue to create another node: ");
      fflush(stdin);
     ch=getchar();
   }

// Displaying Linked List
  temp=first;
    while (temp->link!=NULL)
    {
      printf("%d --> ",temp->data);
      temp=temp->link;
    }
    printf("%d -->NULL",temp->data);
 
 getch();
}

Linked List


Linked List

            In  a linked list, the elements are not stored in continuous memory locations i.e., the elements of linked list are non-continuous.  A linked list is represented as follows:
     Since the elements are non-continuous, the next element is stored in current element i.e., the address of second element is stored in first element.  The address of third element is stored in second element and so on.  In general, address of (i+1)th is stored in ith element.  This address is called Link or Pointer and  element is called Data. The combination Data and Link is called Node.  Typically, node is shown as follows:

  First field in the node is Data Field and Second field is link field.  The link field contains address of the next node.  In the above linked list, the address of second node is 2000, which is stored in link field of Ist node.  The address of the last node is 7000, which is stored in link field of last but one node.  The link field of the last node contains ‘Nil’, which  indicates the end of Linked List.  Nil is the pointer constant.  The address of the first node is stored in the pointer variable “First”.  First always points to the “First Node”.  In other words, ‘First’ is the pointer to the entire linked list.


 Operations on Linked List

Insertion
Case  1.  Insertion in the middle

            Whenever any particular node is to be inserted, all we need to do is change the link field of preceding node to point to new node and link field of new node point to succeeding node.  For example, if we want to insert a new node between ith and (i+1)th node, point ith node link to new node and new node link to (i+1)th node.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

                                                                 Before Insertion
                                                                  After Insertion
 
Case 2: Insertion at the Beginning

            Suppose, if we want to insert a new node at the beginning of Linked List, point new node link to first node and change “First” Pointer to new node.

Before Insertion
After Insertion

Case 3: Insertion at the End

            Suppose, if  we want to insert a new element at the end of the linked list, change link field of the last node to point to new node and new node link field contain Nil.
 Before Insertion


 After Insertion

Deletion

Case  1.  Deleting middle node from Linked List
           
            To delete ith node form linked list, change link field of (i-1)th node to (i+1)th node.

Before Deletion
After Deletion 

Case  2.  Deleting first node from Linked List

            To Delete first node, move “first’ pointer to next node.




Before Deletion
 After Deletion

Case  3.  Deleting Last node from the Linked List

            To delete last node change link field of last but one node to contain ‘Nil’

 Before Deletion
After Deletion

Thus for insertion we have to modify two links and for deletion we have to modify one link.

Creating a Node:

            To create a new node we have to use function called “getnode()”. This function creates a new node and ‘p’ is pointer points to that node.   Whenever we want to insert a node first we have to create it.   malloc() is a function which will allocate memory of size “struct node”.  free() is a function will de-allocate memory of node.

To declare a node  in ‘C’,  following is the syntax

struct node
{
   int data;
   struct node *link;
};

struct node * getnode()
{
  struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter data into the node: ");
  scanf("%d", &p->data);
  p->link=NULL;
  return p;
}

To access each field in the node, we denote p->data to data field and p->link to link field


Friday, January 25, 2013

Procedure for Transpose of Sparse Matrix

Transpose of sparse matrix

Consider a matrix
        
         4    0   2   0
A=    0    6   0   0    
         0    0   5   0
         7    0   0   8
         4    0   0   7
AT=  0    6   0   0    
         2    0   5   0
         0    0   0   8




R
C
value


R
C
value
 0
3
4
6

0
3
4
6
1
1
1
4

1
1
1
4
2
1
2
2

2
1
4
7
3
2
2
6

3
2
2
6
4
3
3
5

4
3
1
2
5
4
1
7

5
3
3
5
6
4
4
8

6
4
4
8











From the above two 3 – tuples we observe that in A-3 tuples are in ascending order and columns are in random way whereas in AT – 3 tuples columns are in ascending order and rows become irregular and actual changes depending on the row and column position.

Procedure Transpose(A: array[0..30, 1..3] of integers)
  B: array[0..30, 1..3] of integers
  I, j, k: integers
Begin
    B[0,1]= A[0,2];  { Copy Ist  row from A into B  such that size MXN becomes  
                               NXM }
    B[0,2]= A[0,1];
    B[0,3]= A[0,3]
    j=1;                         { J points to B 3-tuples   }
    for k= 1 to A[0,2]    {  k represents passes }
    begin
        for i=1 to A[0,3]  { i is a pointer for A 3-tuples }
          begin
              if A[I,2]= k then
                begin
                   B[j,1]= A[i,2];
                   B[j,2]= A[I,1];
                   B[j,3]= A[I,3];
                   J=j+1;
                end
          end
    end
end


Program for Transpose matrix

Program TransposeMatrix()
    A:array[1..30,1..30] of integers ;
    B,C:array[0..30,1..3] of integers ;
    i, j, m,n: integers;
begin
   writeln(“enter size of matrix’);
   read(m,n);
   writeln(“enter elements into matrix”);
   for I =1 to m do
   for j = 1 to n do
    read(A[I,j]);
 
   createsparematrix(A,m,n,B);
   Transpose(B,C);
   writeln(“resultant of Transpose is as follows”);
   for i=1 to C[0,3] do
    begin
      for j= 1 to 3 do
        begin
         write(C[i,j]);
         end
        writeln;
    end

end