#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* createNode(int data) {
struct node* n = (struct node*) malloc(sizeof (struct node));
n->data = data;
n->next = NULL;
return n;
};
void insert(struct node** root, struct node* n) {
struct node* temp = *root;
if (temp == NULL) {
*root = n;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n;
}
}
void display(struct node* root) {
struct node* temp = root;
printf("START->");
while (temp != NULL) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("END");
}
void search(struct node* root, int search) {
struct node* temp = root;
while (temp != NULL) {
if (temp->data == search) {
printf("Item found!");
return;
}
temp = temp->next;
}
printf("Item not found!");
}
void insertAtBeginning(struct node** root) {
int data;
printf("Enter new node data: ");
scanf("%d", &data);
struct node* n = createNode(data);
n->next = *root;
*root = n;
}
int main(int argc, char** argv) {
struct node* root = NULL;
int data, locs = 0;
char ch;
printf("Enter data for the node & -111 to exit: ");
scanf("%d", &data);
while (data != -111) {
insert(&root, createNode(data));
printf("Enter data for the node & -111 to exit: ");
scanf("%d", &data);
locs++;
}
display(root);
printf("\nDo you want to perform search? (Y/N) ");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y') {
printf("Enter search data: ");
scanf("%d", &data);
search(root, data);
}
printf("\nWanna add more elements at beginning? (Y/N) ");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y') {
insertAtBeginning(&root);
display(root);
}
return (EXIT_SUCCESS);
}
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* createNode(int data) {
struct node* n = (struct node*) malloc(sizeof (struct node));
n->data = data;
n->next = NULL;
return n;
};
void insert(struct node** root, struct node* n) {
struct node* temp = *root;
if (temp == NULL) {
*root = n;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n;
}
}
void display(struct node* root) {
struct node* temp = root;
printf("START->");
while (temp != NULL) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("END");
}
void search(struct node* root, int search) {
struct node* temp = root;
while (temp != NULL) {
if (temp->data == search) {
printf("Item found!");
return;
}
temp = temp->next;
}
printf("Item not found!");
}
void insertAtBeginning(struct node** root) {
int data;
printf("Enter new node data: ");
scanf("%d", &data);
struct node* n = createNode(data);
n->next = *root;
*root = n;
}
int main(int argc, char** argv) {
struct node* root = NULL;
int data, locs = 0;
char ch;
printf("Enter data for the node & -111 to exit: ");
scanf("%d", &data);
while (data != -111) {
insert(&root, createNode(data));
printf("Enter data for the node & -111 to exit: ");
scanf("%d", &data);
locs++;
}
display(root);
printf("\nDo you want to perform search? (Y/N) ");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y') {
printf("Enter search data: ");
scanf("%d", &data);
search(root, data);
}
printf("\nWanna add more elements at beginning? (Y/N) ");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y') {
insertAtBeginning(&root);
display(root);
}
return (EXIT_SUCCESS);
}
Comments