In this c code I've tried to complete the all of the task of linked list.Here, four methods of linked list has been used :
- insert method at line no : 9
- search method at line no : 24
- delete method at line no : 40
- print method at line no : 64
And start my main method at line no : 75
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node
- {
- int data;
- struct Node *next;
- }
- }node;
- void insert(node *pointer, int data)
- {
- {
- while(pointer->next!=NULL)
- {
- pointer = pointer -> next;
- }
- pointer->next = (node *)malloc(sizeof(node));
- pointer = pointer->next;
- pointer->data = data;
- pointer->next = NULL;
- }
- }
- int search(node *pointer, int key)
- {
- {
- pointer = pointer -> next;
- while(pointer!=NULL)
- {
- if(pointer->data == key) .
- {
- return 1;
- }
- pointer = pointer -> next;
- }
- return 0;
- }
- }
- void delete(node *pointer, int data)
- {
- {
- while(pointer->next!=NULL && (pointer->next)->data != data)
- {
- pointer = pointer -> next;
- }
- if(pointer->next==NULL)
- {
- printf("Element %d is not present in the list\n",data);
- return;
- }
- node *temp;
- temp = pointer -> next;
- pointer->next = temp->next;
- free(temp);
- return;
- }
- }
- void print(node *pointer)
- {
- {
- if(pointer==NULL)
- {
- return;
- }
- printf("%d ",pointer->data);
- print(pointer->next);
- }
- }
- int main()
- {
- {
- node *start,*temp;
- start = (node *)malloc(sizeof(node));
- temp = start;
- temp -> next = NULL;
- printf("\t\tLinked List\n-------------------------------------\n");
- printf(" Press 1 to Insert Data in the linked list\n");
- printf(" Press 2 Delete data from the linked list\n");
- printf(" Press 3 Print data of the linked list\n");
- printf(" Press 4 Find data from the linked data from the linked list\n");
- printf(" Press 0 To exit the program\n");
- while(1)
- {
- int query;
- printf("Enter your option: ");
- scanf("%d",&query);
- if(query==1)
- {
- int data;
- printf("Enter a value in the linked list : ");
- scanf("%d",&data);
- insert(start,data);
- printf("Value inserted successfully\n");
- }
- else if(query==2)
- {
- int data;
- printf("Enter a data do you want to delete from the linked list: ");
- scanf("%d",&data);
- delete(start,data);
- }
- else if(query==3)
- {
- printf("The list is ");
- print(start->next);
- printf("\n");
- }
- else if(query==4)
- {
- int data;
- scanf("%d",&data);
- int status = search(start,data);
- if(status)
- {
- printf("Element Found\n");
- }
- else
- {
- printf("Element Not Found\n");
- }
- }else if(query == 0){
- print("!!!..Program exited successfully..!!!");
- return 0;
- }
- }
- return 0;
- }
- }
Suggestion:
Don't try to direct copy paste please. This habit will damage your coding power.
Coding is an energy, is a habit, is a funny....
So, try to solve your coding problem by yourself.
For more on linked list, you can see my
previous large lecture on linked list.
See Linked List Details
Ok, since if you face any problem, you kindly write your comment in the comment box...Or mail me: manirujjamanakash@gmail.com. Or in facebook : https://web.facebook.com/manirujjaman.akash. You can better take your eyes in our facebook fanfage for getting regular post from funny-coder https://web.facebook.com/prgraamming.jagath
No comments:
Post a Comment