├── CircularLinkedList.c ├── DoublyLinkedList.c ├── LinkedList.c ├── LinkedList.cpp ├── ListUsingArray.c ├── QueueUsing LL.c ├── QueueUsingArrays.c ├── QueueUsingArraysShifting.c ├── StacksUsingArrays.c └── StacksUsingLL.c /CircularLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct lnode{//define node 6 | int data; 7 | struct lnode* next; //self referential str 8 | }; 9 | 10 | 11 | typedef struct lnode node; 12 | 13 | node* last = NULL; 14 | 15 | void insertAtBeginning(int val){ 16 | node* newNode =(node*)malloc(sizeof(node)); 17 | if(newNode==NULL){ 18 | printf("Out of Memory"); 19 | return; 20 | } 21 | 22 | newNode->data = val; 23 | if(last==NULL){ //list is empty 24 | newNode->next = newNode; 25 | last = newNode; 26 | } 27 | else{ 28 | newNode->next = last->next; 29 | last->next = newNode; 30 | } 31 | printf("\nInserted %d at the beginning",val); 32 | } 33 | 34 | void display(){ 35 | if(last==NULL){ 36 | printf("List is empty"); 37 | return; 38 | } 39 | printf("\nElements in the list are: "); 40 | node* temp = last->next; 41 | do{ 42 | printf("%d ",temp->data); 43 | temp = temp->next; 44 | }while(temp!=last->next); 45 | 46 | } 47 | 48 | void insertAtEnd(int val){ 49 | node* newNode =(node*)malloc(sizeof(node)); 50 | if(newNode==NULL){ 51 | printf("Out of Memory"); 52 | return; 53 | } 54 | 55 | newNode->data = val; 56 | if(last==NULL){ //list is empty 57 | newNode->next = newNode; 58 | last = newNode; 59 | } 60 | else{ 61 | newNode->next = last->next; 62 | last->next = newNode; 63 | last = newNode; 64 | } 65 | printf("\nInserted %d at the beginning",val); 66 | } 67 | 68 | void deleteAtBeginning(){ 69 | if(last==NULL){ 70 | printf("List is empty"); 71 | return; 72 | } 73 | 74 | node* del; 75 | 76 | if(last->next == last){ //only one element in list 77 | del = last; 78 | last = NULL; 79 | } 80 | else{ 81 | del= last->next; 82 | last->next = last->next->next; 83 | } 84 | printf("\ndeleted %d",del->data); 85 | free(del); 86 | 87 | } 88 | 89 | void deleteAtEnd(){ 90 | if(last==NULL){ 91 | printf("List is empty"); 92 | return; 93 | } 94 | 95 | node *del,*temp; 96 | 97 | 98 | if(last->next == last){ //only one element in list 99 | del = last; 100 | last = NULL; 101 | } 102 | else{ 103 | temp = last->next; 104 | while(temp->next!=last){ 105 | temp = temp->next; 106 | } 107 | del = last; 108 | temp->next = last->next; 109 | last = temp; 110 | 111 | } 112 | printf("\ndeleted %d",del->data); 113 | free(del); 114 | } 115 | int main(){ 116 | insertAtEnd(3); 117 | //insertAtEnd(4); 118 | //insertAtEnd(5); 119 | display(); 120 | 121 | deleteAtEnd(); 122 | display(); 123 | 124 | return 0; 125 | } 126 | 127 | /* 128 | try 129 | insertAfter(val) 130 | insertBefore(val) 131 | delete(val) 132 | doubly circular linkedlist 133 | */ 134 | -------------------------------------------------------------------------------- /DoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Doubly Linked List - c/cpp */ 5 | struct lnode{//define node 6 | int data; 7 | struct lnode* prev; 8 | struct lnode* next; //self referential str 9 | }; 10 | 11 | 12 | typedef struct lnode node; 13 | 14 | node* head = NULL; 15 | node* tail = NULL; 16 | 17 | node* head2 = NULL; 18 | node* tail2 = NULL; 19 | 20 | void insertAtBeginning(int val){ 21 | node* newNode=(node*)malloc(sizeof(node)); //allocate 22 | if(newNode==NULL){ 23 | printf("Out of memory"); 24 | return; 25 | } 26 | newNode -> data = val; 27 | newNode -> next = head; 28 | if(head == NULL){ //when list is empty 29 | tail = newNode; 30 | } 31 | else{ //when there is atleast one node 32 | head->prev = newNode; 33 | } 34 | head = newNode; 35 | newNode->prev = NULL; 36 | 37 | printf("\nInserted %d at the beginning",val); 38 | } 39 | 40 | void display(){ 41 | if(head==NULL){ 42 | printf("List is empty"); 43 | return; 44 | } 45 | node* temp = head; 46 | 47 | printf("\nElements in the list are: "); 48 | while(temp!=NULL){ 49 | printf("%d ",temp->data); 50 | temp = temp->next; //jump 51 | } 52 | } 53 | 54 | void displayRev(){ 55 | if(tail==NULL){ 56 | printf("List is empty"); 57 | return; 58 | } 59 | node* temp = tail; 60 | 61 | printf("\nElements in rev: "); 62 | while(temp!=NULL){ 63 | printf("%d ",temp->data); 64 | temp = temp->prev; //jump 65 | } 66 | } 67 | 68 | 69 | void insertAtPos(int pos,int val){ 70 | if(pos==0){ 71 | insertAtBeginning(val); 72 | return; 73 | } 74 | 75 | int i; 76 | node* newNode=(node*)malloc(sizeof(node)); //allocate 77 | if(newNode==NULL){ 78 | printf("Out of memory"); 79 | return; 80 | } 81 | 82 | newNode->data = val; 83 | 84 | node* temp = head; 85 | 86 | for(i=1;i<=pos-1;i++){ //jump to prev node 87 | temp = temp -> next; 88 | if(temp==NULL){ 89 | printf("Invalid position\n"); 90 | return; 91 | } 92 | } 93 | 94 | newNode -> next = temp -> next; 95 | if(temp->next==NULL) 96 | tail = newNode; 97 | else 98 | temp->next->prev = newNode; 99 | 100 | temp -> next = newNode; 101 | newNode -> prev = temp; 102 | 103 | printf("Inserted %d at pos %d",val,pos); 104 | } 105 | 106 | void deleteAtPos(int pos){ 107 | int i; 108 | node* temp = head; 109 | node* prev; 110 | if(head==NULL){ 111 | printf("List is empty "); 112 | return; 113 | } 114 | 115 | if(pos==0){ 116 | head = head -> next; 117 | if(head==NULL) 118 | tail = NULL; 119 | else 120 | head->prev = NULL; 121 | printf("Deleted %d",temp -> data); 122 | free(temp); 123 | return; 124 | } 125 | 126 | for(i=1;i<=pos;i++){ 127 | prev = temp; 128 | temp = temp -> next; 129 | if(temp==NULL){ 130 | printf("Invalid position\n"); 131 | return; 132 | } 133 | } 134 | 135 | prev -> next = temp -> next; 136 | 137 | if(temp->next==NULL) 138 | tail = prev; 139 | else 140 | temp->next->prev = prev; 141 | printf("Deleted %d",temp -> data); 142 | free(temp); 143 | 144 | } 145 | 146 | 147 | int main(){ 148 | // insertAtBeginning(&head,&tail,3); 149 | // insertAtBeginning(&head2,&tail2,7); 150 | insertAtBeginning(4); 151 | /* insertAtBeginning(7); 152 | insertAtBeginning(9);*/ 153 | display(); 154 | deleteAtPos(0); 155 | display(); 156 | displayRev(); 157 | return 0; 158 | } 159 | 160 | 161 | /* 162 | loop- O(n) 163 | O(1) 164 | 165 | insertAtEnd 166 | deleteAtEnd 167 | deleteAtBeginning 168 | search 169 | contains 170 | length 171 | 172 | */ 173 | -------------------------------------------------------------------------------- /LinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct lnode{//define node 6 | int data; 7 | struct lnode* next; //self referential str 8 | }; 9 | 10 | 11 | typedef struct lnode node; 12 | 13 | node* head = NULL; 14 | 15 | void insertAtBeginning(int val){ 16 | node* newNode=(node*)malloc(sizeof(node)); //allocate 17 | if(newNode==NULL){ 18 | printf("Out of memory"); 19 | return; 20 | } 21 | 22 | newNode -> data = val; 23 | if(head == NULL){ //when list is empty 24 | newNode -> next = NULL; 25 | head = newNode; 26 | } 27 | else{ //when there is atleast one node 28 | newNode -> next = head; 29 | head = newNode; 30 | } 31 | 32 | printf("Inserted %d at the beginning",val); 33 | } 34 | 35 | void display(){ 36 | if(head==NULL){ 37 | printf("List is empty"); 38 | return; 39 | } 40 | node* temp = head; 41 | 42 | printf("Elements in the list are: "); 43 | while(temp!=NULL){ 44 | printf("%d ",temp->data); 45 | temp = temp->next; 46 | } 47 | 48 | 49 | } 50 | 51 | void insertAtPos(int pos,int val){ 52 | int i; 53 | node* newNode=(node*)malloc(sizeof(node)); //allocate 54 | if(newNode==NULL){ 55 | printf("Out of memory"); 56 | return; 57 | } 58 | 59 | newNode->data = val; 60 | 61 | node* temp = head; 62 | 63 | for(i=1;i<=pos-1;i++){ //jump to prev node 64 | temp = temp -> next; 65 | if(temp==NULL){ 66 | printf("Invalid position\n"); 67 | return; 68 | } 69 | } 70 | 71 | newNode -> next = temp -> next; 72 | temp -> next = newNode; 73 | 74 | printf("Inserted %d at pos %d",val,pos); 75 | } 76 | 77 | void deleteAtPos(int pos){ 78 | int i; 79 | node* temp = head; 80 | node* prev; 81 | if(head==NULL){ 82 | printf("List is empty "); 83 | return; 84 | } 85 | 86 | if(pos==0){ 87 | head = head -> next; 88 | printf("Deleted %d",temp -> data); 89 | free(temp); 90 | return; 91 | } 92 | 93 | for(i=1;i<=pos;i++){ 94 | prev = temp; 95 | temp = temp -> next; 96 | if(temp==NULL){ 97 | printf("Invalid position\n"); 98 | return; 99 | } 100 | } 101 | 102 | prev -> next = temp -> next; 103 | printf("Deleted %d",temp -> data); 104 | free(temp); 105 | 106 | } 107 | 108 | int main(){ 109 | int choice,val,pos; 110 | while(1){ 111 | printf("\n -------- Linked List Menu -----------\n"); 112 | printf("1.Insert at Beginning\n"); 113 | printf("2.Display the list\n"); 114 | printf("3.Insert at specified position \n"); 115 | printf("4.Delete from specified position\n"); 116 | printf("5.Exit\n"); 117 | printf("\n--------------------------------------\n"); 118 | printf("Enter your choice:\t"); 119 | scanf("%d",&choice); 120 | switch(choice){ 121 | case 1: printf("Enter the data: "); 122 | scanf("%d",&val); 123 | insertAtBeginning(val); 124 | break; 125 | case 2: display(); 126 | break; 127 | case 3: printf("Enter the pos\(starts at 0\): "); 128 | scanf("%d",&pos); 129 | if(pos<0){ 130 | printf("Invalid position"); 131 | break; 132 | } 133 | printf("Enter the data: "); 134 | scanf("%d",&val); 135 | if(pos==0) 136 | insertAtBeginning(val); 137 | else 138 | insertAtPos(pos,val); 139 | break; 140 | case 4: printf("Enter the pos: "); 141 | scanf("%d",&pos); 142 | if(pos<0){ 143 | printf("Invalid position"); 144 | break; 145 | } 146 | deleteAtPos(pos); 147 | break; 148 | case 5: exit(0); 149 | 150 | default: printf("Wrong choice"); 151 | 152 | 153 | } 154 | } 155 | return 0; 156 | } 157 | -------------------------------------------------------------------------------- /LinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct lnode{//define node 6 | int data; 7 | lnode* next; //self referential str 8 | }; 9 | 10 | 11 | typedef struct lnode node; 12 | 13 | 14 | class LinkedList{ 15 | 16 | node* head = NULL; 17 | public: 18 | void insertAtBeginning(int val){ 19 | node* newNode=(node*)malloc(sizeof(node)); //allocate 20 | if(newNode==NULL){ 21 | printf("Out of memory"); 22 | return; 23 | } 24 | 25 | newNode -> data = val; 26 | if(head == NULL){ //when list is empty 27 | newNode -> next = NULL; 28 | head = newNode; 29 | } 30 | else{ //when there is atleast one node 31 | newNode -> next = head; 32 | head = newNode; 33 | } 34 | 35 | printf("Inserted %d at the beginning",val); 36 | } 37 | 38 | void display(){ 39 | if(head==NULL){ 40 | printf("List is empty"); 41 | return; 42 | } 43 | node* temp = head; 44 | 45 | printf("Elements in the list are: "); 46 | while(temp!=NULL){ 47 | printf("%d ",temp->data); 48 | temp = temp->next; 49 | } 50 | 51 | 52 | } 53 | 54 | void insertAtPos(int pos,int val){ 55 | int i; 56 | node* newNode=(node*)malloc(sizeof(node)); //allocate 57 | if(newNode==NULL){ 58 | printf("Out of memory"); 59 | return; 60 | } 61 | 62 | newNode->data = val; 63 | 64 | node* temp = head; 65 | 66 | for(i=1;i<=pos-1;i++){ //jump to prev node 67 | temp = temp -> next; 68 | if(temp==NULL){ 69 | printf("Invalid position\n"); 70 | return; 71 | } 72 | } 73 | 74 | newNode -> next = temp -> next; 75 | temp -> next = newNode; 76 | 77 | printf("Inserted %d at pos %d",val,pos); 78 | } 79 | 80 | void deleteAtPos(int pos){ 81 | int i; 82 | node* temp = head; 83 | node* prev; 84 | if(head==NULL){ 85 | printf("List is empty "); 86 | return; 87 | } 88 | 89 | if(pos==0){ 90 | head = head -> next; 91 | printf("Deleted %d",temp -> data); 92 | free(temp); 93 | return; 94 | } 95 | 96 | for(i=1;i<=pos;i++){ 97 | prev = temp; 98 | temp = temp -> next; 99 | if(temp==NULL){ 100 | printf("Invalid position\n"); 101 | return; 102 | } 103 | } 104 | 105 | prev -> next = temp -> next; 106 | printf("Deleted %d",temp -> data); 107 | free(temp); 108 | 109 | } 110 | 111 | }; 112 | 113 | 114 | int main(){ 115 | int choice,val,pos; 116 | LinkedList list1; 117 | LinkedList list2; 118 | while(1){ 119 | printf("\n -------- Linked List Menu -----------\n"); 120 | printf("1.Insert at Beginning\n"); 121 | printf("2.Display the list\n"); 122 | printf("3.Insert at specified position \n"); 123 | printf("4.Delete from specified position\n"); 124 | printf("5.Exit\n"); 125 | printf("\n--------------------------------------\n"); 126 | printf("Enter your choice:\t"); 127 | scanf("%d",&choice); 128 | switch(choice){ 129 | case 1: printf("Enter the data: "); 130 | scanf("%d",&val); 131 | list1.insertAtBeginning(val); 132 | break; 133 | case 2: list1.display(); 134 | break; 135 | case 3: printf("Enter the pos\(starts at 0\): "); 136 | scanf("%d",&pos); 137 | if(pos<0){ 138 | printf("Invalid position"); 139 | break; 140 | } 141 | printf("Enter the data: "); 142 | scanf("%d",&val); 143 | if(pos==0) 144 | list1.insertAtBeginning(val); 145 | else 146 | list1.insertAtPos(pos,val); 147 | break; 148 | case 4: printf("Enter the pos: "); 149 | scanf("%d",&pos); 150 | if(pos<0){ 151 | printf("Invalid position"); 152 | break; 153 | } 154 | list1.deleteAtPos(pos); 155 | break; 156 | case 5: exit(0); 157 | 158 | default: printf("Wrong choice"); 159 | 160 | 161 | } 162 | } 163 | return 0; 164 | } 165 | -------------------------------------------------------------------------------- /ListUsingArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 50 3 | int arr[MAX_SIZE]; 4 | int size = 0; 5 | 6 | 7 | void display(){ 8 | int i; 9 | for(i=0;i=pos;i--){ 18 | arr[i+1]=arr[i]; 19 | } 20 | 21 | arr[pos]= val; 22 | size++; 23 | printf("Inserted %d at pos %d",val,pos); 24 | } 25 | 26 | void deleteFromPos(int pos){ 27 | int i; 28 | int del = arr[pos]; 29 | for(i=pos;i=size) 59 | { 60 | printf("Invalid position"); 61 | break; 62 | } 63 | printf("Enter the data: "); 64 | scanf("%d",&val); 65 | insertAtPos(pos,val); 66 | break; 67 | case 3: if(size==0){ 68 | printf("Array is empty"); 69 | break; 70 | } 71 | printf("Enter the pos: "); 72 | scanf("%d",&pos); 73 | if(pos<0 || pos>=size) 74 | printf("Invalid position"); 75 | else 76 | deleteFromPos(pos); 77 | break; 78 | 79 | case 4: display(); 80 | break; 81 | case 5: exit(0); 82 | 83 | default: printf("Wrong choice"); 84 | break; 85 | //read,update,search,length,deleteAtEnd,deleteFromBeginning,insertAtBeginning 86 | 87 | } 88 | } 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /QueueUsing LL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct lnode{ 5 | int data; 6 | struct lnode* next; 7 | }; 8 | 9 | typedef struct lnode node; 10 | 11 | struct Queue{ 12 | node* front; 13 | node* rear; 14 | }; 15 | 16 | 17 | struct Queue* createQueue(){ 18 | 19 | struct Queue* p = (struct Queue*)malloc(sizeof(struct Queue)); 20 | p->front = NULL; 21 | p->rear = NULL; 22 | 23 | return p; //return q pointer 24 | }; 25 | 26 | void enqueue(struct Queue* q, int val){ 27 | 28 | node* newNode = (node*)malloc(sizeof(node)); 29 | if(newNode==NULL){ 30 | printf("Out of memory"); 31 | return; 32 | } 33 | 34 | newNode->data = val; 35 | newNode->next = NULL; 36 | if(q->rear==NULL) //adding the first element of the Q 37 | q->front = newNode; 38 | else 39 | q->rear->next = newNode; 40 | q->rear = newNode; 41 | printf("\nEnqueued %d",val); 42 | } 43 | 44 | int dequeue(struct Queue* q){ 45 | if(q->front==NULL){ 46 | return INT_MIN; 47 | } 48 | 49 | node* del = q->front; 50 | q->front = q->front->next; 51 | if(q->front==NULL) //when last element in the Q is delete 52 | q->rear = NULL; 53 | int temp = del->data; 54 | free(del); 55 | return temp; 56 | 57 | } 58 | 59 | int main(){ 60 | 61 | struct Queue* q1 = createQueue(); 62 | struct Queue* q2 = createQueue(); 63 | struct Queue* q3 = createQueue(); 64 | 65 | enqueue(q1,5); 66 | enqueue(q1,7); 67 | enqueue(q1,8); 68 | 69 | 70 | printf("\nDequeued %d " , dequeue(q1)); 71 | printf("\nDequeued %d " , dequeue(q1)); 72 | printf("\nDequeued %d " , dequeue(q1)); 73 | 74 | enqueue(q1,5); 75 | enqueue(q1,7); 76 | 77 | printf("\nDequeued %d " , dequeue(q1)); 78 | printf("\nDequeued %d " , dequeue(q1)); 79 | 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /QueueUsingArrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 30 5 | 6 | struct Queue{ 7 | int arr[MAX_SIZE]; 8 | int front,rear; 9 | }; 10 | 11 | struct Queue* createQueue(){ 12 | struct Queue* p = (struct Queue*)malloc(sizeof(struct Queue)); 13 | p->front = -1; 14 | p->rear = -1; 15 | return p; 16 | } 17 | 18 | void enqueue(struct Queue* Q,int val){ 19 | if(Q->rear==MAX_SIZE-1){ 20 | printf("Queue is full"); 21 | return; 22 | } 23 | if(Q->front==-1) //when adding first element of Q 24 | Q->front++; 25 | 26 | Q->arr[++(Q->rear)] = val; 27 | 28 | } 29 | 30 | int dequeue(struct Queue* Q){ 31 | if(Q->front==-1 || Q->front>Q->rear){//no elements added or all elements added have been dequeued 32 | printf("\nQueue is empty"); 33 | return INT_MIN; 34 | } 35 | 36 | return Q->arr[Q->front++]; 37 | } 38 | 39 | int elementAtFront(struct Queue* Q){ 40 | if(Q->front==-1 || Q->front>Q->rear){ 41 | printf("\nQueue is empty"); 42 | return INT_MIN; 43 | } 44 | 45 | return Q->arr[Q->front]; 46 | } 47 | 48 | int isEmpty(struct Queue* Q){ 49 | if(Q->front==-1 || Q->front>Q->rear){ 50 | return 1; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | int main(){ 57 | 58 | struct Queue *q = createQueue(); 59 | struct Queue *q2 = createQueue(); 60 | struct Queue *q3 = createQueue(); 61 | 62 | enqueue(q,5); 63 | enqueue(q,7); 64 | enqueue(q,4); 65 | enqueue(q2,1); 66 | 67 | printf("Dequeued %d",dequeue(q)); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /QueueUsingArraysShifting.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 30 5 | 6 | struct Queue{ 7 | int arr[MAX_SIZE]; 8 | int rear; 9 | }; 10 | 11 | struct Queue* createQueue(){ 12 | struct Queue* p = (struct Queue*)malloc(sizeof(struct Queue)); 13 | p->rear = -1; 14 | return p; 15 | } 16 | 17 | void enqueue(struct Queue* Q,int val){ 18 | if(Q->rear==MAX_SIZE-1){ 19 | printf("Queue is full"); 20 | return; 21 | } 22 | 23 | Q->arr[++(Q->rear)] = val; 24 | 25 | } 26 | 27 | int dequeue(struct Queue* Q){ 28 | 29 | if(Q->rear==-1){ 30 | printf("Queue is Empty"); 31 | return INT_MIN; 32 | } 33 | int temp = Q->arr[0]; 34 | int i; 35 | 36 | 37 | for(i=1;i<=Q->rear;i++) 38 | Q->arr[i-1] = Q->arr[i]; 39 | 40 | Q->rear--; 41 | return temp; 42 | 43 | } 44 | 45 | int elementAtFront(struct Queue* Q){ 46 | if(Q->rear==-1){ 47 | printf("Queue is Empty"); 48 | return INT_MIN; 49 | } 50 | return Q->arr[0]; 51 | } 52 | 53 | int isEmpty(struct Queue* Q){ 54 | if(Q->rear==-1){ 55 | return 1; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | int main(){ 62 | 63 | struct Queue *q = createQueue(); 64 | struct Queue *q2 = createQueue(); 65 | struct Queue *q3 = createQueue(); 66 | 67 | enqueue(q,5); 68 | enqueue(q,7); 69 | enqueue(q,4); 70 | enqueue(q2,1); 71 | 72 | printf("\nDequeued %d",dequeue(q)); 73 | printf("\nDequeued %d",dequeue(q)); 74 | printf("\nDequeued %d",dequeue(q)); 75 | printf("\nDequeued %d",dequeue(q)); 76 | 77 | enqueue(q,5); 78 | printf("\nDequeued %d",dequeue(q)); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /StacksUsingArrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_SIZE 20 4 | 5 | struct Stack{ 6 | int arr[MAX_SIZE]; 7 | int top ; 8 | }; 9 | 10 | struct Stack* createStack(){ 11 | struct Stack* p = (struct Stack*)malloc(sizeof(struct Stack)); 12 | p->top = -1; 13 | return p; 14 | } 15 | 16 | void push(struct Stack *stack,int val){ //O(1) 17 | if(stack->top==MAX_SIZE-1){ 18 | printf("Stack is Full"); 19 | return; 20 | } 21 | stack->arr[++stack->top] = val; 22 | 23 | } 24 | 25 | int pop(struct Stack *stack){ 26 | if(stack->top==-1){ 27 | printf("stack is Empty"); 28 | return INT_MIN; 29 | } 30 | return stack->arr[stack->top--]; 31 | } 32 | 33 | int peek(struct Stack *stack){ 34 | 35 | if(stack->top==-1){ 36 | printf("stack is Empty"); 37 | return INT_MIN; 38 | } 39 | return stack->arr[stack->top]; 40 | } 41 | 42 | 43 | int main(){ 44 | struct Stack *S = createStack(); 45 | struct Stack *S2 = createStack(); 46 | 47 | 48 | push(S,5); 49 | push(S,7); 50 | push(S2,3); 51 | 52 | printf("\nPopped %d " , pop(S)); 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /StacksUsingLL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct lnode{ 6 | int data; 7 | struct lnode* next; 8 | }; 9 | 10 | typedef struct lnode node; 11 | 12 | 13 | void push(node **top,int val){ 14 | node* newNode=(node*)malloc(sizeof(node)); 15 | if(newNode==NULL){ 16 | printf("Out of memory"); 17 | return; 18 | } 19 | newNode->data = val; 20 | newNode->next = *top; 21 | *top = newNode; 22 | } 23 | 24 | int pop(node **top){ //O(1) 25 | if(*top==NULL){ 26 | printf("stack is Empty"); 27 | return INT_MIN; 28 | } 29 | node* del = *top; 30 | *top = (*top)->next; 31 | int temp = del->data; 32 | free(del); 33 | return temp; 34 | 35 | } 36 | 37 | int peek(node **top){ 38 | 39 | if(*top==NULL){ 40 | printf("stack is Empty"); 41 | return INT_MIN; 42 | } 43 | return (*top)->data; 44 | } 45 | 46 | int main(){ 47 | 48 | node* top = NULL; 49 | node* top2 = NULL; 50 | 51 | push(&top,3); 52 | push(&top,4); 53 | push(&top,7); 54 | push(&top,9); 55 | 56 | printf("Popped %d",pop(&top)); 57 | 58 | return 0; 59 | } 60 | 61 | /* 62 | try 63 | insertAfter(val) 64 | delete(val) 65 | doubly circular linkedlist 66 | */ 67 | --------------------------------------------------------------------------------