Program to create a linked list. If the inserted value is an even number, the number is inserted at the beginning. If the inserted number is odd, it i

Program to create a linked list. If the inserted value is an even number, the number is inserted at the beginning. If the inserted number is odd, it is inserted at the end.


#include
#include

typedef struct listnode
{
struct listnode * next;
int value;
}node;

Node * Node_construct(int v)
{
Node * n = malloc(sizeof(node));
n -> value = v;
n -> next = 0;
return n;
}

void Node_destruct(Node * n)
{
free(n);
}

void List_instert(Node ** h, int v)
{
Node * n = Node_construct(v);
n -> next = h;
return n;
}
\


void List_delete(Node ** h, int v)
{
/*delete the first occurrence of the value v*/

if ((*h) == 0)
{return;}
Node *p = *h;

if((p -> value) == v)
{
/* if it is v, move the head to the next Node and delete the original head*/

return;
}
/*Otherwise, find the first Node whose value is v */

/* If no Node whose value is v, do nothing. If a Node is found, delete it from the list */

/* If a Node is found, destroy the Node and release memory */
}

void List_destruct(Node ** h)
{
Node *p = *h;
node *q;
while (p != 0)
{
q = p -> next;
Node_destruct(p);
p=q;
}
}

void Node_print(Node *n)
{ printf("%d", n -> value);}
void List_print(Node *n)
{
Node *p = n;
while (p != 0)
{
Node_print(p);
p = p -> next;
}
printf("\n\n");
}
int main(int argc, char * argv[])
{
int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 7, 4, 2, 0};
int n = sizeof(x) / sizeof(int);
int i;
Node * head = 0;
for (i = 0; i < n =" Node_construct(v);" h ="="" h=" n;}" 2 ="="" head =" n;}"> next != NULL)
{n = n -> next;}
/* make the new Node the last Node in the list */
n -> next = n;
}
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!