|
3 | 3 | function can be modified according to the data type, easily.
|
4 | 4 | deleteNode deletes a node when passed with a key of the node.
|
5 | 5 | */
|
6 |
| -#include <stdio.h> |
7 |
| -#include <stdlib.h> |
8 |
| - |
9 |
| -struct Node |
10 |
| -{ |
11 |
| - int data; |
12 |
| - struct Node *next; |
| 6 | +#include<stdio.h> |
| 7 | +struct node |
| 8 | +{int info; |
| 9 | + struct node *link; |
13 | 10 | };
|
14 |
| - |
15 |
| -void push(struct Node** head_ref, int new_data) |
| 11 | +struct node *start=NULL; |
| 12 | +/////////////////////////////////////////////////////////// |
| 13 | +struct node * createnode()//function to create node |
16 | 14 | {
|
17 |
| - struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); |
18 |
| - new_node->data = new_data; |
19 |
| - new_node->next = (*head_ref); |
20 |
| - (*head_ref) = new_node; |
| 15 | + struct node *t; |
| 16 | + t=(struct node*)malloc(sizeof(struct node)); |
| 17 | + return(t); |
21 | 18 | }
|
22 |
| - |
23 |
| -void deleteNode(struct Node **head_ref, int key) |
| 19 | +//////////////////////////////////////////////////////// |
| 20 | +void insert()//function to insert at first location |
24 | 21 | {
|
25 |
| - struct Node* temp = *head_ref, *prev; |
26 |
| - |
27 |
| - if (temp != NULL && temp->data == key) |
| 22 | + struct node *p; |
| 23 | + p=createnode(); |
| 24 | + printf("\nenter the number to insert"); |
| 25 | + scanf("%d",&p->info); |
| 26 | + p->link=NULL; |
| 27 | + if(start==NULL) |
| 28 | + { |
| 29 | + start=p; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + p->link=start; |
| 34 | + start=p; |
| 35 | + } |
| 36 | +} |
| 37 | +/////////////////////////////////////////////////////////// |
| 38 | +void deleteion()//function to delete from first position |
| 39 | +{ |
| 40 | + struct node *t; |
| 41 | + if(start==NULL) |
28 | 42 | {
|
29 |
| - *head_ref = temp->next; |
30 |
| - free(temp); |
31 |
| - return; |
| 43 | + printf("\nlist is empty"); |
32 | 44 | }
|
33 |
| - |
34 |
| - |
35 |
| - while (temp != NULL && temp->data != key) |
| 45 | + else |
36 | 46 | {
|
37 |
| - prev = temp; |
38 |
| - temp = temp->next; |
| 47 | + struct node *p; |
| 48 | + p=start; |
| 49 | + start=start->link; |
| 50 | + free(p); |
39 | 51 | }
|
40 |
| - |
41 |
| - if (temp == NULL) return; |
42 |
| - |
43 |
| - prev->next = temp->next; |
44 |
| - |
45 |
| - free(temp); |
46 | 52 | }
|
47 |
| - |
48 |
| -void printList(struct Node *node) |
| 53 | +/////////////////////////////////////////////////////// |
| 54 | +void viewlist()//function to display values |
49 | 55 | {
|
50 |
| - while (node != NULL) |
| 56 | + struct node *p; |
| 57 | + if(start==NULL) |
51 | 58 | {
|
52 |
| - printf(" %d ", node->data); |
53 |
| - node = node->next; |
| 59 | + printf("\nlist is empty"); |
| 60 | + } |
| 61 | + else |
| 62 | + { p=start; |
| 63 | + while(p!=NULL) |
| 64 | + { |
| 65 | + printf("%d ",p->info); |
| 66 | + p=p->link; |
| 67 | + } |
54 | 68 | }
|
55 | 69 | }
|
| 70 | +////////////////////////////////////////////////////////////////////// |
56 | 71 |
|
57 | 72 | int main()
|
58 | 73 | {
|
59 |
| - /* new node can be created here as :- |
60 |
| - struct Node* head = NULL; |
61 |
| - push(&head, data); |
62 |
| -
|
63 |
| - and a node can be delete by using |
64 |
| - deleteNode(&head, key); |
65 |
| - */ |
66 |
| - return 0; |
| 74 | + int n; |
| 75 | + while(1) |
| 76 | + { |
| 77 | + printf("\n1.add value at first location"); |
| 78 | + printf("\n2.delete value from first location"); |
| 79 | + printf("\n3.view value"); |
| 80 | + printf("\nenter your choice"); |
| 81 | + scanf("%d",&n); |
| 82 | + switch(n) |
| 83 | + { |
| 84 | + case 1: |
| 85 | + insert(); |
| 86 | + break; |
| 87 | + case 2: |
| 88 | + deleteion(); |
| 89 | + break; |
| 90 | + case 3: |
| 91 | + viewlist(); |
| 92 | + break; |
| 93 | + default: |
| 94 | + printf("\ninvalid choice"); |
| 95 | + } |
| 96 | + } |
| 97 | + return(0); |
67 | 98 | }
|
0 commit comments