Program to show basic funnctions and how to declear Data structures in C.

/*Stack*/

struct stack
{
int top;
int maxstk;
char arr[100];
};

/*Queue*/

struct queue
{
int maxqueue;
int top;
int rear;
char arr[100];
}

/*insertion into a queue*/

void insert(struct queue q, char item)
{
if(q.front==0)
{
q.front=q.rear=1;
q.arr[q.front]=item;
}
else
if(q.rear==q.maxqueue-1)
{
printf("The queue is full");
exit(1);
}
else
q.arr[++q.rear]=item;
}

/*deletion from a queue*/

char delete(struct queue q)
{
char c;
if(front==0 && rear==0)
{
printf("The queue is empty");
exit(1);
}
else
if(front==rear)
{
c=q.arr[q.front];
front=rear=0;
return c;
}
else
return q.arr[q.front++];
}

/*insertion into a circular queue*/

void insert(struct queue q, char item)
{
if(front==0)
{
front=rear=1;
q.arr[q.front]=item;
}
else
if( (q.front==1 && q.rear==q.maxqueue-1) || (q.front=q.rear+1) )
{
printf("The queue is full");
exit(1);
}
else
if(q.rear==q.maxqueue-1)
{
q.rear=1;
q.arr[q.rear]=item;
}
else
q.arr[++q.rear]=item;
}

/*deletion from a circular queue*/

char delete(struct queue q)
{
char c;
if(q.front==0)
{
printf("The queue is empty");
exit(1);
}
else
if(q.front==q.rear)
{
c=q.arr[q.front];
q.front=q.rear=0;
return c;
}
else
if(q.front==q.maxqueue-1)
{
c=q.arr[q.front];
q.front=1;
return c;
}
else
return q.arr[q.front++];
}

/*Linked List*/

struct node
{
int item;
struct node *next;
}

/*insertion into a linked list*/

void insert(struct node *where, struct node s)
{
struct node *temp;
temp=where->next;
where->next=&s;
s.next=temp;
}

/*delete from a linked list*/

void delete(struct node *where)
{
struct node *temp;
where->next->next=temp;
free(where->next);
where->next=temp;
}

/*insert an element into a stack*/

void insert(struct stack s,char item)
{
if(top==maxstk-1)
{
printf("The stack is full");
exit(1);
}
else
s.arr[++s.top]=item;
}

/*delete an element from a stack*/

char delete(struct stack s)
{
if(s.top==-1)
{
printf("The stack is empty");
exit(1);
}
else
return s.arr[s.top--];
}


/*doubly linked list*/

struct node
{
int item;
struct node *next;
struct node *previous;
}

/*insertion into a doubly linked list*/

void insert(struct node *where, struct node *s)
{
s=where->next->previous;
s->next=where->next;
s->previous=where;
where->next=s;
}

/*deletion from a doubly linked list*/

void delete(struct node *where)
{
struct node *temp;
where->next->next=temp;
where->next->next->previous=where;
free(where->next);
where->next=temp->next->previous;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s