Write a C program to create linear linked list interactively and print out the list and total number of items in the list.
1: #include<stdio.h>
2: #include<conio.h>
3: #include<process.h>
4: #include<stdlib.h>
5: struct node
6: {
7: int info;
8: struct node *next;
9: }*head,*last,*temp,*n;
10: void menu(); // to select the choise
11: void create(); // insert at last
12: void display(); // to print the link list
13: int i;
14: void main()
15: {
16: i=0;
17: clrscr();
18: menu();
19: getch();
20: }
21: void menu()
22: {
23: int ch;
24: printf("1. Create\n");
25: printf("2. Display\n");
26: printf("3. Exit\n");
27: printf("Enter choise : ");
28: scanf("%d",&ch);
29: switch(ch)
30: {
31: case 1 : create();
32: case 2 : display();
33: case 3 : printf("You have choose to exit\n");
34: getch(); exit(0);
35: default : printf("Invalid choise\n"); menu();
36: }
37: }
38: void create()
39: {
40: n= new node;
41: printf("Enter the information : ");
42: scanf("%d",&n->info);
43: if(i==0)
44: {
45: head=n;
46: i++;
47: }
48: else
49: {
50: last->next=n;
51: i++;
52: }
53: last=n;
54: last->next=NULL;
55: menu();
56: }
57: void display()
58: {
59: if(head==NULL)
60: {
61: printf("No information in list\n");
62: menu();
63: }
64: temp=head;
65: while(temp!=NULL)
66: {
67: printf("%d->",temp->info);
68: temp=temp->next;
69: }
70: printf("\n");
71: printf("Total number of elements in the link list are : %d\n",i);
72: menu();
73: }
74: Output:
75: 1. Create
76: 2. Display
77: 3. Exit
78: Enter choise : 1
79: Enter the information : 12
80: 1. Create
81: 2. Display
82: 3. Exit
83: Enter choise : 1
84: Enter the information : 23
85: 1. Create
86: 2. Display
87: 3. Exit
88: Enter choise : 1
89: Enter the information : 45
90: 1. Create
91: 2. Display
92: 3. Exit
93: Enter choise : 2
94: 12->23->45->
95: Total number of elements in the link list are : 3
96: 1. Create
97: 2. Display
98: 3. Exit
99: Enter choise : 3
100: You have choose to exit
No comments:
Post a Comment