Linked list using C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct LLNode
{
	int data;
	struct LLNode* nextNode;
}Node;//Alias name

void AddNewNodeAtEnd(Node* head,int data)
{
	Node *newNode = NULL;

	newNode = malloc(sizeof(Node));

	if(newNode == NULL)
	{
		printf("Error while creating node!!");
	}
	else
	{
		//printf("Created node!!");

		newNode->data = data;
		newNode->nextNode = NULL;

		Node * t_head = head;

		//add this node to the end of the list
		while(t_head->nextNode!= NULL)
		{
			t_head = t_head->nextNode;
		}

		//add the node
		t_head->nextNode = newNode;
	}
}

//This function replaces the existing Node * head reference
Node* AddNewNodeAtBegin(Node* head, int data)
{
	Node* newNode = malloc(sizeof(Node));
	newNode->data = data;
	newNode->nextNode = head;

	return newNode;
}

int PrintLinkedList(Node *head)
{
	printf("\nPrinting the LinkedList : \n");

	Node* t_head = head;

	if(head == NULL)
	{
		printf("List is empty!");
		return 0;
	}

	while(t_head != NULL)
	{
		printf("%d \n",t_head->data);
		t_head = t_head->nextNode;
	}

	printf("\n ----------------------------------- \n");
	return 0;
}

//This one searches and deletes a node
void DeleteBeginNode(Node** head)
{
	if((*head)->nextNode == NULL)
		(*head)= NULL;
	else
	{
		(*head) = (*head)->nextNode;
	}
}

void DeleteLastNode(Node** head)
{
	//when list size is 1
	if((*head)->nextNode == NULL)
		(*head)= NULL;
	else
	{
		Node * temp = *head;

		//when list size is 2 or more
		while(temp->nextNode->nextNode != NULL)
		{
			temp = temp->nextNode;
		}

		temp->nextNode = NULL;

	}
}

int main(void) {

	//eclipse specifics
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	//Create Head Node
	Node * head = NULL;
	head = malloc(sizeof(Node));

	printf("\nEnter data for HeadNode : ");
	scanf("%d",&head->data);

	head->nextNode = NULL;




	int count = 0;
	int data = 0;

	printf("Enter the number of nodes to add at end : ");
	scanf("%d",&count);

	for(int i=0; i<count; i++)
	{
		printf("Enter the data : ");
		scanf("%d",&data);

		AddNewNodeAtEnd(head, data);
	}

	//print the entire list
	PrintLinkedList(head);

	//Add new node at front
	printf("\n Adding a new node at beginning ");
	scanf("%d",&data);
	head = AddNewNodeAtBegin(head, data);

	//print the entire list
	PrintLinkedList(head);

	//Delete head node
	printf("\nPress any key to delete the head node");
	scanf("%d",&data);
	DeleteBeginNode(&head);

	//print the entire list
	PrintLinkedList(head);

	//Delete tail node
	printf("\nPress any key to delete the tail node");
	scanf("%d",&data);
	DeleteLastNode(&head);

	//print the entire list
	PrintLinkedList(head);

	//Delete tail node
	printf("\nPress any key to delete the tail node");
	scanf("%d",&data);
	DeleteLastNode(&head);

	//print the entire list
	PrintLinkedList(head);


	printf("\n\n =========== END ============ ");

	return EXIT_SUCCESS;
}

Self referencing struct in C

Along with typedef, There are times you want to create reference to the struct that you are actually creating.
Just refer the example code below ,

#include 'stdio.h'

typedef struct LLNode
{
   int data;
   struct LLNode* nextNode; //'struct' keyword must
}Node;

int main()
{
   Node* n = NULL; //using alias name
   n = malloc(sizeof(Node));
   data = 10;
   nextNode = NULL;
   printf("Node data is : %d",data);
   return 0;
}

Multi-threading in C#

A simple mult-threading in c# .

 

 

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

namespace ConsoleApp1
{
    class MyThread
    {
        public static void Main(String[] args)
        {
            Console.WriteLine("Threading test : ");


            Task1 t_one = new Task1();
            t_one.num = 5;

            //Thread one
            Thread thread1 = new Thread(t_one.Print);
            thread1.Name = "Thread 1";
            thread1.Start();

            //Thread two
            Thread thread2 = new Thread(t_one.Print);
            thread2.Name = "Thread 2";
            thread2.Start();

            thread1.Join();
            thread2.Join();

            Console.WriteLine("All done");

            Console.ReadKey();
        }
    }

    class Task1
    {
        public int num;

        public void Print()
        {
           
            for(int i=0;i< num;i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " : " + i);
                Thread.Sleep(1000);
            }
            Console.WriteLine(Thread.CurrentThread.Name +" done");
        }
    }
}

Retrieve similar nodes in XML using C# Linq

This is the C# way to retrieve values from similar nodes inside a parent node.

Given input XML data

xml

    C# Code:
namespace XMLTest
{
    class Program
    {

        static void Main(string[] args)
        {
            string text = System.IO.File.ReadAllText(@"E:\testxml.txt");
            Console.WriteLine(text);

            Console.WriteLine("\n\n OUTPUT :\n");
            ReadXML(text);
        }

        public static void ReadXML(string xmlContent)
        {
            //Read the XML file
            XDocument xmlDoc = XDocument.Parse(xmlContent);
            IEnumerable xmlData_PackageNodes = xmlDoc.Descendants("linux").Descendants("package");

            for (int i = 0; i < xmlData_PackageNodes.Count(); i++)
            {
                //Get a single 'Package' node 
                XElement singleItem = xmlData_PackageNodes.ElementAt(i);

                //Get all the child 'driver' nodes with this name as IEnumerable
                IEnumerable driverNodes = singleItem.Descendants("driver");

                Console.WriteLine("=========");
                Console.WriteLine("For i = " + i);
                //get the 'driver' nodes as array
                foreach (XElement t in driverNodes)
                {
                    Console.WriteLine(t.Value.Trim());
                }

                Console.WriteLine("=========");
            }
        }
    }
}

OUTPUT:

OP

Wave effects in Unity using ShaderLabs

Shader "praveenmax/TestWaveShader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_MyColor ("MyColor", Color) = (0,0,1,1)
		_StartVertexShader("StartVertexShader", int) = 0
		_StartFragmentShader("StartFragmentShader", int) = 0
	    _WaveStrengthX("WaveStrength X", float) = 2
		_WaveStrengthY("WaveStrength Y", float) = 2
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _MyColor;
			int _StartVertexShader, _StartFragmentShader;
			int _WaveStrengthX, _WaveStrengthY;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);

				//get the world coords 
				float worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				//o.vertex.y += sin(worldPos.y + _Time.w);

				if (_StartVertexShader == 1)
				{
					o.vertex.y += sin(worldPos.x * _WaveStrengthX + _Time.w) / 2;
					o.vertex.x += sin(worldPos.x * _WaveStrengthY + _Time.z) / 4;
				}

				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				
				if (_StartFragmentShader == 1)
				{
					col.x += sin(col.x + _Time.w) /2;
					col.y += sin(col.x + _Time.w) /2;
					col.z += sin(col.x + _Time.w) /2;

				}

				return col;
			}
			ENDCG
		}
	}

	Fallback "VertexLit"
}

Simple diffuse shader using ShaderLab in Unity

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/SimpleShader" {
	Properties {
		_AmbientLightColor("Ambient Light", Color) = (1,1,1,1)
		_AmbientLightIntensity("Ambient Light Intensity", Range(0.0,1.0)) = 1.0

		_DiffuseDirection("Diffuse Light Direction", Vector) = (0.2,0.8,0.7,1)
		_DiffuseColor("Diffuse Light Color", Color)=(1,1,1,1)
		_DiffuseIntensity("Diffuse Light Intensity", Range(0.0,1.0))=1.0
	}

	SubShader
	{
		Pass
		{
			CGPROGRAM

			#pragma vertex vertexShader
			#pragma fragment fragmentShader

			fixed4 _AmbientLightColor;
			float _AmbientLightIntensity;
			float3 _DiffuseDirection;
			float4 _DiffuseColor;
			float _DiffuseIntensity;

			struct VertexShaderInput{
				float4 position : POSITION;
				float3 normal : NORMAL;
			};

			struct VertexShaderOutput{
				float4 position : SV_POSITION;
				float3 normal : NORMAL;
			};

			//Input : VertexShaderInput
			//Output: VertexShaderOutput
			VertexShaderOutput vertexShader(VertexShaderInput vIn)
			{
				//The vertex position is multiplied
				//with the MVP matrices of Unity.
;				VertexShaderOutput vOut;
				vOut.position = UnityObjectToClipPos(vIn.position);
				vOut.normal = vIn.normal;

				return vOut;
			}

			//OUTPUT : A render target
			float4 fragmentShader(VertexShaderOutput vOut) : SV_Target
			{
				float diffuseFactor = saturate(dot(_DiffuseDirection, vOut.normal));
				return (_AmbientLightColor * _AmbientLightIntensity) +
						(diffuseFactor * _DiffuseColor *  _DiffuseIntensity);
			}

			ENDCG
		}
	}
}

Simple ShaderLab code for Flat color material


// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'</code>

Shader "Custom/SimpleShader" {
Properties {
  _AmbientLightColor("Ambient Light", Color) = (1,1,1,1)
  _AmbientLightIntensity("Ambient Light Intensity", Range(0.0,1.0)) = 1.0
}
SubShader
{
  Pass
  {
   CGPROGRAM

   #pragma vertex vertexShader
   #pragma fragment fragmentShader

   fixed4 _AmbientLightColor;
   float _AmbientLightIntensity;

   //Input : POSITION
   //Output: SV_POSITION
   float4 vertexShader(float4 v:POSITION) : SV_POSITION
   {
     //The vertex position is multiplied
     //with the MVP matrices of Unity.

     return UnityObjectToClipPos(v);
   }

   //OUTPUT : A render target
   fixed4 fragmentShader() : SV_Target
   {
     return _AmbientLightColor * _AmbientLightIntensity;
   }

   ENDCG
  }
 }
}

Function Pointers inside Struct in C

#include <stdio.h>

typedef struct{
	int x;
	int y;
	int (*addXPoints) (int x,int y);
	int (*subXPoints) (int x,int y);
}Point;

int addPoints_x(int p1, int p2)
{
		return (p1+p2);
}	

int subtracteXPoints(int p1,int p2)
{
		return (p1-p2);
}

void printPointDetails(Point* temp)
{
		printf("Point [x,y] : %d , %d \n", temp->x, temp->y);
}

int main()
{
	Point p1={30,30};
	
	//another way to initialize
	//p1.x=10; 
	//p1.y=20;
	p1.addXPoints=addPoints_x;
	p1.subXPoints=subtracteXPoints;
	
	printPointDetails(&p1);
	
	printf("Total X Value of p1,p2 : %d\n",p1.addXPoints(p1.x,p1.x));
	printf("Total X Value of p1,p2 : %d\n",p1.subXPoints(p1.x,p1.x));
	

	return 0;
}

Constant pointers and Pointers to constant in C

#include <stdio.h>
int main()
{
	int val = 250;
	int val2 =300;

        // ptrInt is a pointer to integer constant
        const int* ptrInt = &val; //Pointer to Constant

        // ptrInt1 is a constant pointer to integer
        int* const ptrInt1 = &val; //Constant Pointer

        *ptrInt = 500; //pointer to constant wont allow

	ptrInt1 = &val2; //constant pointer wont allow

	return 0;
}

Dual Assault

It is an endless top-down shooter game.The main objective of the game is to prevent the enemies from capturing the territory. Made using Unity3d.