0% found this document useful (0 votes)
11 views

Linkedlist C

LL

Uploaded by

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

Linkedlist C

LL

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

struct node {
int data;
struct node *link;
};

struct node *start = NULL;


struct node *ptr = NULL;
struct node *temp = NULL;
struct node *p = NULL;
struct node *q = NULL;

int append() {
ptr = (struct node *)malloc(sizeof(struct node));

printf("\nEnter the value to add at the end: ");


scanf("%d", &ptr->data);

if (start == NULL) {
start = ptr;
return 1;
}

temp = start;

while (temp != NULL) {


if (temp->link == NULL) {
// temp is the last node in the list
temp->link = ptr;
return 1;
}
temp = temp->link;
}

// The loop should never reach this point


return 0;
}

void add_beg() {
ptr = (struct node *)malloc(sizeof(struct node));

printf("\nEnter the value to add at the beginning: ");


scanf("%d", &ptr->data);

if (start == NULL) {
ptr->link = NULL;
start = ptr;
return;
}

ptr->link = start;
start = ptr;
}

void del_end() {
if (start == NULL) {
printf("Linked list is empty\n");
return;
}

if (start->link == NULL) {
temp = start;
start = NULL;
free(temp);
return;
}

p = start;
temp = start->link;

while (temp->link != NULL) {


p = temp;
temp = temp->link;
}

p->link = NULL;
free(temp);
}

void display() {
temp = start;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->link;
}
}

int count() {
q = start;
int c = 0;
while (q != NULL) {
c++;
q = q->link;
}
return c;
}

void add_any_pos() {
int i, pos;
printf("\nEnter the position where you want to insert the node: ");
scanf("%d", &pos);

if (pos < 1 || pos > count() + 1) {


printf("Invalid position\n");
return;
}

ptr = (struct node *)malloc(sizeof(struct node));


printf("Enter data: ");
scanf("%d", &ptr->data);

if (pos == 1) {
ptr->link = start;
start = ptr;
return;
}

temp = start;
for (i = 0; i < pos - 1 && temp != NULL; i++) {
temp = temp->link;
}

ptr->link = temp->link;
temp->link = ptr;
}

void traverse() {
temp = start;

if (temp == NULL) {
printf("Linked list is empty\n");
return;
}

while (temp != NULL) {


printf("%d ", temp->data);
temp = temp->link;
}

printf("\n");
}

void deleteBeg() {
if (start == NULL) {
printf("Linked list is empty\n");
return;
}

temp = start;
start = start->link;
free(temp);
}

void deleteAtAnyPos() {
int pos;
printf("Enter position: ");
scanf("%d", &pos);

if (start == NULL) {
printf("Linked list is empty\n");
return;
}

if (pos == 1) {
deleteBeg();
return;
}
p = start;
temp = start->link;
int i;
for ( i = 0; i < pos - 1 && temp != NULL; i++) {
p = temp;
temp = temp->link;
}

if (temp == NULL) {
printf("Invalid position\n");
return;
}

p->link = temp->link;
free(temp);
}

void deleteKey() {
int k;

printf("Enter key: ");


scanf("%d", &k);

if (start == NULL) {
printf("Linked list is empty\n");
return;
}

temp = start->link;
p = start;
while (temp != NULL) {
if (temp->data == k) {
p->link = temp->link;
free(temp);
return;
}
p = temp;
temp = temp->link;
}

printf("Key not found in the linked list\n");


}

void create() {
int n, i;
printf("Enter the number of nodes: ");
scanf("%d", &n);

for (i = 0; i < n; i++) {


append();
}
}

int main() {
create();

int op = 0;

while (op != 8) {
printf("\nMENU\n1. Insert at beginning\n2. Insert at end\n3. Insert at position\n4. Traverse\
n5. Delete at start\n6. Delete at position\n7. Delete by key\n8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &op);

switch (op) {
case 1:
add_beg();
break;

case 2:
append();
break;

case 3:
add_any_pos();
break;

case 4:
traverse();
break;

case 5:
deleteBeg();
break;

case 6:
deleteAtAnyPos();
break;
case 7:
deleteKey();
break;

case 8:
printf("Exiting program\n");
break;

default:
printf("Invalid option. Please enter a valid option.\n");
}
}

return 0;
}

You might also like