c4-1.c
/* code: c4-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * node) {
while (NULL != node) {
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
head = malloc (sizeof (NODE_TYPE));
head->data = 300;
head->next = malloc (sizeof (NODE_TYPE));
head->next->data = 400;
head->next->next = malloc (sizeof (NODE_TYPE));
head->next->next->data = 500;
head->next->next->next = malloc (sizeof (NODE_TYPE));
head->next->next->next->data = 600;
head->next->next->next->next = NULL;
linked_list_print (head);
return 0;
}
c4-2.c
/* code: c4-2.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void linked_list_insert_head (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
if (*head == NULL) {
new_node->next = NULL;
*head = new_node;
}
else {
new_node->next = *head;
*head = new_node;
}
}
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1;
head = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("inserting (head): ");
printf ("%02d\n", data1);
linked_list_insert_head (&head, data1);
}
linked_list_print (head);
return 0;
}
c4-3.c
/* code: c4-3.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
int linked_list_delete_head (NODE_TYPE ** head) {
int data;
NODE_TYPE *temp;
if (*head == NULL) {
return NOT_FOUND;
}
data = (*head)->data;
temp = (*head);
*head = (*head)->next;
free (temp);
return data;
}
/* ------------------------------------------- */
void linked_list_insert_head (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
if (*head == NULL) {
new_node->next = NULL;
*head = new_node;
}
else {
new_node->next = *head;
*head = new_node;
}
}
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1;
head = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("inserting (head): ");
printf ("%02d\n", data1);
linked_list_insert_head (&head, data1);
}
linked_list_print (head);
for (i = 0; i < DATA_SIZE / 2; i++) {
printf ("deleting (head): ");
data1 = linked_list_delete_head (&head);
printf ("%02d\n", data1);
}
linked_list_print (head);
return 0;
}
q4-1.c
/* code: q4-1.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
int linked_list_search_node (NODE_TYPE * head, int key) {
int i;
i = 0;
while (NULL != head) {
if (key == head->data) {
return i;
}
head = head->next;
i++;
}
return NOT_FOUND;
}
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
void linked_list_insert_head (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
if (*head == NULL) {
new_node->next = NULL;
*head = new_node;
}
else {
new_node->next = *head;
*head = new_node;
}
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1, data2, stat;
head = NULL;
data2 = 0;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding (head node): ");
printf ("%02d\n", data1);
linked_list_insert_head (&head, data1);
if (i == (DATA_SIZE / 2)) {
data2 = data1;
}
}
linked_list_print (head);
stat = linked_list_search_node (head, data2);
if (stat == NOT_FOUND) {
printf ("not found : %d", data2);
}
else {
printf ("found : %d (%d)", data2, stat);
}
return 0;
}
q4-2.c
/* code: q4-2.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
int linked_list_length (NODE_TYPE * head) {
int c;
c = 0;
while (NULL != head) {
head = head->next;
c++;
}
return c;
}
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
void linked_list_insert_head (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
if (*head == NULL) {
new_node->next = NULL;
*head = new_node;
}
else {
new_node->next = *head;
*head = new_node;
}
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1;
head = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding (head node): ");
printf ("%02d\n", data1);
linked_list_insert_head (&head, data1);
}
linked_list_print (head);
printf ("number of node(s): %d\n", linked_list_length (head));
return 0;
}
q4-3.c
/* code: q4-3.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n\n");
}
/* ------------------------------------------- */
void linked_list_insert_tail (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
NODE_TYPE *temp;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
temp = new_node;
}
else {
temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
}
/* ------------------------------------------- */
int linked_list_delete_tail (NODE_TYPE ** head) {
int data;
NODE_TYPE *temp;
NODE_TYPE *prev;
data = NOT_FOUND;
if (*head == NULL) {
return data;
}
else {
temp = *head;
prev = *head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
data = temp->data;
if ((*head)->next == NULL) {
*head = NULL;
}
else {
prev->next = NULL;
}
free (temp);
}
return data;
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1;
head = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding (tail node): ");
printf ("%02d\n", data1);
linked_list_insert_tail (&head, data1);
}
linked_list_print (head);
for (i = 0; i < DATA_SIZE; i++) {
printf ("deleting (tail node): ");
data1 = linked_list_delete_tail (&head);
printf ("%02d\n", data1);
}
linked_list_print (head);
return 0;
}
q4-4.c
/* code: q4-4.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define DATA_SIZE 10
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
struct linked_list {
NODE_TYPE *head;
NODE_TYPE *tail;
NODE_TYPE *current;
};
typedef struct linked_list LINKED_LIST;
/* ------------------------------------------- */
void linked_list_init (LINKED_LIST * list) {
list->head = NULL;
list->tail = NULL;
list->current = NULL;
}
/* ------------------------------------------- */
void linked_list_insert_node_h (LINKED_LIST * list, int data) {
NODE_TYPE *node;
node = malloc (sizeof (NODE_TYPE));
node->data = data;
if (NULL == list->head) {
list->tail = node;
node->next = NULL;
}
else {
node->next = list->head;
}
list->head = node;
}
/* ------------------------------------------- */
void linked_list_insert_node_t (LINKED_LIST * list, int data) {
NODE_TYPE *node;
node = malloc (sizeof (NODE_TYPE));
node->data = data;
node->next = NULL;
if (NULL == list->head) {
list->head = node;
}
else {
list->tail->next = node;
}
list->tail = node;
}
/* ------------------------------------------- */
NODE_TYPE *linked_list_find_node (LINKED_LIST * list, int data) {
NODE_TYPE *node;
node = list->head;
while (NULL != node) {
if (node->data == data) {
return node;
}
node = node->next;
}
return NULL;
}
/* ------------------------------------------- */
void linked_list_delete_node (LINKED_LIST * list, NODE_TYPE * node) {
if (node == list->head) {
if (NULL == list->head->next) {
list->head = NULL;
list->tail = NULL;
}
else {
list->head = list->head->next;
}
}
else {
NODE_TYPE *temp;
temp = list->head;
while ((NULL != temp) && (temp->next != node)) {
temp = temp->next;
}
if (NULL != temp) {
temp->next = node->next;
}
}
free (node);
}
/* ------------------------------------------- */
void linked_list_print (LINKED_LIST * list) {
NODE_TYPE *node;
printf ("linked list [ ");
node = list->head;
while (NULL != node) {
printf ("%02d ", node->data);
node = node->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
int linked_list_count_node (LINKED_LIST * list) {
NODE_TYPE *node;
int i;
i = 0;
node = list->head;
while (NULL != node) {
i++;
node = node->next;
}
return i;
}
/* ------------------------------------------- */
int main () {
LINKED_LIST *list;
NODE_TYPE *node;
int i, data1, data2, del_data;
list = malloc (sizeof (LINKED_LIST));
linked_list_init (list);
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding node to head of linked list: ");
printf ("%02d\n", data1);
linked_list_insert_node_h (list, data1);
}
linked_list_print (list);
for (i = 0; i < DATA_SIZE; i++) {
data2 = (int) rand () % 100;
printf ("adding node to tail of linked list: ");
printf ("%02d\n", data2);
linked_list_insert_node_t (list, data2);
}
linked_list_print (list);
del_data = data2;
printf ("finding node: [%d]\n", del_data);
node = linked_list_find_node (list, del_data);
if (NULL != node) {
printf ("deleting node: [%d]\n", del_data);
linked_list_delete_node (list, node);
}
else {
printf ("Node not found: [%d]\n", del_data);
}
linked_list_print (list);
printf ("number of node(s):%d\n", linked_list_count_node (list));
free (list);
return 0;
}
q4-5.c
/* code: q4-5.c (v1.18.00) */
#include<stdio.h>
#include<stdlib.h>
#define NOT_FOUND (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
int linked_list_search_node (NODE_TYPE * head, int key) {
int i;
i = 0;
while (NULL != head) {
if (key == head->data) {
return i;
}
head = head->next;
i++;
}
return NOT_FOUND;
}
/* ------------------------------------------- */
int linked_list_length (NODE_TYPE * head) {
int c;
c = 0;
while (NULL != head) {
head = head->next;
c++;
}
return c;
}
/* ------------------------------------------- */
void linked_list_print (NODE_TYPE * head) {
printf ("Linked_list [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
void linked_list_insert_head (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
if (*head == NULL) {
new_node->next = NULL;
*head = new_node;
}
else {
new_node->next = *head;
*head = new_node;
}
}
/* ------------------------------------------- */
int linked_list_delete_head (NODE_TYPE ** head) {
int data;
NODE_TYPE *temp;
if (*head == NULL) {
return NOT_FOUND;
}
data = (*head)->data;
temp = (*head);
*head = (*head)->next;
free (temp);
return data;
}
/* ------------------------------------------- */
void linked_list_insert_tail (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
NODE_TYPE *temp;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
temp = new_node;
}
else {
temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
}
/* ------------------------------------------- */
int linked_list_delete_tail (NODE_TYPE ** head) {
int data;
NODE_TYPE *temp;
NODE_TYPE *prev;
data = NOT_FOUND;
if (*head == NULL) {
return data;
}
else {
temp = *head;
prev = *head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
data = temp->data;
if ((*head)->next == NULL) {
*head = NULL;
}
else {
prev->next = NULL;
}
free (temp);
}
return data;
}
/* ------------------------------------------- */
void linked_list_delete_all (NODE_TYPE ** head) {
NODE_TYPE *current;
NODE_TYPE *next;
current = *head;
while (current != NULL) {
next = current->next;
free (current);
current = next;
}
*head = NULL;
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
int i, data1;
head = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding (head node): ");
printf ("%02d\n", data1);
linked_list_insert_head (&head, data1);
}
linked_list_print (head);
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("adding (tail node): ");
printf ("%02d\n", data1);
linked_list_insert_tail (&head, data1);
}
linked_list_print (head);
for (i = 0; i < DATA_SIZE / 2; i++) {
printf ("deleting (head node): ");
data1 = linked_list_delete_head (&head);
printf ("%02d\n", data1);
}
linked_list_print (head);
for (i = 0; i < DATA_SIZE / 2; i++) {
printf ("deleting (tail node): ");
data1 = linked_list_delete_tail (&head);
printf ("%02d\n", data1);
}
linked_list_print (head);
printf ("Length of the linked list :%d\n", linked_list_length (head));
printf ("deleting entire linked list\n");
linked_list_delete_all (&head);
linked_list_print (head);
return 0;
}
EOF