0% found this document useful (0 votes)
3 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)
3 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
You are on page 1/ 5

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("\n1.Create Linked List:"); printf("List reversed successfully!\n");
printf("\n2.Display:"); Display(head);
printf("\n3.Insert At Beginning:"); break;
printf("\n4.Insert At Ending:"); case 10: printf("Exiting...\n"); break;
printf("\n5.Insert At Position:"); default: printf("Invalid choice\n");
printf("\n6.Delete At Beginning:"); }
printf("\n7.Delete At End:"); } while (ch != 10);
printf("\n8.Delete by value:"); return 0;
printf("\n9.Reverse list"); }
printf("\n10.Exit"); OUTPUT:=
printf("\nEnter Your Choice: ");
scanf("%d", &ch); 1.Create Linked List:
2.Display:
switch(ch) { 3.Insert At Beginning:
case 1: head = createLinkedList(); break; 4.Insert At Ending:
case 2: Display(head); break; 5.Insert At Position:
case 3: 6.Delete At Beginning:
printf("Enter value: "); 7.Delete At End:
scanf("%d", &val); 8.Delete by value:
head = InsertAtBeg(head,val); 9.Reverse list
break; 10.Exit
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: 1.Create Linked List:
printf("Enter position: "); 2.Display:
scanf("%d", &pos); 3.Insert At Beginning:
printf("Enter value: "); 4.Insert At Ending:
scanf("%d", &val); 5.Insert At Position:
head = InsertInBet(head,pos,val); 6.Delete At Beginning:
break; 7.Delete At End:
case 6: head = DeleteAtBeg(head); break; 8.Delete by value:
case 7: head = DeleteAtEnd(head); break; 9.Reverse list
case 8: 10.Exit
Enter Your Choice: 2 2.Display:
1 -> 2 -> 4 -> 5 -> NULL 3.Insert At Beginning:
4.Insert At Ending:
1.Create Linked List: 5.Insert At Position:
2.Display: 6.Delete At Beginning:
3.Insert At Beginning: 7.Delete At End:
4.Insert At Ending: 8.Delete by value:
5.Insert At Position: 9.Reverse list
6.Delete At Beginning: 10.Exit
7.Delete At End: Enter Your Choice: 9
8.Delete by value: List reversed successfully!
9.Reverse list 4 -> 2 -> 1 -> 123 -> NULL
10.Exit
Enter Your Choice: 3 1.Create Linked List:
Enter value: 123 2.Display:
3.Insert At Beginning:
1.Create Linked List: 4.Insert At Ending:
2.Display: 5.Insert At Position:
3.Insert At Beginning: 6.Delete At Beginning:
4.Insert At Ending: 7.Delete At End:
5.Insert At Position: 8.Delete by value:
6.Delete At Beginning: 9.Reverse list
7.Delete At End: 10.Exit
8.Delete by value: Enter Your Choice: 6
9.Reverse list
10.Exit 1.Create Linked List:
Enter Your Choice: 2 2.Display:
123 -> 1 -> 2 -> 4 -> 5 -> NULL 3.Insert At Beginning:
4.Insert At Ending:
1.Create Linked List: 5.Insert At Position:
2.Display: 6.Delete At Beginning:
3.Insert At Beginning: 7.Delete At End:
4.Insert At Ending: 8.Delete by value:
5.Insert At Position: 9.Reverse list
6.Delete At Beginning: 10.Exit
7.Delete At End: Enter Your Choice: 2
8.Delete by value: 2 -> 1 -> 123 -> NULL
9.Reverse list
10.Exit 1.Create Linked List:
Enter Your Choice: 7 2.Display:
3.Insert At Beginning:
1.Create Linked List: 4.Insert At Ending:
5.Insert At Position:
6.Delete At Beginning:
7.Delete At End:
8.Delete by value:
9.Reverse list
10.Exit
Enter Your Choice: 8
Enter value to delete: 123

1.Create Linked List:


2.Display:
3.Insert At Beginning:
4.Insert At Ending:
5.Insert At Position:
6.Delete At Beginning:
7.Delete At End:
8.Delete by value:
9.Reverse list
10.Exit
Enter Your Choice: 2
2 -> 1 -> NULL

1.Create Linked List:


2.Display:
3.Insert At Beginning:
4.Insert At Ending:
5.Insert At Position:
6.Delete At Beginning:
7.Delete At End:
8.Delete by value:
9.Reverse list
10.Exit
Enter Your Choice:

You might also like