Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node{
- struct node *prev;
- int data;
- struct node *next;
- } *a, *b, *c, *d, *head, *tail, *current;
- //1. Create the linked list with 4 nodes.
- void create(){
- a = (struct node*)malloc(sizeof(struct node));
- b = (struct node*)malloc(sizeof(struct node));
- c = (struct node*)malloc(sizeof(struct node));
- d = (struct node*)malloc(sizeof(struct node));
- a->prev = NULL;
- a->data = 10;
- a->next = b;
- b->prev = a;
- b->data = 20;
- b->next = c;
- c->prev = b;
- c->data = 30;
- c->next = d;
- d->prev = c;
- d->data = 40;
- d->next = NULL;
- head = a;
- tail = d;
- }
- void display(){
- current = head;
- while(current != NULL){
- printf("%d ", current->data);
- current = current->next;
- }
- }
- // 2. Insert a new node at the beginning.
- void appendFirst(int key){
- struct node *newNode;
- current = head;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->prev = NULL;
- newNode->data = key;
- newNode->next = head;
- head = newNode;
- }
- // 3. Insert a new node at the end.
- void appendLast(int key){
- current = tail;
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = key;
- newNode->next = NULL;
- tail->next = newNode;
- newNode->prev = tail;
- }
- // 4. Insert a new node after the 3rd node.
- void appendAtThird(int data){
- current = head;
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = data;
- int i=1;
- for(i=1;i<3;i++){
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- newNode->prev = current;
- current->next->prev = newNode;
- }
- // 5. Insert a new node after a node by searching the data with a specific key.
- void appendAfterKey(int key){
- current = head;
- while(current->data != key){
- current = current->next;
- }
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = 666;
- newNode->next = current->next;
- current->next = newNode;
- current->next->prev = newNode;
- newNode->prev = current;
- }
- //6.Insert a new node before a node by searching the data with a specific key.
- void appendBeforeKey(int key){
- current = head;
- while(current->next->data != key){
- current = current->next;
- }
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = 444;
- newNode->next = current->next;
- current->next = newNode;
- current->next->prev = newNode;
- newNode->prev = current;
- }
- // 7. Update data of a node after searching the node and replace by a inputted value.
- void updateAtKey(int key){
- current = head;
- while(current->data != key){
- current = current->next;
- }
- current->data = 22;
- }
- // 8. Delete the first node.
- void deleteFirst(){
- current = head;
- current->next->prev = NULL;
- head = current->next;
- }
- //9. Delete the last node.
- void deleteLast(){
- current = tail;
- current->prev->next = NULL;
- tail = tail;
- }
- //10. Delete the node after searching with a specific key.
- void deleteAtKey(int key){
- current = head;
- while(current->next->data != key){
- current = current->next;
- }
- current->next = current->next->next;
- current->next->next->prev = current;
- }
- //(Pro) 11. Delete the next node after searching with a specific key.
- void deleteAfterKey(int key){
- current = head;
- while(current->data != key){
- current = current->next;
- }
- current->next = current->next->next;
- current->next->next->prev = current;
- }
- // 12. Delete the previous node after searching with a specific key.
- void deleteBeforeKey(int key){
- current = head;
- while(current->next->data != key){
- current = current->next;
- }
- current->prev->next = current->next;
- current->next->prev = current->prev;
- }
- int main(){
- printf("Creating 4 Nodes: \n");
- create();
- display();
- printf("\n\nAppending Node at first: \n");
- appendFirst(22);
- display();
- printf("\n\nAppending Node at Last: \n");
- appendLast(50);
- display();
- printf("\n\nAppending Node at 3rd: \n");
- appendAtThird(500);
- display();
- printf("\n\nAppending after key: \n");
- appendAfterKey(20);
- display();
- printf("\n\nAppending before key: \n");
- appendBeforeKey(30);
- display();
- printf("\n\nUpdating at a key: \n");
- updateAtKey(444);
- display();
- printf("\n\nDelete First: \n");
- deleteFirst();
- display();
- printf("\n\nDelete Last: \n");
- deleteLast();
- display();
- printf("\n\nDelete At a Key 500: \n");
- deleteAtKey(500);
- display();
- printf("\n\nDelete After Key 20: \n");
- deleteAfterKey(20);
- display();
- printf("\n\nDelete Before Key 20: \n");
- //deleteAfterKey(22);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement