diff --git a/DSA_CPP/creatingDoublyLL.c b/DSA_CPP/creatingDoublyLL.c new file mode 100644 index 00000000..7259ec9c --- /dev/null +++ b/DSA_CPP/creatingDoublyLL.c @@ -0,0 +1,116 @@ +#include +#include + +typedef int itemType; + +//self referential structure +typedef struct node +{ + itemType info; + struct node *link, *prev; +}node; + +typedef struct List +{ + node *head, *tail; +}List; + +node *createNode(itemType x) +{ + node *p; + p = (node *)malloc(1*sizeof(node)); + if (p==NULL) + { + printf("Memory not allocated\n"); + exit(1); + } + p->info = x; + p->link = NULL; + p->prev = NULL; + return p; +} +void show(List *lp) +{ + node *p; + + p = lp->head; + printf("Linked list is: "); + while(p != NULL) + { + printf("%d---->", p->info); + p = p->link; + } + printf("NULL\n\n"); +} +void showReverse(List *lp) +{ + node *p; + + p = lp->tail; + printf("Linked list is: "); + while(p != NULL) + { + printf("%d---->", p->info); + p = p->prev; + } + printf("NULL\n\n"); +} + +void insertAtTail(List *lp, itemType x) +{ + node *p; + + p = createNode(x); + + if (lp->head == NULL) + lp->head = p; + else + { + lp->tail->link = p; + p->prev = lp->tail; + } + lp->tail = p; +} + +void initialize(List *lp) +{ + lp->head = NULL; + lp->tail = NULL; +} +List *createList() +{ + List *lp; + itemType x; + int n, i; + + //allocate space for head and tail + lp = (List *)malloc(sizeof(List)); + if (lp==NULL) + { + printf("Memory not allocated\n"); + exit(1); + } + initialize(lp); + + printf("Enter count of items: "); + scanf("%d", &n); + + srand(time(0)); + for (i=0; i + +using namespace std; + +typedef int itemType; + +class Queue +{ +private: + int front, rear, count, max; + itemType *a; + +public: + Queue(int s=5) + { + initialize(); + max = s; + a = new itemType[max]; + } + initialize() + { + front = 0; + rear = -1; + count = 0; + } + int full() + {return count==max;} + int empty() + {return count==0;} + + void addition(itemType x) + { + if (full()) + { + cout<<"Queue overflow\n"; + exit(1); + } + rear = (rear+1)%max; + a[rear] = x; + ++count; + } + + itemType deletion() + { + if (empty()) + { + cout<<"Queue underflow\n"; + exit(2); + } + itemType x; + x = a[front]; + front = (front+1)%max; + --count; + return x; + } + void show() + {} + + //free the memory allocated in the constructor + ~Queue() + {delete a;} +}; + +int main() +{ + Queue q(4); + itemType x; + + x=10; q.addition(x); + x=50; q.addition(x); + x=90; q.addition(x); + x=20; q.addition(x); + //x=10; q.addition(x); //overflow + + while (!q.empty()) + cout<<"The deleted item is: "< +#include + +#define MAX 20 +typedef int itemType; + +void fill(itemType L[], int n); +void show(itemType L[], int n); +void shellSort(itemType L[], int n); + +int main() +{ + itemType list[MAX]; + int n; + + printf("Enter the current size of the list: "); + scanf("%d", &n); + + fill(list, n); + show(list, n); + shellSort(list, n); + show(list, n); + + return 0; +} + +void shellSort(itemType L[], int n) +{ + int i, current, h; + itemType temp; + + for(h=n/3+1; h>=1; h=h/3+1) + { + for (current=h; current=0; i-=h) + { + if (L[i]>temp) + { + L[i+h] = L[i]; + //show(L,n); + //getch(); + } + else + break; + } + L[i+h] = temp; + } + } + +} +void fill(itemType L[], int n) +{ + int i; + srand(time(0)); + for (i=0; i