0% found this document useful (0 votes)
24 views4 pages

Linked Lists

Uploaded by

roweena.cd23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Linked Lists

Uploaded by

roweena.cd23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h> struct Node *new_node; new_node->next = start;


#include <stdlib.h> int n; start = new_node;
printf("ENTER VALUE TO BE INSERTED: "); return;
struct Node { scanf("%d", &n); }
int info; new_node = getNode(n); struct Node *temp = start;
struct Node *next; if (new_node == NULL) return; for (int i = 0; i < position - 1 && temp != NULL; i++) {
}; if (start == NULL) { temp = temp->next;
printf("NOT POSSIBLE, LIST IS EMPTY\n"); }
struct Node *start = NULL; return; if (temp == NULL) {
} else if (start->next == NULL) { printf("POSITION OUT OF RANGE\n");
struct Node* getNode(int n) { start->next = new_node; free(new_node);
struct Node *p = (struct Node *)malloc(sizeof(struct } else { } else {
Node)); new_node->next = start->next; new_node->next = temp->next;
if (p == NULL) { start->next = new_node; temp->next = new_node;
printf("NO MEMORY ALLOCATED\n"); } }
return NULL; } }
}
p->info = n; void insert_last_position() { void insert_into_sorted(int key) {
p->next = NULL; struct Node *new_node; struct Node *new_node = getNode(key);
return p; int n; if (new_node == NULL) return;
} printf("ENTER VALUE TO BE INSERTED: "); if (start == NULL || start->info > key) {
scanf("%d", &n); new_node->next = start;
void insert_beginning() { new_node = getNode(n); start = new_node;
struct Node *new_node; if (new_node == NULL) return; } else {
int n; if (start == NULL) { struct Node *p = start;
printf("ENTER VALUE TO BE INSERTED: "); start = new_node; while (p->next != NULL && p->next->info < key) {
scanf("%d", &n); } else { p = p->next;
new_node = getNode(n); struct Node *temp = start; }
if (new_node == NULL) return; while (temp->next != NULL) { new_node->next = p->next;
if (start == NULL) { temp = temp->next; p->next = new_node;
start = new_node; } }
} else { temp->next = new_node; }
new_node->next = start; }
start = new_node; } void traverse() {
} void insert_at_any_position(int position, int value) { struct Node *temp = start;
} struct Node *new_node = getNode(value); if (start == NULL) {
if (new_node == NULL) return; printf("LIST IS EMPTY\n");
void insert_second_position() { if (position == 0) { return;
} printf("LAST NODE IS DELETED\n"); return count;
printf("LIST ELEMENTS: "); } }
while (temp != NULL) {
printf("%d\t", temp->info); void delete_node_at_any_pos(int position) { void reverse_list() {
temp = temp->next; if (start == NULL) { struct Node *prev = NULL, *current = start, *next =
} printf("LIST IS EMPTY\n"); NULL;
printf("\n"); return; while (current != NULL) {
} } next = current->next;
struct Node *temp = start; current->next = prev;
void delete_first() { if (position == 0) { prev = current;
if (start == NULL) { start = temp->next; current = next;
printf("LIST IS EMPTY\n"); free(temp); }
return; printf("NODE AT POSITION %d DELETED\n", start = prev;
} position); printf("LINKED LIST REVERSED\n");
struct Node *temp = start; return; }
start = start->next; }
free(temp); for (int i = 0; temp != NULL && i < position - 1; i++) { void bubbleSort(struct Node* start) {
printf("FIRST NODE IS DELETED\n"); temp = temp->next; int swapped;
} } struct Node *ptr, *lptr = NULL;
if (temp == NULL || temp->next == NULL) {
void delete_last() { printf("POSITION %d DOES NOT EXIST\n", if (start == NULL || start->next == NULL) {
if (start == NULL) { position); return;
printf("LIST IS EMPTY\n"); return; }
return; }
} struct Node *next = temp->next->next; do {
if (start->next == NULL) { free(temp->next); swapped = 0;
free(start); temp->next = next; ptr = start;
start = NULL; printf("NODE AT POSITION %d DELETED\n",
printf("LAST NODE IS DELETED, LIST IS NOW position); while (ptr->next != lptr) {
EMPTY\n"); } if (ptr->info > ptr->next->info) {
return; int temp = ptr->info;
} int count_nodes() { ptr->info = ptr->next->info;
struct Node *temp = start; int count = 0; ptr->next->info = temp;
while (temp->next->next != NULL) { struct Node *temp = start; swapped = 1;
temp = temp->next; while (temp != NULL) { }
} count++; ptr = ptr->next;
free(temp->next); temp = temp->next; }
temp->next = NULL; } lptr = ptr;
} while (swapped); temp = temp->next; printf("ENTER VALUE TO BE INSERTED INTO
printf("LIST SORTED\n"); position++; SORTED LIST: ");
} } scanf("%d", &value);
printf("\n"); insert_into_sorted(value);
void print_odd_position_nodes() { } break;
struct Node *temp = start; void main() { case 6: traverse(); break;
int position = 1; int choice, position, value; case 7: delete_first(); break;
while (1) { case 8: delete_last(); break;
if (start == NULL) { printf("1: INSERT AT BEGINNING\n"); case 9:
printf("LIST IS EMPTY\n"); printf("2: INSERT AT SECOND POSITION\n"); printf("ENTER POSITION TO DELETE: ");
return; printf("3: INSERT AT LAST POSITION\n"); scanf("%d", &position);
} printf("4: INSERT AT ANY POSITION\n"); delete_node_at_any_pos(position);
printf("5: INSERT INTO SORTED LIST\n"); break;
printf("NODES AT ODD POSITIONS: "); printf("6: TRAVERSE LIST\n"); case 10:
while (temp != NULL) { printf("7: DELETE FIRST NODE\n"); printf("TOTAL NODES: %d\n", count_nodes());
if (position % 2 != 0) { printf("8: DELETE LAST NODE\n"); break;
printf("%d\t", temp->info); printf("9: DELETE NODE AT ANY POSITION\n"); case 11: reverse_list(); break;
} printf("10: COUNT NODES\n"); case 12: bubbleSort(start); break;
temp = temp->next; printf("11: REVERSE LIST\n"); case 13: print_odd_position_nodes(); break;
position++; printf("12: SORT LIST\n"); case 14: print_even_position_nodes(); break;
} printf("13: PRINT NODES AT ODD POSITIONS\n"); case 0: return;
printf("\n"); printf("14: PRINT NODES AT EVEN POSITIONS\n"); default: printf("INVALID CHOICE\n");
} printf("0: EXIT\n"); }
printf("ENTER YOUR CHOICE: "); }
void print_even_position_nodes() { scanf("%d", &choice); return;
struct Node *temp = start; }
int position = 1; switch (choice) {
case 1: insert_beginning(); break;
if (start == NULL) { case 2: insert_second_position(); break;
printf("LIST IS EMPTY\n"); case 3: insert_last_position(); break;
return; case 4:
} printf("ENTER POSITION AND VALUE TO BE
INSERTED: ");
printf("NODES AT EVEN POSITIONS: "); scanf("%d%d", &position, &value);
while (temp != NULL) { insert_at_any_position(position, value);
if (position % 2 == 0) { break;
printf("%d\t", temp->info); case 5:
}

You might also like