/*menu program to illustrate singly linked list*/
#include<stdio.h>
struct llist {
int info;
struct llist *next;
};
struct llist *start;
void insrtbeg() {
struct llist *nnode;
nnode = (struct llist *) malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d", &nnode->info);
nnode->next = start;
start = nnode;
}
void insrtend() {
struct llist *nnode, *temp;
nnode = (struct llist *) malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d", &nnode->info);
nnode->next = NULL;
if (start == NULL) {
start = nnode;
return;
}
temp = start;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = nnode;
}
int getcounter() {
struct llist *temp;
int counter;
temp = start;
for (counter = 0; temp->next != NULL; counter++) {
temp = temp->next;
}
return counter + 1;
}
void insrtnth() {
int pos, i;
struct llist *nnode, *temp;
if (start == NULL) {
insrtbeg();
return;
}
printf("\nEnter the position to insert data: ");
scanf("%d", &pos);
if ((pos > getcounter() + 1) || (pos == 0)) {
printf("\nInvalid position!");
return;
}
if (pos == (getcounter() + 1)) {
insrtend();
return;
}
if (pos == 1) {
insrtbeg();
return;
}
nnode = (struct llist *) malloc(sizeof(struct llist));
printf("\nEnter data to insert: ");
scanf("%d", &nnode->info);
temp = start;
for (i = 1; i < pos - 1; i++) {
temp = temp->next;
}
nnode->next = temp->next;
temp->next = nnode;
}
void deltfirst() {
if (start == NULL) {
printf("\nList is empty!");
return;
}
printf("\nThe deleted item is: %d", start->info);
start = start->next;
}
void deltlast() {
struct llist *temp;
if (start == NULL) {
printf("\nList is empty!");
return;
}
if (start->next == NULL) {
start = NULL;
return;
}
temp = start;
while (temp->next->next != NULL) {
temp = temp->next;
}
printf("\nThe deleted item is: %d", temp->next->info);
temp->next = NULL;
}
void deltnth() {
int pos, i;
struct llist *temp;
if (start == NULL) {
printf("\nList is empty!");
return;
}
printf("\nEnter the position to delete data: ");
scanf("%d", &pos);
if ((pos > getcounter()) || (pos == 0)) {
printf("\nInvalid position!");
return;
}
if (pos == getcounter()) {
deltlast();
return;
}
if (pos == 1) {
deltfirst();
return;
}
temp = start;
for (i = 1; i < pos - 1; i++) {
temp = temp->next;
}
printf("\nThe deleted item is: %d", temp->next->info);
temp->next = temp->next->next;
}
void traverse() {
struct llist *temp;
if (start == NULL) {
printf("\nList is empty!");
return;
}
temp = start;
printf("\nThe List items are:\n");
while (temp->next != NULL) {
printf("%5d", temp->info);
temp = temp->next;
}
printf("%5d", temp->info);
}
void search() {
int s;
struct llist *temp;
if (start == NULL) {
printf("\nList is empty!");
return;
}
temp = start;
printf("\nEnter data to search: ");
scanf("%d", &s);
while (temp->next != NULL) {
if (temp->info == s) {
printf("\nThe given data is found in the list");
return;
}
temp = temp->next;
}
if (temp->info == s) {
printf("\nThe given data is found in the list");
return;
}
printf("\nThe given data is not found in the list");
}
void main() {
int a;
start = NULL;
while (1) {
printf("\nMENU");
printf("\n1.Insert at the beginning");
printf("\n2.Insert at last");
printf("\n3.Insert at the nth position");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete nth node");
printf("\n7.Traverse all nodes");
printf("\n8.Search any value");
printf("\n9.Exit");
printf("\n\n\tEnter your choice: ");
scanf("%d", &a);
switch (a) {
case 1:
insrtbeg();
break;
case 2:
insrtend();
break;
case 3:
insrtnth();
break;
case 4:
deltfirst();
break;
case 5:
deltlast();
break;
case 6:
deltnth();
break;
case 7:
traverse();
break;
case 8:
search();
break;
case 9:
exit(1);
default:
printf("\nInvalid Choice!\n");
}
}
}
WHAT'S NEW?
Loading...
0 comments:
Post a Comment