#include<stdio.h>
#include<stdlib.h>
typedef struct node Node;
struct node {
int data;
Node *next;
};
Node *create_node(int item, Node *next)
{
Node
*new_node
= (Node
*) malloc(sizeof(Node
));
if(new_node == NULL) {
printf("Error! Could Not Create A New Node\n");
}
new_node->data = item;
new_node->next = next;
return new_node;
}
Node *prepend(Node *head, int item)
{
Node *new_node = create_node(item, head);
return new_node;
}
Node *append(Node *head, int item)
{
Node *new_node = create_node(item, NULL);
if(head == NULL) {
return new_node;
}
Node *current_node = head;
while(current_node->next != NULL) {
current_node = current_node->next;
}
current_node->next = new_node;
return head;
}
void print_linked_list(Node *head)
{
Node *current_node = head;
while(current_node != NULL) {
printf("%d ", current_node
->data
);
current_node = current_node->next;
}
}
int count(Node *head)
{
int count = 0;
Node *current_node = head;
while(current_node != NULL) {
count++;
current_node = current_node->next;
}
return count;
}
Node *search(Node *head, int item)
{
Node *current_node = head;
while(current_node != NULL) {
if(current_node->data == item)
return current_node;
else
current_node = current_node->next;
}
return current_node;
}
Node *remove_node(Node *head, Node *node)
{
if (node == head) {
head = node->next;
return head;
}
Node *current_node = head;
while(current_node != NULL) {
if(current_node->next == node) {
break;
}
current_node = current_node->next;
}
if(current_node == NULL) {
return head;
}
current_node->next = node->next;
return head;
}
void insert(Node *node, int item)
{
Node *new_node = create_node(item, node->next);
node->next = new_node;
print_linked_list(node);
}
int main()
{
Node *n1, *n2, *head, *n3;
head = NULL;
n1 = create_node(10, NULL);
head = n1;
print_linked_list(head);
head = prepend(head, 20);
print_linked_list(head);
head = append(head, 30);
print_linked_list(head);
printf("Number of Node = %d\n", count
(head
));
printf("Searching Node is = %d\n",(search
(head
, 30))->data
);
head = remove_node(head, n1);
print_linked_list(head);
n2 = head;
head = remove_node(head, n2);
print_linked_list(head);
n2 = head;
head = remove_node(head, n2);
print_linked_list(head);
insert(head, 20);
return 0;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}