Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node{
- int data;
- struct node *next;
- }*a, *b, *c, *d, *head, *tail, *current;
- //1. Function to 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->data = 10;
- a->next = b;
- b->data = 20;
- b->next = c;
- c->data = 30;
- c->next = d;
- 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. Function to Insert a new node at the beginning.
- void appendStart(){
- struct node *n;
- n = (struct node*)malloc(sizeof(struct node));
- n->data = 5;
- n->next = head;
- head = n;
- }
- //3. Function to Insert a new node at the end.
- void appendLast(){
- struct node *n;
- n = (struct node*)malloc(sizeof(struct node));
- n->data = 50;
- n->next = NULL;
- tail->next = n;
- tail = n;
- }
- // 4. Function to Insert a new node after the 3rd node.
- void appendAtThird(){
- current = head;
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = 25;
- int i = 1;
- for(i=1; i<3; i++){
- current = current->next;
- }
- newNode->next = current->next;
- current->next = newNode;
- }
- //5. Function to create 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 = 121;
- newNode->next = current->next;
- current->next = newNode;
- }
- //6. Function to Insert a new node before a node by searching the data with a specific key.
- void appendBeforeKey(int key){
- current = head;
- int i=0;
- while(current->data != key){
- current = current->next;
- i++;
- }
- current = head;
- int j=1;
- for(j=1; j<i; j++){
- current = current->next;
- }
- struct node *newNode;
- newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = 121;
- newNode->next = current->next;
- current->next = newNode;
- }
- // 7. Function to Update data of a node after searching the node and replace by a inputted value.
- void updateByKey(int key, int val){
- current = head;
- while(current->data != key){
- current = current->next;
- }
- current->data = val;
- }
- // 8. Delete first node
- void deleteFirst(){
- current = head;
- head = current->next;
- }
- // 9. Delete last
- void deleteLast(){
- current = head;
- while(current->next != tail){
- current = current->next;
- }
- current->next = NULL;
- }
- //10. Delete at a key
- void deleteAtKey(int key){
- current = head;
- while(current->next->data != key){
- current=current->next;
- }
- current->next = current->next->next;
- }
- //11. Delete after key
- void deleteAfterKey(int key){
- current = head;
- while(current->data != key){
- current=current->next;
- }
- current->next = current->next->next;
- }
- //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->next = current->next->next;
- }
- int main(){
- printf("Creating the Node Structure: ");
- printf("\n");
- create();
- display();
- printf("\n\n");
- printf("Appending Node at Starting: \n");
- appendStart();
- display();
- printf("\n\n");
- printf("Appending at Last: \n");
- appendLast();
- display();
- printf("\n\n");
- printf("Appanding after third Node: \n");
- appendAtThird();
- display();
- printf("\n\n");
- printf("Appending After key 10: \n");
- appendAfterKey(10);
- display();
- printf("\n\n");
- printf("Appending Before key 40: \n");
- appendBeforeKey(40);
- display();
- printf("\n\n");
- printf("Updating at key 40: \n");
- updateByKey(40, 333);
- display();
- printf("\n\n");
- printf("Delete first Node: \n");
- deleteFirst();
- display();
- printf("\n\n");
- printf("Delete Last Node: \n");
- deleteLast();
- display();
- printf("\n\n");
- printf("Delete At Key 20: \n");
- deleteAtKey(20);
- display();
- printf("\n\n");
- printf("Delete After Key 30: \n");
- deleteAfterKey(30);
- display();
- printf("\n\n");
- printf("Delete Before Key 25: \n");
- deleteBeforeKey(25);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement