Posts

Showing posts from July, 2014

Implementation of Stack

Implementatio in C: Using Array #include<stdio.h> #include<stdlib.h> struct stack {     int top;     unsigned capacity;     int* array; }; struct stack* create(unsigned capacity) {     struct stack* stack = (struct stack*)malloc(sizeof(struct stack));     stack->top = -1;     stack->capacity = capacity;     stack->array = (int*)malloc(capacity * sizeof(int));     return(stack); } int isFull(struct stack* stack) {     return(stack->top == stack->capacity - 1); } int isEmpty(struct stack* stack) {     return(stack->top == -1); } void push(struct stack* stack, int data) {     if(isFull(stack))         printf("Stack overflow");     stack->array[++stack->top] = data;     printf("\nPushed data on stack : %d\n",data); } int pop(struct stack* stack) {     if(isEmpty(...

Function to find Identical LinkedList

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } int identical(struct node* a,struct node* b) {     while(1)     {     if(a == NULL && b == NULL)         return 1;     if(a == NULL && b != NULL)         return 0;     if(a != NULL && b == NULL)         return 0;     if(a->data != b->data)         return 0;     a = a->next;     b = b->next;     } } int main() {     struct node* a = NULL;     struct node* b = NULL;     p...

Delete Alternate Nodes in LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void alternateDelete(struct node* head) {     struct node* prev= head;     struct node* node=head->next ;     if(head == NULL)         return;     while(prev != NULL && node != NULL)     {     prev->...

Move Last Element to first of LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void relocate(struct node** head) {     struct node* current= *head;     struct node* temp= NULL;     if(*head == NULL || (*head)->next == NULL)         return;     while(current->next != NULL)     {     tem...

Pairwise Swap elements of LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void pairswap(struct node* head) {     struct node* current = head;     while(current!=NULL && current->next != NULL)     {     swap(&current->data,&current->next->data);     current = current->next->next; ...

Remove duplicates from sorted LL

void removeDuplicates(struct node* head) {     struct node* current = head;     struct node* next;     if(current == NULL)         return;     while(current->next!= NULL)     {         if(current->data == current->next->data)         {         next=current->next->next;         free(current->next);         current->next=next;         }         else         {         current = current->next;         }     } }   Time Complexity:  O(n)

Print reverse of LL by recursive function

void printReverse(struct node* head) { if(head == NULL) return; else printReverse(head->list); printf("%d",head->data); } Time Complexity:  O(n)

Find intersection point of two LL

Logic: 1) Find the total number of elements in both the LL. 2) Find the difference between number of elements of the two linked list. 3) Move the pointer of that LL which contains more number of elements by the difference. 4) Then move the pointers of both the list and check if nodes present in first LL is same as the node present in second LL. Time Complexity:  O(m+n) if first LL contains m elements and second LL contains n elements. Space Complexity:  O(1)

Insert element in LL in sorted order

Logic: 1) If LL is empty, then insert the node as head and return it. 2) If value of node to be inserted in LL is less than the value of the head node, then insert the node at start and make it head. 3) Otherwise, traverse through the list and find the appropriate place for the node and check if the value of node to be inserted is less than the value of next node, then insert the node before the next node. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; struct node* newNode(int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data=newData;     newNode->next=NULL;     return newNode; } void print(struct node* n) {     while(n!=NULL)     {         printf("%d    ",n->data);         n=n->next;   ...

Print Middle element of LL

Method 1: Traverse linked list using two pointers. Move first pointer by one and second pointer by two, when second pointer reaches to the end of the list, then first pointer will reach to the middle of the list. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,1);     push(&head,2);     push(&head,3);     push(&head,4);     push(&head,5);     push(&head,6);     push(&head,7);     printMiddle(head);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void printMiddle(struct node...

Function to get Nth Node in LL

From beginning of the list: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,8);     push(&head,7);     push(&head,3);     push(&head,4);     push(&head,1);     printf("LinkedList:\n");     print(head);     int value = GetNth(head,0);     printf("\nValue of Nth node is: %d",value);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } int GetNth(struct node* head, int index) {     struct node* current = head;     int count =0;     while(current != NULL)    ...

Deletion in LL

#include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; }; int main() {     struct node *head = NULL;     push(&head,7);     push(&head,8);     push(&head,4);     push(&head,3);     printf("\n Linked List is:\n");     print(head);     delete(&head,8);     printf("\n LinkedList after Deletion of 8: \n");     print(head);     return 0; } void push(struct node** ref, int newData) {     struct node *newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void delete(struct node** headref,int key) {     struct node* temp = *headref;struct node* prev;     if(temp != NULL && temp->data == key)     {   ...

Insertion in LL

#include<stdio.h> #include<stdlib.h> struct node { int data ; struct node * next ; }; int main () { struct node * head = NULL ; append ( & head , 8 ); append ( & head , 7 ); push ( & head , 3 ); push ( & head , 4 ); append ( & head , 5 ); insertAfter ( head -> next -> next , 1 ); printf ( "Linked List is: \n " ); print ( head ); return 0 ; } void push ( struct node ** ref , int newData ) { struct node * newNode = ( struct node * ) malloc ( sizeof ( struct node )); newNode -> data = newData ; newNode -> next = ( * ref ); ( * ref ) = newNode ; } void insertAfter ( struct node * prev , int newData ) { if ( prev == NULL ) { printf ( "the given previous node can't be null" ); return 0 ; } struct node * newNode = ( struct node * ) malloc ( sizeof ( struct node )); ...

LinkedList in C

#include<stdio.h> #include<stdlib.h> struct node {  int data;  struct node *next; }; int main() {  struct node *head = NULL;  struct node *second = NULL;  struct node *third = NULL;  head = (struct node*)malloc(sizeof(struct node));  second = (struct node*)malloc(sizeof(struct node));  third = (struct node*)malloc(sizeof(struct node));  head->data = 1;  head->next = second;  second -> data = 2;  second -> next = third;  third -> data =3;  third -> next = NULL;  print(head);  return 0; } void print(struct node *n) {  while(n!= NULL)  {  printf("%d",n->data);  n = n ->next;  } }