DreamsCoder.com

We Code your Dreams

Singly Link List program in C along with code - [Explained in Video]



 Singly Link List program in C along with code - [Explained in Video]




Source code





You can Download code here - 
http://dreamscoder.com/examples/singlyll.c
OR 
Click above on Source code

    #include
    #include

    struct node {
        int data;
        struct node *next;
    } *head, *temp, *curr, *left;

/* To add new node in Link list */

    void add(int num) {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;

            if(head == NULL) {
                head = temp;
                head->next = NULL;
            }
            else
            {
                temp->next = head;                
                head = temp;
            }

    }//add

/* To add new node in Link list after the
 specified location */

    void add_after(int num, int loc)
    {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;
        curr = head;
            if(head == NULL) {
                    head = temp;
                    head->next = NULL;
            }
           else {
                for(i=0; inext;
                }

                left->next = temp;
                temp->next = curr;
            }//else
    }

/* To append new node in Link list */

    void append(int data) {

        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = data;
        curr = head;

        if(curr == NULL) {
            head = temp; 
            head->next = NULL;
        }
        
        while(curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = temp;
        curr = temp;
        curr->next = NULL;
    }

/* To delete node in Link list which contains the 
specified number */

    void delete(int num) {
        curr = head;

        if(curr == NULL) {
            printf("Link List is empty");
        }
        else
        {
            while(curr != NULL) {
               
                if(curr->data == num) {

                    if(curr == head) {
                    head = curr->next;
                    free(curr);
                    break;
                }
                    left->next = curr->next;
                    free(curr);
                    break;
                }
                else {
                    left = curr;
                    curr = curr->next;
                }
            } //while loop
        }
    }

/* To delete the entire Link list */

    void deleteAll() {
        curr = head;
        if(curr == NULL) {
            printf("Link list is empty");
        }
        else {
            while(curr != NULL) {
                left = curr->next;
                free(curr);
                curr = left;
            }
            head = NULL;
            printf("Deleted Link list");
        }
    }

/* To display all nodes in Link list */

    void disp() {
        if(head == NULL) {
            printf("Link list is empty");
            return;
        }
        else { 
            curr = head;
                 printf("Data is");
                while(curr != NULL) {
                    printf("%d	 ",curr->data);
                    curr = curr->next;
                } //while
            }//else
        }


    int main() {

        int choice;
        int num,loc;

        while(1)
        {
            printf("1. Add");
            printf("2. Add after");
            printf("3. Append");
            printf("4. Delete");
            printf("5. Delete All");
            printf("6. Display");
            printf("7. Exit");
            printf("---------------------");
            printf("Enter choice");
            scanf("%d",&choice);
             printf("");
            

        switch(choice) {
            case 1: printf("Enter data");
                    scanf("%d",&num);
                    add(num);
                    break;
            case 2: printf("Enter data and location ");
                    scanf("%d%d",&num,&loc);
                    add_after(num,loc);
                    break;
            case 3: printf("Enter the data");
                    scanf("%d",&num);
                    append(num);
                    break;
            case 4: printf("Enter the number to delete ");
                    scanf("%d",&num);
                    delete(num);
                    break;
            case 5: deleteAll();
                    break;
            case 6: disp();
                    break;
            case 7: return 0;
            
            default: printf("Incorrect choice");
                    break;

        }
    }//while

        return 0;
    }

Label - data-structure


by -




Drawing Prism using Three.js



Drawing Prism using Three.js


Click here for Live demo

Files

Three.js | Source code
var scene = new THREE.Scene();
 
var camera = new THREE.PerspectiveCamera
(75, window.innerWidth/window.innerHeight, 0.2, 1000);

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,
window.innerHeight);
            document.body.appendChild(renderer.domElement);
 
            camera.position.z = 3.9;
 
            //1st top planar polygon
            //2nd is bottom planar polygon
            //3rd is  Height
            //4th is dimension 4 for 4d

var pyramidgeometry = new THREE.CylinderGeometry(0.1, bp, h, d, false);

var pyramidmaterial = new THREE.MeshBasicMaterial
({wireframe: true, color: 0x0fff00});
var pyramid = new THREE.Mesh(pyramidgeometry, pyramidmaterial);
    
pyramid.position.set(0.3,0.4,1.6);
scene.add(pyramid);
 
 
            var render = function () {
                requestAnimationFrame(render);
                
                pyramid.rotation.y += 0.01;
                pyramid.rotation.z += 0.01;
                pyramid.rotation.x += 0.01;
                
 
                renderer.render(scene, camera);
            };
 
 
            render();

Label - javascript


by -




Spinning letters in using C - graphics



Spinning letters in using C - graphics


Watch demo




#include
#include

