0% found this document useful (0 votes)
20 views5 pages

Linked List

The document contains C code for implementing a singly linked list with various operations such as creating a list, displaying it, inserting and deleting nodes at different positions, and reversing the list. It includes functions for handling user input and displaying the linked list. The main function provides a menu-driven interface for interacting with the linked list operations.

Uploaded by

kaveri.nagare
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)
20 views5 pages

Linked List

The document contains C code for implementing a singly linked list with various operations such as creating a list, displaying it, inserting and deleting nodes at different positions, and reversing the list. It includes functions for handling user input and displaying the linked list. The main function provides a menu-driven interface for interacting with the linked list operations.

Uploaded by

kaveri.nagare
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

Name:=Kaveri Nagare

Rollno:=UIT2025005
_____________________________________________________________________________________
#include <stdio.h> }
#include <stdlib.h>
void Display(struct node *head) {
struct node { if (head == NULL) {
int data; printf("List is empty!\n");
struct node *next; return;
}; }
struct node *temp = head;
struct node* createNode(int val) { while (temp != NULL) {
struct node *newnode = (struct printf("%d -> ", temp->data);
node*)malloc(sizeof(struct node)); temp = temp->next;
newnode->data = val; }
newnode->next = NULL; printf("NULL\n");
return newnode; }
}
struct node* InsertAtBeg(struct node* head, int
struct node* createLinkedList() { val) {
struct node *head = NULL, *p = NULL, *temp struct node* newnode = createNode(val);
= NULL; newnode->next = head;
int n, data; return newnode;
printf("How many nodes you want to insert: }
");
scanf("%d", &n); struct node* InsertAtLast(struct node* head, int
val) {
if (n <= 0) return NULL; struct node* newnode = createNode(val);
if (head == NULL) return newnode;
printf("Enter the node values:\n");
for (int i = 0; i < n; i++) { struct node *p = head;
scanf("%d", &data); while (p->next != NULL) {
temp = createNode(data); p = p->next;
if (head == NULL) { }
head = temp; p->next = newnode;
p = head; return head;
} else { }
p->next = temp;
p = p->next; struct node* InsertInBet(struct node* head, int
} pos, int val) {
} if (pos <= 1) return InsertAtBeg(head, val);
return head;
struct node* newnode = createNode(val); }
struct node* temp = head;
int i = 1; struct node* DeleteByValue(struct node* head,
int val) {
while (i < pos - 1 && temp->next != NULL) { if (head == NULL) {
temp = temp->next; printf("List is Empty\n");
i++; return NULL;
} }
newnode->next = temp->next;
temp->next = newnode; struct node* temp = head;
return head; if (head->data == val) {
} head = head->next;
free(temp);
struct node* DeleteAtBeg(struct node* head) { return head;
if (head == NULL) { }
printf("List is Empty\n");
return NULL; struct node* prev = NULL;
} while (temp != NULL && temp->data != val) {
struct node* temp = head; prev = temp;
head = head->next; temp = temp->next;
free(temp); }
return head; if (temp == NULL) {
} printf("Value %d not found!\n", val);
return head;
struct node* DeleteAtEnd(struct node* head) { }
if (head == NULL) { prev->next = temp->next;
printf("List is Empty\n"); free(temp);
return NULL; return head;
} }
if (head->next == NULL) {
free(head); struct node* SLLReverse(struct node* head) {
return NULL; struct node* prevnode = NULL;
} struct node* current = head;
struct node* nextnode = NULL;
struct node *p = head, *temp = NULL;
while (p->next->next != NULL) { while (current != NULL) {
p = p->next; nextnode = current->next;
} current->next = prevnode;
temp = p->next; prevnode = current;
p->next = NULL; current = nextnode;
free(temp); }
return head; return prevnode;
} printf("Enter value to delete: ");
scanf("%d", &val);
int main() { head = DeleteByValue(head,val);
struct node *head = NULL; break;
int ch, val, pos; case 9:
do { head = SLLReverse(head);
printf("\[Link] Linked List:"); printf("List reversed successfully!\n");
printf("\[Link]:"); Display(head);
printf("\[Link] At Beginning:"); break;
printf("\[Link] At Ending:"); case 10: printf("Exiting...\n"); break;
printf("\[Link] At Position:"); default: printf("Invalid choice\n");
printf("\[Link] At Beginning:"); }
printf("\[Link] At End:"); } while (ch != 10);
printf("\[Link] by value:"); return 0;
printf("\[Link] list"); }
printf("\[Link]"); OUTPUT:=
printf("\nEnter Your Choice: ");
scanf("%d", &ch); [Link] Linked List:
[Link]:
switch(ch) { [Link] At Beginning:
case 1: head = createLinkedList(); break; [Link] At Ending:
case 2: Display(head); break; [Link] At Position:
case 3: [Link] At Beginning:
printf("Enter value: "); [Link] At End:
scanf("%d", &val); [Link] by value:
head = InsertAtBeg(head,val); [Link] list
break; [Link]
case 4: Enter Your Choice: 1
printf("Enter value: "); How many nodes you want to insert: 4
scanf("%d", &val); Enter the node values:
head = InsertAtLast(head,val); 1245
break;
case 5: [Link] Linked List:
printf("Enter position: "); [Link]:
scanf("%d", &pos); [Link] At Beginning:
printf("Enter value: "); [Link] At Ending:
scanf("%d", &val); [Link] At Position:
head = InsertInBet(head,pos,val); [Link] At Beginning:
break; [Link] At End:
case 6: head = DeleteAtBeg(head); break; [Link] by value:
case 7: head = DeleteAtEnd(head); break; [Link] list
case 8: [Link]
Enter Your Choice: 2 [Link]:
1 -> 2 -> 4 -> 5 -> NULL [Link] At Beginning:
[Link] At Ending:
[Link] Linked List: [Link] At Position:
[Link]: [Link] At Beginning:
[Link] At Beginning: [Link] At End:
[Link] At Ending: [Link] by value:
[Link] At Position: [Link] list
[Link] At Beginning: [Link]
[Link] At End: Enter Your Choice: 9
[Link] by value: List reversed successfully!
[Link] list 4 -> 2 -> 1 -> 123 -> NULL
[Link]
Enter Your Choice: 3 [Link] Linked List:
Enter value: 123 [Link]:
[Link] At Beginning:
[Link] Linked List: [Link] At Ending:
[Link]: [Link] At Position:
[Link] At Beginning: [Link] At Beginning:
[Link] At Ending: [Link] At End:
[Link] At Position: [Link] by value:
[Link] At Beginning: [Link] list
[Link] At End: [Link]
[Link] by value: Enter Your Choice: 6
[Link] list
[Link] [Link] Linked List:
Enter Your Choice: 2 [Link]:
123 -> 1 -> 2 -> 4 -> 5 -> NULL [Link] At Beginning:
[Link] At Ending:
[Link] Linked List: [Link] At Position:
[Link]: [Link] At Beginning:
[Link] At Beginning: [Link] At End:
[Link] At Ending: [Link] by value:
[Link] At Position: [Link] list
[Link] At Beginning: [Link]
[Link] At End: Enter Your Choice: 2
[Link] by value: 2 -> 1 -> 123 -> NULL
[Link] list
[Link] [Link] Linked List:
Enter Your Choice: 7 [Link]:
[Link] At Beginning:
[Link] Linked List: [Link] At Ending:
[Link] At Position:
[Link] At Beginning:
[Link] At End:
[Link] by value:
[Link] list
[Link]
Enter Your Choice: 8
Enter value to delete: 123

[Link] Linked List:


[Link]:
[Link] At Beginning:
[Link] At Ending:
[Link] At Position:
[Link] At Beginning:
[Link] At End:
[Link] by value:
[Link] list
[Link]
Enter Your Choice: 2
2 -> 1 -> NULL

[Link] Linked List:


[Link]:
[Link] At Beginning:
[Link] At Ending:
[Link] At Position:
[Link] At Beginning:
[Link] At End:
[Link] by value:
[Link] list
[Link]
Enter Your Choice:

You might also like