Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //#include <string.h>
- struct node
- {
- int value;
- struct node *next;
- struct node *prev;
- };
- struct node *head = NULL, *tail = NULL;
- void insertfirst(int x);
- void insertlast(int x);
- void print();
- int search(int x);
- void remove(int x);
- int length();
- int main()
- {
- while (1)
- {
- printf(" ...MENU...\n");
- printf("(1) Insert First\n");
- printf("(2) Insert Last\n");
- printf("(3) Show All\n");
- printf("(4) Search\n");
- printf("(5) Remove\n");
- printf("(6) Length\n");
- printf("(8) Exit\n");
- printf("------------\n");
- printf("Enter Your Choice:");
- int choice;
- scanf("%d", &choice);
- //system("cls");
- int x;
- switch (choice)
- {
- case 1:
- printf("Enter Number: ");
- scanf("%d", &x);
- insertfirst(x);
- print();
- break;
- case 2:
- printf("Enter Number: ");
- scanf("%d", &x);
- insertlast(x);
- print();
- break;
- case 3:
- print();
- break;
- case 4:
- printf("Search value : ");
- scanf("%d", &x);
- if (search(x))
- printf("Found\n");
- else
- printf("Not Found\n");
- break;
- case 5:
- printf("Which value you want to remove : ");
- scanf("%d", &x);
- remove(x);
- print();
- break;
- case 6:
- printf("length = %d\n", length());
- break;
- case 8:
- exit(0);
- }
- }
- }
- void insertfirst(int x)
- {
- node *temp = (node*)malloc(sizeof(node));
- if (head == NULL)
- {
- temp->prev = NULL;
- temp->value = x;
- temp->next = NULL;
- head = temp;
- tail = head;
- }
- else
- {
- temp->prev = NULL;
- temp->value = x;
- temp->next = head;
- head->prev = temp;
- head = temp;
- }
- }
- void print()
- {
- node *currentnode = head;
- printf("\nYour List is: ");
- while (currentnode != NULL)
- {
- printf("%d ", currentnode->value);
- currentnode = currentnode->next;
- }
- printf("\n\n\n");
- }
- void insertlast(int x)
- {
- node *temp = (node*)malloc(sizeof(node));
- if (head == NULL)
- {
- temp->prev = NULL;
- temp->value = x;
- temp->next = NULL;
- head = temp;
- tail = temp;
- }
- else
- {
- temp->prev = tail;
- temp->value = x;
- temp->next = NULL;
- tail->next = temp;
- tail = temp;
- }
- }
- int search(int x)
- {
- node *currentnode = head;
- while (currentnode != NULL)
- {
- if (currentnode->value == x)
- return 1;
- currentnode = currentnode->next;
- }
- return 0;
- }
- int length()
- {
- int l = 0;
- node *currentnode = head;
- while (currentnode != NULL)
- {
- l++;
- currentnode = currentnode->next;
- }
- return l;
- }
- void remove(int x){
- node *temp=head, *prev = NULL, *currentnode =NULL;
- while (temp->value != x){
- temp = temp->next;
- }
- if (temp->prev == NULL){
- head = head->next;
- free(temp);
- head->prev = NULL;
- }
- else if (temp->next == NULL){
- currentnode = temp->prev;
- free(temp);
- currentnode->next = NULL;
- }
- else {
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- free(temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement