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;
}
}
No comments:
Post a Comment