void main()
{
int j;
char str[40]="SPINNING LETTERS IN C - 
DREAMSCODER.COM";
clrscr();
textcolor(14);
gotoxy(25,6); //pointing to x,y

	for(j=0;j<39;j++)
	{
		gotoxy(25+j,6);
		cprintf("|"); delay(30);
		gotoxy(25+j,6);
		cprintf("/"); delay(30);
		gotoxy(25+j,6);
		cprintf("-"); delay(30);
		gotoxy(25+j,6);
		cprintf("\\"); delay(30);
		gotoxy(25+j,6);
		cprintf("|"); delay(30);
		gotoxy(25+j,6);
		cprintf("%c",str[j]);
		delay(50);
	}//for
getch();
}//void main

Label - c


by -




Difference between ArrayList and LinkedList in Java with example



Difference between ArrayList and LinkedList in Java with example


  • For frequent addition and deletion LinkedList is a best choice.
  • If there are more search operations then ArrayList would be a best solution.
  • ArrayList has direct references to every element in the list, so it can get the n-th element in constant time.
  • LinkedList has to traverse the list from the beginning to get to the n-th element.
  • Array List LinkedList
    get() O(1) O(n)
    add() O(1) O(1) amortized
    remove() O(n) O(n)

    /* LINKEDLIST EXAMPLE */
    	LinkedList ll = new LinkedList();
      
          ll.add("Dreams");
          ll.add("Coder");
          ll.add(".com");
          ll.addLast("Programming");
          ll.addFirst("www.");
          System.out.println("Original contents of ll:"+ll);
    
          // remove elements from the linked list
          ll.remove(".com");
          ll.remove(1);
          System.out.println("Contents of Linked List 
    ll after deletion: "+ ll);
          
          // remove first and last elements
          ll.removeFirst();
          ll.removeLast();
          System.out.println("Contents of Linked List 
    ll after deleting first and last: "+ ll);
    
    /* ARRAYLIST EXAMPLE */
         ArrayList arrlist = new
     ArrayList(5);
    
       arrylist.add(1);
       arrylist.add(2);
       arrylist.add(3);
       arrylist.add(4);
    
       for (Integer num : arrylist) {
       System.out.println("Number = " + number);
       } 
    	
       // retrieves element at 3rd postion
       int third =arrylist.get(2);
       System.out.println("Retrieved element is = " +third);
    }

    Label - java


    by -




    XML to JSON conversion using PHP



    XML to JSON conversion using PHP


    Click here for live demo



    Source code


    $xmlNode = simplexml_load_file($url);
    $arrayData = xmlToArray($xmlNode);
    function xmlToArray($xml, $options = array()) 
    {
       $defaults = array('namespaceSeparator' =>':',
    //Seperator
     'attributePrefix' => '@',   
    //to distinguish between attributes and nodes 
    with the same name
     'alwaysArray' => array(),
     'autoArray' => true,        
     'textContent' => '$',       
     'autoText' => true,
     'keySearch' => false,       
     'keyReplace' => false       
        );
        $options = array_merge($defaults, $options);
        $namespaces = $xml->getDocNamespaces();
        $namespaces[''] = null; 
     
        //get attributes from all namespaces
        $attributesArray = array();
        foreach ($namespaces as $prefix => $namespace) 
          {
            foreach ($xml->attributes($namespace) as
     $attributeName => $attribute) {
                //replace characters in attribute name
                if ($options['keySearch']) $attributeName =
                str_replace($options['keySearch'],
     $options['keyReplace'], $attributeName);
                $attributeKey = $options['attributePrefix']. 
    ($prefix ? $prefix . 
    $options['namespaceSeparator'] : '').$attributeName;
    echo "
    "; $attributesArray[$attributeKey] = (string)$attribute; } } $tagsArray = array(); foreach ($namespaces as $prefix => $namespace) { foreach ($xml->children($namespace) as $childXml) { $childArray = xmlToArray($childXml, $options); list($childTagName, $childProperties) = each($childArray); if ($options['keySearch']) $childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName); if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName; if (!isset($tagsArray[$childTagName])) { $tagsArray[$childTagName] = in_array($childTagName, $options['alwaysArray']) || !$options['autoArray'] ? array($childProperties) : $childProperties; } elseif (is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName]) === range(0, count($tagsArray[$childTagName]) - 1)) { $tagsArray[$childTagName][] = $childProperties; } else { $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties); } } } //get text content of node $textContentArray = array(); $plainText = trim((string)$xml); if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText; //stick it all together $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText; //return node as array return array( $xml->getName() => $propertiesArray ); } $jdata = json_encode($arrayData);

    Label - php


    by -




    < previous page | next page>

    Latest Technology,Tricks and Tips