Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to implement Linear Linked List.
- #include <stdio.h>
- #include <malloc.h>
- #include <conio.h>
- struct student {
- char name[20];
- struct student *next;
- } *first = NULL, *last, *temp;
- void insert() {
- temp = (struct student *) malloc(sizeof(struct student));
- printf("Enter name: ");
- scanf("%s", temp->name);
- temp->next = NULL;
- if (first == NULL) {
- first = temp;
- last = temp;
- printf("Created first node.");
- getch();
- } else {
- last->next = temp;
- last = temp;
- printf("Inserted new node.");
- getch();
- }
- }
- void display() {
- temp = first;
- while (temp != NULL) {
- printf("%s\n", temp->name);
- temp = temp->next;
- }
- getch();
- }
- void search() {
- char name[20];
- int count = 1;
- printf("Enter name to search: ");
- scanf("%s", name);
- temp = first;
- while (temp != NULL) {
- if (!strcmp(name, temp->name)) {
- printf ("Match found at node %d.", count);
- getch();
- break;
- }
- count++;
- temp = temp->next;
- }
- if (temp == NULL) {
- printf("Match not found.");
- getch();
- }
- }
- void erase() {
- if (first->next == NULL) {
- printf("The list cannot be empty.");
- getch();
- return;
- }
- temp = first;
- first = first->next;
- free(temp);
- printf("Deleted first node.");
- getch();
- }
- void main()
- {
- char choice;
- while (1) {
- clrscr();
- printf("1.Insert node\n2.Display list\n3.Search node\n4.Delete node\n5.Exit\n\n");
- choice = getch();
- switch (choice) {
- case '1':
- insert();
- break;
- case '2':
- display();
- break;
- case '3':
- search();
- break;
- case '4':
- erase();
- break;
- case '5':
- return;
- default:
- printf("Invalid input.");
- getch();
- }
- }
- }
Add Comment
Please, Sign In to add comment