DEV Community

Adarsh
Adarsh

Posted on • Updated on

Linked List

Inserting into linked list at any position

Image description

#include <stdio.h>
#include <stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void print()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
  printf("\n");
}

void insert(int data, int n)
{
  Node *temp1 = new Node();
  temp1->data = data;
  temp1->next = NULL;
  if (n == 1)
  {
    temp1->next = head;
    head = temp1;
    print();
    return;
  }
  Node *temp2 = head;
  for (int i = 0; i < n - 2; i++)
  {
    temp2 = temp2->next;
  }

  temp1->next = temp2->next;
  temp2->next = temp1;
  print();
}

int main()
{
  head = NULL; // empty list
  insert(2, 1);
  insert(3, 2);
  insert(4, 1);
  insert(5, 2);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Deleting the node in the linked list

Image description

Image description

#include <stdio.h>
#include <stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void printList()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("[%d|%p]->", temp->data, (void *)&temp);
    temp = temp->next;
  }
  printf("\n");
}
void insert(int data)
{
  Node *temp = new Node();
  temp->data = data;
  temp->next = head;
  head = temp;
}
void deleteList(int n)
{
  struct Node *temp1 = head;
  if (n == 1)
  {
    head = temp1->next;
    free(temp1);
    return;
  }
  int i;
  for (i = 0; i < n - 2; i++)
  {
    temp1 = temp1->next;
  }
  printf("deleted data: %d ", temp1->next->data);

  printf("\n");
  struct Node *temp2 = temp1->next;
  temp1->next = temp2->next;
  free(temp2);
}
int main()
{
  head = NULL;
  int n;
  insert(1);
  insert(2);
  insert(3);
  insert(4);
  insert(5);
  insert(6);
  insert(7);
  printList();
  printf("Enter a position to delete \n");
  scanf("%d", &n);
  deleteList(n);
  printList();
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Reversing a linked list

Image description
Image description
Image description
Image description

#include <stdio.h>
#include <stdlib.h>
struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void reverseList()
{
  struct Node *prev, *current, *next;
  current = head;
  prev = NULL;
  while (current != NULL)
  {
    next = current->next;
    current->next = prev; // linking to prev node
    // updating for the next node
    prev = current;
    current = next;
  }
  head = prev;
}
void printList()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
  printf("\n");
}
void insertNode(int data)
{
  Node *temp = new Node();
  temp->data = data;
  temp->next = head;
  head = temp;
}
int main()
{
  struct Node *head = NULL;
  insertNode(1);
  insertNode(2);
  insertNode(3);
  insertNode(4);
  printf("Linked List : ");
  printList();
  reverseList();
  printf("Reversed list: ");
  printList();
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)