Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions DSA_CPP/creatingDoublyLL.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <stdio.h>
#include <stdlib.h>

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<n; ++i)
{
x = rand()%100;
insertAtTail(lp, x);
}
return lp;
}

int main()
{
List *lp;

lp = createList();
show(lp);
showReverse(lp);

return 0;
}
80 changes: 80 additions & 0 deletions DSA_CPP/queueClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>

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: "<<q.deletion()<<endl;

//x = q.deletion(); //underflow
return 0;
}