Untitled

 avatar
unknown
plain_text
2 years ago
4.8 kB
6
Indexable
#include <stdio.h>
#include <stdlib.h>

struct linkedlist {
    int data;
    struct linkedlist *next;
};
struct stack{
  int a[1000];
  int top;
}s;
struct linkedlist* front = NULL;
struct linkedlist* back = NULL;
struct linkedlist* newB = NULL;
struct linkedlist* newF = NULL;
struct linkedlist* newbw = NULL;

int count(){
    struct linkedlist* temp = front;
    int count = 0;
    while(temp!=NULL){
        count++;
        temp = temp->next;
    }
    return count;
}
void insert_at_last(int v){
    newB = (struct linkedlist*)malloc(sizeof(struct linkedlist));
    newB->data = v;
    newB->next = NULL;
    if(front == NULL){
        front= back = newB;
    }
    else{
        (*back).next = newB;
        back = newB;
    }
    printf("%d is pushed in the end of linkedlist\n",v);

}
void insert_at_between(int v, int k){
    int size = count();
    if(k<0||k>size){
        printf("Invalid position!!\n");
    }
    else{
        newbw=(struct linkedlist *) malloc(sizeof(struct linkedlist));
        newbw->data = v;
        struct linkedlist* itr = front;     // itr is after node
        int i = 1;
        while(i<k){
            itr = itr->next;
            i++;
        }
        struct linkedlist* before = itr->next;
        newbw->next = before;
        itr->next = newbw;
        printf("%d is pushed after %d element of linkedlist\n",v,k);

    }
}
void print(){
    struct linkedlist* temp = front;
    printf("LINKED LIST IS: ");
    while(temp!=NULL){
        printf("%d ",temp->data);
        temp = temp->next;
    }printf("\n");
}

void insert_at_begin(int v){
    newF = (struct linkedlist*)malloc(sizeof(struct linkedlist));
    newF->data = v;
    newF->next = front;
    if(front==NULL) front= back = newF;
    else front = newF;
    printf("%d is pushed in the beginning of linkedlist\n",v);
}
int deletion(int node){
  int size = count();
  if(node<1 || node > size){
    printf("Node Does NOT Exist");
    return -1;
  }
  else if(node == 1){
    struct linkedlist* temp = front;
    front = temp->next;
    return temp->data;
  }
  else{
    struct linkedlist* temp = front;
    struct linkedlist* tempbefore = NULL;
    int i = 1;
    while(i != node){
      tempbefore = temp;
      temp = temp->next;
      i++;
    }
    tempbefore->next = temp->next;
    return temp->data;
    free(temp);
  }
}
void reverse(){
  struct linkedlist* temp = front;
  while(temp!=NULL){
    s.a[++s.top]  = temp->data;
    temp = temp->next;
  }
  temp = front;
  while(s.top>=0){
    temp->data = s.a[s.top--];
    temp = temp->next;
  }
  free(temp);
}
int main(){
    /*pushfront(10);
    pushback(90);
    pushback(70);
    // pushfront(20);
    pushback(60);
    pushback(50);
    pushfront(30);
    pushafter(40,5);//push after first element
    print();*/

     s.top = -1;
     int choice,value,i=1;
     printf("OPERATIONS : \n");
     printf("1. PUSHED AN ELEMENT FROM BACK IN THE LINKED LIST\n");
     printf("2. PUSHED AN ELEMENT FROM FRONT IN THE LINKED LIST\n");
     printf("3. PUSHED AN ELEMENT AFTER Nth NODE IN THE LINKED LIST\n");
     printf("4. DELETE A NODE FROM THE LINKEDLIST\n");
     printf("5. COUNT THE ELEMNETS PRESENT IN THE LINKEDLIST\n");
     printf("6. PRINT THE LINKEDLIST\n");
     printf("7. REVERSE THE LINKEDLIST AND PRINT IT\n");

     printf("8. EXIT\n");
     do{

       printf("Enter Your Choice: ");
       scanf("%d",&choice);
       switch(choice){

         case 1:{
                 printf("Enter a Number: "); scanf("%d",&value);
                 insert_at_last(value); break;
                }
         case 2:{
                 printf("Enter a Number: "); scanf("%d",&value);
                 insert_at_begin(value); break;
                }
         case 3:{
                 printf("Enter a Number: "); scanf("%d",&value);
                 int nodenum;
                 printf("Enter the node number : "); scanf("%d",&nodenum);
                 insert_at_between(value,nodenum); break;
                }
         case 4:{
                 int nodenum;
                 printf("Enter the node number : "); scanf("%d",&nodenum);
                 printf("%d is deleted from LinkedList\n",deletion(nodenum)); break;
                }
         case 5:{
                 printf("Number of Nodes in linkedlist : %d",count());  break;
           
                }
         case 6:{
                 print(); break;
                }
         case 7:{
                 printf("Before Reversing "); print();
                 reverse();
                 printf("After Reversing ");  print();
                 break;
                }
         case 8:{
                  i = 0; printf("Exited Successfuly\n"); break;
                }
         default: printf("WRONG CHOICE!! TRY AGAIN.."); break;

       }

      printf("\n\n");
    }while(i);

  return 0;
}
Editor is loading...