[go: up one dir, main page]

0% found this document useful (0 votes)
8 views3 pages

Inserting and Traversing The Elements of A Linked List

The document provides a C program for managing a linked list, including functions to create an empty list, insert elements at the beginning, end, or a specific position, and traverse the list. It includes a main menu for user interaction to perform these operations. The program utilizes a structure to define nodes and employs dynamic memory allocation for node creation.

Uploaded by

highonmeth890
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)
8 views3 pages

Inserting and Traversing The Elements of A Linked List

The document provides a C program for managing a linked list, including functions to create an empty list, insert elements at the beginning, end, or a specific position, and traverse the list. It includes a main menu for user interaction to perform these operations. The program utilizes a structure to define nodes and employs dynamic memory allocation for node creation.

Uploaded by

highonmeth890
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/ 3

Inserting and Traversing the elements of a linked list.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node {
int num;
struct node *next;
}*start;

void createmptylist(struct node**start) {


*start = NULL;
}

void traverse(struct node *start) {


struct node *p = start;
while (p != NULL) {
printf("%d \n", p->num);
p = p->next;
}
}

void insertatbegin(int item) {


struct node *p;
p = malloc(sizeof(struct node));
p->num = item;

if (start == NULL)
p->next = NULL;
else
p->next = start;

start = p;
}

void insertatend(int item) {


struct node *p, *loc;
p = malloc(sizeof(struct node));
p->num = item;
p->next = NULL;

if (start == NULL)
start = p;
else {
loc = start;
while (loc->next != NULL) {
loc = loc->next;
}
loc->next = p;
}
}

void insert_spe(int item, int position) {


struct node *p, *temp;
int k;

p =malloc(sizeof(struct node));
p->num = item;
p->next = NULL;

if (position == 0) {
p->next = start;
start = p;
return;
}

temp = start;
for (k = 0; k < position - 1 && temp != NULL; k++) {
temp = temp->next;
}

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

p->next = temp->next;
temp->next = p;
}

void main() {
int choice, item, position;

clrscr();
createmptylist(&start);

do {
clrscr();
printf("1. Insert element at begin \n");
printf("2. Insert element at end position\n");
printf("3. Insert at specific position\n");
printf("4. Traverse the list in order\n");
printf("5. Exit\n");
printf("Enter your choice\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the item\n");
scanf("%d", &item);
insertatbegin(item);
break;

case 2:
printf("Enter the item\n");
scanf("%d", &item);
insertatend(item);
break;

case 3:
printf("Enter the item\n");
scanf("%d", &item);
printf("Enter the position\n");
scanf("%d", &position);
insert_spe(item, position);
break;

case 4:
printf("\nTraverse the list:\n");
traverse(start);
getch(); // To pause and see the output
break;

case 5:
exit(0);
}

} while (choice != 5);


}

You might also like