Saturday, April 30, 2011

TREE in Data Structure with C

TREE

It is a non-linear data structure that may contain several contents. In a tree the nodes are connected to each other a parent and child relationship where each parent node may have multiple children nodes but has exactly a single parent node. The node which has no parent at all is called a root node. All the other nodes in the tree are descendent of root node.

Root node:- The only node which has no parent at all is called root node. But itself it is a parent of all other nodes in the tree.

Leaf node – The node which has no child node at all are called leaf node/terminal node or external node.

Non-leaf node – The node which has at least one child node are called non-leaf nod / non-terminal or internal node.

Height of tree – The maximum distance of a leaf node form the root node is called height of the tree.

Level of a node – The distance of a particular node from the root node is called its level. The root node is itself considered a level zero; its children are at level one and so on.

NB – Maximum number of children in a binary tree can be only three.

Order / Degree of a tree – The maximum number of children at a node in tree have, is called order of tree of degree of a tree. In general a tree n has maximum children per node and (n-1) keys per node.

Binary Tree –
A tree in which each node can have maximum of two children is called a binary tree. In other words a tree of order two is called binary tree. In which each node can have only two children and single info field. The two children of a node are referred to as left and right son respectively.

Strictly Binary Tree –
A binary tree in which each node has exactly two children or no children at all is called strictly binary tree.

Complete or full Binary tree –
A strictly binary tree is which all the leaf node at the same level is called complete or full binary tree.

Binary Tree traverse –
Traversing a binary tree means to visit each and every node in the tree only once in a predetermine sequence. There are following technique to traverse a binary tree –
1. Pre-order traverse
2. In-order traverse
3. Post-order traverse
4. Level by level traverse

1. Pre-order traverse – In this technique of traverse to a binary tree, the root node is visited before visiting its left and right children. The sequence of traverse may be define recursive in following three steps –
a. Visit the root
b. Traverse the left sub-tree in pre-order
c. Traverse the right sub-tree in pre-order

2. In-order traverse – In this sequence of traversing a binary tree, the root node is visited after visiting the left children but before right child. The step for recursive definition is given as below –
a. Traverse the left sub-tree in in-order
b. Visit the root
c. Traverse the right sub-tree in in-order.

3. Post-order traverse – In this sequence of traversing, the root node visited after traversing both its left and right sub-tree. The steps are given below –
a. Traverse the left sub-tree in post-order
b. Traverse the right sub-tree in post-order
c. Visit the root node.

4. Level by level traverse – In this scheme of traversing a binary tree, the root node is visited first then the nodes at level one and then the nodes at level two and so on.

TREE in Data Structure with C

TREE

It is a non-linear data structure that may contain several contents. In a tree the nodes are connected to each other a parent and child relationship where each parent node may have multiple children nodes but has exactly a single parent node. The node which has no parent at all is called a root node. All the other nodes in the tree are descendent of root node.

Root node:- The only node which has no parent at all is called root node. But itself it is a parent of all other nodes in the tree.

Leaf node – The node which has no child node at all are called leaf node/terminal node or external node.

Non-leaf node – The node which has at least one child node are called non-leaf nod / non-terminal or internal node.

Height of tree – The maximum distance of a leaf node form the root node is called height of the tree.

Level of a node – The distance of a particular node from the root node is called its level. The root node is itself considered a level zero; its children are at level one and so on.

NB – Maximum number of children in a binary tree can be only three.

Order / Degree of a tree – The maximum number of children at a node in tree have, is called order of tree of degree of a tree. In general a tree n has maximum children per node and (n-1) keys per node.

Binary Tree –
A tree in which each node can have maximum of two children is called a binary tree. In other words a tree of order two is called binary tree. In which each node can have only two children and single info field. The two children of a node are referred to as left and right son respectively.

Strictly Binary Tree –
A binary tree in which each node has exactly two children or no children at all is called strictly binary tree.

Complete or full Binary tree –
A strictly binary tree is which all the leaf node at the same level is called complete or full binary tree.

Binary Tree traverse –
Traversing a binary tree means to visit each and every node in the tree only once in a predetermine sequence. There are following technique to traverse a binary tree –
1. Pre-order traverse
2. In-order traverse
3. Post-order traverse
4. Level by level traverse

1. Pre-order traverse – In this technique of traverse to a binary tree, the root node is visited before visiting its left and right children. The sequence of traverse may be define recursive in following three steps –
a. Visit the root
b. Traverse the left sub-tree in pre-order
c. Traverse the right sub-tree in pre-order

2. In-order traverse – In this sequence of traversing a binary tree, the root node is visited after visiting the left children but before right child. The step for recursive definition is given as below –
a. Traverse the left sub-tree in in-order
b. Visit the root
c. Traverse the right sub-tree in in-order.

3. Post-order traverse – In this sequence of traversing, the root node visited after traversing both its left and right sub-tree. The steps are given below –
a. Traverse the left sub-tree in post-order
b. Traverse the right sub-tree in post-order
c. Visit the root node.

4. Level by level traverse – In this scheme of traversing a binary tree, the root node is visited first then the nodes at level one and then the nodes at level two and so on.

Saturday, April 23, 2011

Queue Circular

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 5

struct cqueue
{
int info[MAX];
int front, rear, count;
} cq;

int main(void)
{
int ch, n;
cq.front = 0;
cq.rear = -1;
cq.count = 0;
while(1)
{
clrscr();
f_disp(&cq);
printf("\n\n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&cq, n);
break;
case 2: n = dequeue(&cq);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
else
{
printf("Queue underflow...");
getch();
break;
}
break;
case 3: disp(&cq);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(struct cqueue *q, int n)
{
if(q->count == MAX)
{
printf("\nQueue overflow...");
getch();
return;
}
q->rear = (q->rear+1)%MAX;
q->info[q->rear] = n;
q->count++;
}

/* delete node from queue list */
dequeue(struct cqueue *q)
{
int n;
if(q->count==0)
{
printf("Queue underflow...");
getch();
return -1;
}

n = q->info[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return n;
}


/* display items of queue */
disp(struct cqueue *q)
{
int i, n = q->count;
clrscr();
if(n == 0)
{
printf("No item...");
getch();
return;
}
else
{
printf("\n\n\tQueue are : \n");
for(i=q->front;n>0;n--)
{
printf("\t\t\t%d\n ", q->info[i]);
i = (i+1)%MAX;
}
}
printf("\n\nPress any key to return to main menu...");
getch();

}

/*display items on head of menu display. */
f_disp(struct cqueue *q)
{
int i, n = q->count;
if(n == 0)
{
printf("No item...");
return;
}
printf("Queue are : \n\t\t");
for(i=q->front;n>0;n--)
{
printf("%d, ", q->info[i]);
i = (i+1)%MAX;
}
}

Queue (Priority)

/* priority queue */

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

struct q
{
int info;
struct q *next;
} queue, *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueuePqueue(front, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */

enqueuePqueue(struct q *q, int n)
{
struct q *temp, *p = NULL;
temp = (struct q*)malloc(sizeof(struct q));
temp->info = n;
if(q==NULL)
{
front = rear = temp;
temp->next = NULL;
}
else if(ninfo)
{
temp->next = q;
front = temp;
}
else
{
while(q!=NULL&&q->infonext;
}
p->next = temp;
temp->next = q;
}
}



/* delete node from queue list */
dequeue(struct q *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(struct q *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(struct q *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue Circular

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 5

struct cqueue
{
int info[MAX];
int front, rear, count;
} cq;

int main(void)
{
int ch, n;
cq.front = 0;
cq.rear = -1;
cq.count = 0;
while(1)
{
clrscr();
f_disp(&cq);
printf("\n\n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&cq, n);
break;
case 2: n = dequeue(&cq);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
else
{
printf("Queue underflow...");
getch();
break;
}
break;
case 3: disp(&cq);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(struct cqueue *q, int n)
{
if(q->count == MAX)
{
printf("\nQueue overflow...");
getch();
return;
}
q->rear = (q->rear+1)%MAX;
q->info[q->rear] = n;
q->count++;
}

/* delete node from queue list */
dequeue(struct cqueue *q)
{
int n;
if(q->count==0)
{
printf("Queue underflow...");
getch();
return -1;
}

n = q->info[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return n;
}


/* display items of queue */
disp(struct cqueue *q)
{
int i, n = q->count;
clrscr();
if(n == 0)
{
printf("No item...");
getch();
return;
}
else
{
printf("\n\n\tQueue are : \n");
for(i=q->front;n>0;n--)
{
printf("\t\t\t%d\n ", q->info[i]);
i = (i+1)%MAX;
}
}
printf("\n\nPress any key to return to main menu...");
getch();

}

/*display items on head of menu display. */
f_disp(struct cqueue *q)
{
int i, n = q->count;
if(n == 0)
{
printf("No item...");
return;
}
printf("Queue are : \n\t\t");
for(i=q->front;n>0;n--)
{
printf("%d, ", q->info[i]);
i = (i+1)%MAX;
}
}

Queue (Priority)

/* priority queue */

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

struct q
{
int info;
struct q *next;
} queue, *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueuePqueue(front, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */

enqueuePqueue(struct q *q, int n)
{
struct q *temp, *p = NULL;
temp = (struct q*)malloc(sizeof(struct q));
temp->info = n;
if(q==NULL)
{
front = rear = temp;
temp->next = NULL;
}
else if(ninfo)
{
temp->next = q;
front = temp;
}
else
{
while(q!=NULL&&q->infonext;
}
p->next = temp;
temp->next = q;
}
}



/* delete node from queue list */
dequeue(struct q *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(struct q *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(struct q *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue Double (can insert through both end)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;

queue *rear, *front;

int main(void)
{
int ch, n;
clrscr();
while(1)
{
clrscr();
f_disp(front);
printf("\n1. Enqueue rear\n2. Enqueue Front\n3. Dequeue front\n4. Dequeue rear\n5. Display\n6. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: n = getinput();
enqueue_rear(rear,n);
break;
case 2: n = getinput();
enqueue_front(front, n);
break;
case 3: dequeue_front(front);
break;
case 4: dequeue_rear(front);
break;
case 5: disp(front);
break;
case 6: exit(0);
default: printf("Invalid choice.\nPlease enter 1 - 6\n");
getch();
}
}
}

int getinput()
{
int n;
printf("Enter the new info : ");
scanf("%d",&n);
return n;
}

enqueue_rear(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

enqueue_front(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = s;
front = temp;
if(s == NULL)
rear = temp;
}

dequeue_front(queue *s)
{
int n;
if(s!=NULL)
{
n = s->info;
front = s->next;
if(s->next == NULL)
rear = NULL;
free(s);
return n;
}
else
{ clrscr();
printf("\n\n\t\tQueue underflow...");
getch();
return -1;
}
getch();
}

dequeue_rear(queue *s)
{
queue *p;
int n;
if(s == NULL)
{
clrscr();
printf("\n\n\t\tQueue Underflow...");
getch();
return -1;
}
if(s == rear)
{
n = s->info;
front = rear = NULL;
free(s);
}
else
{
for(; (s->next)->next!=NULL;s=s->next);
n = rear->info;
free(rear);
}
s->next = NULL;
rear = s;
return n;
}


disp(queue *s)
{
clrscr();
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("\n\t\t%5d ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
getch();
}

f_disp(queue *s)
{
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
}

Queue Double (can insert through both end)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;

queue *rear, *front;

int main(void)
{
int ch, n;
clrscr();
while(1)
{
clrscr();
f_disp(front);
printf("\n1. Enqueue rear\n2. Enqueue Front\n3. Dequeue front\n4. Dequeue rear\n5. Display\n6. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: n = getinput();
enqueue_rear(rear,n);
break;
case 2: n = getinput();
enqueue_front(front, n);
break;
case 3: dequeue_front(front);
break;
case 4: dequeue_rear(front);
break;
case 5: disp(front);
break;
case 6: exit(0);
default: printf("Invalid choice.\nPlease enter 1 - 6\n");
getch();
}
}
}

int getinput()
{
int n;
printf("Enter the new info : ");
scanf("%d",&n);
return n;
}

enqueue_rear(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

enqueue_front(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = s;
front = temp;
if(s == NULL)
rear = temp;
}

dequeue_front(queue *s)
{
int n;
if(s!=NULL)
{
n = s->info;
front = s->next;
if(s->next == NULL)
rear = NULL;
free(s);
return n;
}
else
{ clrscr();
printf("\n\n\t\tQueue underflow...");
getch();
return -1;
}
getch();
}

dequeue_rear(queue *s)
{
queue *p;
int n;
if(s == NULL)
{
clrscr();
printf("\n\n\t\tQueue Underflow...");
getch();
return -1;
}
if(s == rear)
{
n = s->info;
front = rear = NULL;
free(s);
}
else
{
for(; (s->next)->next!=NULL;s=s->next);
n = rear->info;
free(rear);
}
s->next = NULL;
rear = s;
return n;
}


disp(queue *s)
{
clrscr();
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("\n\t\t%5d ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
getch();
}

f_disp(queue *s)
{
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
}

Queue (Dynamic implementation)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;
queue *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(rear, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

/* delete node from queue list */
dequeue(queue *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(queue *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(queue *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue (Dynamic implementation)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;
queue *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(rear, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

/* delete node from queue list */
dequeue(queue *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(queue *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(queue *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue (Static implementation)

/* static implementation of queue */
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

#define MAX 5

typedef struct
{
int info[MAX];
int front , rear;
} queue;

queue que;

main()
{
int n, ch;
clrscr();
que.front = 0;
que.rear = -1;
while(1)
{
clrscr();
f_disp(que);
puts("1. Enqueue\n2. Dequeue\n3. Display\4. Exit");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&que, n);
break;
case 2: n = dequeue(&que);
if(n!=-1)
printf("Dequeued %d", n);
else
printf("Queue underflow...");
getch();
break;
case 3: disp(que);
break;
case 4: exit(0);
default : printf("Invalid choice.");
}
}
}

enqueue(queue *q, int n)
{
if(q->rear == MAX)
{
puts("Queue overflow...");
getch();
return;
}
q->info[++q->rear] = n;
}

dequeue(queue *q)
{
int n;
if(q->fornt > q->rear)
{
puts("Queue underflow...");
getch();
return -1;
}

n = q->inf[q->front++];
return n;
}

disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("\n %d", q->info[i]);
getch();
}

f_disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("%d, ", q->info[i]);

}

Queue (Static implementation)

/* static implementation of queue */
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

#define MAX 5

typedef struct
{
int info[MAX];
int front , rear;
} queue;

queue que;

main()
{
int n, ch;
clrscr();
que.front = 0;
que.rear = -1;
while(1)
{
clrscr();
f_disp(que);
puts("1. Enqueue\n2. Dequeue\n3. Display\4. Exit");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&que, n);
break;
case 2: n = dequeue(&que);
if(n!=-1)
printf("Dequeued %d", n);
else
printf("Queue underflow...");
getch();
break;
case 3: disp(que);
break;
case 4: exit(0);
default : printf("Invalid choice.");
}
}
}

enqueue(queue *q, int n)
{
if(q->rear == MAX)
{
puts("Queue overflow...");
getch();
return;
}
q->info[++q->rear] = n;
}

dequeue(queue *q)
{
int n;
if(q->fornt > q->rear)
{
puts("Queue underflow...");
getch();
return -1;
}

n = q->inf[q->front++];
return n;
}

disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("\n %d", q->info[i]);
getch();
}

f_disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("%d, ", q->info[i]);

}

Wednesday, April 20, 2011

Evaluation of prefix expression

/* program on Evaluation of prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",prefix);
for(i=0;prefix[i]!='\0';i++)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalprefix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the prefix expression */

evalprefix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0;prefix[i]!='\0';i++);
for(i=i-1; i>=0; i--)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op1 = pop(stk);
op2 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of prefix expression

/* program on Evaluation of prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",prefix);
for(i=0;prefix[i]!='\0';i++)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalprefix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the prefix expression */

evalprefix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0;prefix[i]!='\0';i++);
for(i=i-1; i>=0; i--)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op1 = pop(stk);
op2 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of postfix expression

/* program on Evaluation of postfix expression */

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalpostfix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the postfix expression */

evalpostfix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0; postfix[i]!='\0'; i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op2 = pop(stk);
op1 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of postfix expression

/* program on Evaluation of postfix expression */

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalpostfix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the postfix expression */

evalpostfix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0; postfix[i]!='\0'; i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op2 = pop(stk);
op1 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Infix expression to prefix expression conversion

/* program on conversion of infix to prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopre();
printf("\nPrefix expression is : %s", prefix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* ---- get character type here wheather operator or operand ----- */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* --- get precedence of operator ------ */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}


/* convert infix to prefix expression ---- */
intopre()
{
int i,p, type, pr;
char ch, op;
for(i=0; infix[i]!='\0';i++);

for(i=i-1, p=0; i>=0;i--)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: prefix[p++] = ch;
break;
case RP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& prinfo))
prefix[p++] = pop(stk);
push(stk, ch);
break;
case LP: while(stk!=NULL && (op = pop(stk))!=')')
prefix[p++] = op;
break;
}
}

while(stk!=NULL)
prefix[p++] = pop(stk);
prefix[p] = '\0';
strrev(prefix);
}

Infix expression to prefix expression conversion

/* program on conversion of infix to prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopre();
printf("\nPrefix expression is : %s", prefix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* ---- get character type here wheather operator or operand ----- */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* --- get precedence of operator ------ */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}


/* convert infix to prefix expression ---- */
intopre()
{
int i,p, type, pr;
char ch, op;
for(i=0; infix[i]!='\0';i++);

for(i=i-1, p=0; i>=0;i--)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: prefix[p++] = ch;
break;
case RP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& prinfo))
prefix[p++] = pop(stk);
push(stk, ch);
break;
case LP: while(stk!=NULL && (op = pop(stk))!=')')
prefix[p++] = op;
break;
}
}

while(stk!=NULL)
prefix[p++] = pop(stk);
prefix[p] = '\0';
strrev(prefix);
}

Infix expression to postfix expression conversion

/* program on conversion of infix to postfix expression */

#include"stdio.h"
#include"stdlib.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopost();
printf("\nPostfix expression is : %s", postfix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* get character type form infix expression */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* get precedence of operator */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}

/* convert infix to postfix expression */
intopost()
{
int i,p, type, pr;
char ch, op;

for(i=0, p=0; infix[i]!='\0';i++)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: postfix[p++] = ch;
break;
case LP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& pr<=prec(stk->info))
postfix[p++] = pop(stk);
push(stk, ch);
break;
case RP: while(stk!=NULL && (op = pop(stk))!='(')
postfix[p++] = op;
break;
}
}

while(stk!=NULL)
postfix[p++] = pop(stk);
postfix[p] = '\0';
}

Infix expression to postfix expression conversion

/* program on conversion of infix to postfix expression */

#include"stdio.h"
#include"stdlib.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopost();
printf("\nPostfix expression is : %s", postfix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* get character type form infix expression */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* get precedence of operator */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}

/* convert infix to postfix expression */
intopost()
{
int i,p, type, pr;
char ch, op;

for(i=0, p=0; infix[i]!='\0';i++)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: postfix[p++] = ch;
break;
case LP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& pr<=prec(stk->info))
postfix[p++] = pop(stk);
push(stk, ch);
break;
case RP: while(stk!=NULL && (op = pop(stk))!='(')
postfix[p++] = op;
break;
}
}

while(stk!=NULL)
postfix[p++] = pop(stk);
postfix[p] = '\0';
}

Monday, April 18, 2011

Stack Dynamic implemetation by Sweta Mam

Stack (Dynamic implemetation) by Sweta Mam

/* Stack Dynamic implemetation by Sweta Mam */

#include"stdio.h"
#include"stdlib.h"
#include"conio.h"

struct stack
{
int info;
struct stack *next;
}*front;


int main(void)
{
int n, ch;
while(1)
{
clrscr();
puts("1. Push\n2. Pop\n3. Display\n4. Exit");
puts("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(front, n);
break;
case 2: n = pop(front);
if(n == -1)
{
printf("\nPopped %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);

default: puts("Invalid choice.");
getch();
}
}
return 0;
}


/* push function here */

push(struct stack *s, int n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
front = temp;
}

/* pop function here */

pop(struct stack *s)
{
int n;
if(s==NULL)
{
printf("Stack underflow.");
return -1;
}
n = s->info;
front = s->next;
free(s);
return n;
}

/* display function here */

disp(struct stack *s)
{
clrscr();
if(s!=NULL)
{
printf("\nStack are : \n");

while(s!=NULL)
{
printf("\n\t %d",s->info);
s = s->next;
}
getch();
}
else
{
printf("Stack is empty.");
getch();
}
}

Stack Dynamic implemetation by Sweta Mam

Stack (Dynamic implemetation) by Sweta Mam

/* Stack Dynamic implemetation by Sweta Mam */

#include"stdio.h"
#include"stdlib.h"
#include"conio.h"

struct stack
{
int info;
struct stack *next;
}*front;


int main(void)
{
int n, ch;
while(1)
{
clrscr();
puts("1. Push\n2. Pop\n3. Display\n4. Exit");
puts("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(front, n);
break;
case 2: n = pop(front);
if(n == -1)
{
printf("\nPopped %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);

default: puts("Invalid choice.");
getch();
}
}
return 0;
}


/* push function here */

push(struct stack *s, int n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
front = temp;
}

/* pop function here */

pop(struct stack *s)
{
int n;
if(s==NULL)
{
printf("Stack underflow.");
return -1;
}
n = s->info;
front = s->next;
free(s);
return n;
}

/* display function here */

disp(struct stack *s)
{
clrscr();
if(s!=NULL)
{
printf("\nStack are : \n");

while(s!=NULL)
{
printf("\n\t %d",s->info);
s = s->next;
}
getch();
}
else
{
printf("Stack is empty.");
getch();
}
}

Stack static implementation program by Sweta Mam

/* Stack static implementation program by Sweta Mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

#define MAX 5

struct stack
{
int info[MAX];
int top;
} stk;

int main(void)
{
int n, ch;
stk.top = -1;
clrscr();
while(1)
{
clrscr();
puts("\n\n1. Push\n2. Pop\n3. Display\n4. Exit.");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(&stk, n);
break;
case 2: n = pop(&stk);
if(n!=-1)
{
printf("Popped : %d",n);
getch();
}
break;
case 3: disp(&stk);
break;
case 4: exit(0);
default: puts("\nInvalid Input.\nPlease enter 1-4.");
getch();
}
}
}
push(struct stack *s, int n)
{
if(s->top == MAX-1)
{
puts("Stack overflow.");
getch();
return;
}
s->info[++s->top] = n;
}

pop(struct stack *s)
{
int n;
if(s->top == -1)
{
printf("Stack underflow.");
getch();
return;
}
n = s->info[s->top--];
return n;
}

disp(struct stack *s)
{
int i;
clrscr();
printf("\nStacks are : \n");
if(s->top!= -1)
{
for(i=s->top; i>=0; i--)
printf("\n\t%d",s->info[i]);
}
else
printf("No stacks created yet.");
getch();
}

Stack static implementation program by Sweta Mam

/* Stack static implementation program by Sweta Mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

#define MAX 5

struct stack
{
int info[MAX];
int top;
} stk;

int main(void)
{
int n, ch;
stk.top = -1;
clrscr();
while(1)
{
clrscr();
puts("\n\n1. Push\n2. Pop\n3. Display\n4. Exit.");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(&stk, n);
break;
case 2: n = pop(&stk);
if(n!=-1)
{
printf("Popped : %d",n);
getch();
}
break;
case 3: disp(&stk);
break;
case 4: exit(0);
default: puts("\nInvalid Input.\nPlease enter 1-4.");
getch();
}
}
}
push(struct stack *s, int n)
{
if(s->top == MAX-1)
{
puts("Stack overflow.");
getch();
return;
}
s->info[++s->top] = n;
}

pop(struct stack *s)
{
int n;
if(s->top == -1)
{
printf("Stack underflow.");
getch();
return;
}
n = s->info[s->top--];
return n;
}

disp(struct stack *s)
{
int i;
clrscr();
printf("\nStacks are : \n");
if(s->top!= -1)
{
for(i=s->top; i>=0; i--)
printf("\n\t%d",s->info[i]);
}
else
printf("No stacks created yet.");
getch();
}

Circular Doubly link list program by Sweta mam

/* Circular Doubly link list program by sweta mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

struct dclist
{
int info;
struct dclist *next, *prev;
} *st;

void main()
{
int ch;
clrscr();
while(1)
{
clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete\n4. Insert After");
puts("5. Insert Before\n6. Display Forward\n7. Display Backward");
puts("8. Save list to disk.\n9. Read from disk\n10. Exit.");
printf("Enter your choice (1-9): ");
scanf("%d",&ch);
switch(ch)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_aft(st);break;
case 5: ins_bef(st); break;
case 6: disp_frd(st);break;
case 7: disp_bck(st); break;
case 8: save_list(st); break;
case 9: read_list(st);break;
case 10: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 10\n");
}
}
}

/* Add function here */
add(struct dclist *s)
{
struct dclist *temp;
int n;
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter info : ");
scanf("%d",&n);
temp->info = n;
if(s==NULL)
{
st = temp;
s = st;
}
else
for(;s->next!=st;s=s->next);
temp->prev = s;
temp->next = st;
st->prev = temp;
s->next = temp;
}

/* modify function here */
mod(struct dclist *s)
{
int n;
printf("Modify? ");
scanf("%d",&n);
while(st!=s->next)
{
if(s->info==n)
{
printf("Enter new info: ");
scanf("%d",&n);
s->info = n;
return;
}
s = s->next;
}
printf("\n%d not found.",n);
getch();
}

/* Delete function here */
del(struct dclist *s)
{
int n;
if(s!=NULL)
{
printf("Delete?");
scanf("%d",&n);
do
{
if(s->info==n)
{
if(s==st&&st->next==st)
st = NULL;
else
{
s->next->prev = s->prev;
s->prev->next = s->next;
if(s==st)
st = st->next;
}
free(s);
return;
}

s = s->next;

}while(s!=st);
printf("%d not found.",n);
}
getch();
}

/* to edited form and modify functins insert before function here */
ins_bef(struct dclist *s)
{
int n;
struct dclist *temp;

if(s!=NULL)
{
printf("Insert before? ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
temp->prev = s->prev;
temp->prev->next = temp;
s->prev = temp;

if(s==st)
st = temp;
return;
}
s = s->next;

}while(s!=st);
}

printf("%d not found.",n);
getch();
}

/* insert after */
ins_aft(struct dclist *s)
{
int n;
struct dclist *temp;
if(s!=NULL)
{
printf("Insert after : ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->prev = s;
temp->next = s->next;
temp->next->prev = temp;
s->next = temp;
return;
}
s = s->next;
}while(s!=st);
printf("%d not found.");
}
else printf("\nList not created yet.");
getch();
}

/* display function here */

disp_frd(struct dclist *s)
{
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->next;
}while(s!=st);
getch();
}
else
{ printf("List not create yet.");
getch();
}
}

disp_bck(struct dclist *s)
{
for(;st!=s;s=s->next);
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->prev;
}while(s!=st);
getch();
}
else
printf("List is empty.");
}


disp(struct dclist *s)
{
printf("List are : \n");
printf("\n|\tAddress\t|\tInfo\t|\tNext\t|");
if(s==NULL)
{
printf("\n\n\tNo list created yet.");
getch();
return;
}
while(s!=NULL)
{

printf("\n|\t%5u\t|\t%d\t|\t%5u\t|",s,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

f_disp(struct dclist *s)
{
if(s!=NULL)
{
printf("List is : ");
do
{
printf("%d, ",s->info);
s = s->next;
}while(s!=st);
}
else printf("No list yet.");

}

save_list(struct dclist *s)
{
FILE *fp;
int n;
fp = fopen("dclist.txt", "w");
clrscr();
if(s==NULL)
{
printf("\nNo list is to save.");
fclose(fp);
return;
}
do
{
n = s->info;
fprintf(fp,"%d\n",n);
s = s->next;
}while(st!=s->next);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dclist *s)
{
FILE *fp;
struct dclist *temp;
int n;
fp = fopen("dclist.txt","r");
clrscr();
do
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
fscanf(fp,"%d\n",&n);
temp->info = n;
temp->next = s;
if(s==NULL) st = s = temp;
else
{
for(;st!=s->next;s=s->next);
s->next = temp;
}
}while(!feof(fp));
for(;st!=s->next; s=s->next);
s->next = st;
printf("\nList imported sucessfull.");
getch();
}

Circular Doubly link list program by Sweta mam

/* Circular Doubly link list program by sweta mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

struct dclist
{
int info;
struct dclist *next, *prev;
} *st;

void main()
{
int ch;
clrscr();
while(1)
{
clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete\n4. Insert After");
puts("5. Insert Before\n6. Display Forward\n7. Display Backward");
puts("8. Save list to disk.\n9. Read from disk\n10. Exit.");
printf("Enter your choice (1-9): ");
scanf("%d",&ch);
switch(ch)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_aft(st);break;
case 5: ins_bef(st); break;
case 6: disp_frd(st);break;
case 7: disp_bck(st); break;
case 8: save_list(st); break;
case 9: read_list(st);break;
case 10: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 10\n");
}
}
}

/* Add function here */
add(struct dclist *s)
{
struct dclist *temp;
int n;
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter info : ");
scanf("%d",&n);
temp->info = n;
if(s==NULL)
{
st = temp;
s = st;
}
else
for(;s->next!=st;s=s->next);
temp->prev = s;
temp->next = st;
st->prev = temp;
s->next = temp;
}

/* modify function here */
mod(struct dclist *s)
{
int n;
printf("Modify? ");
scanf("%d",&n);
while(st!=s->next)
{
if(s->info==n)
{
printf("Enter new info: ");
scanf("%d",&n);
s->info = n;
return;
}
s = s->next;
}
printf("\n%d not found.",n);
getch();
}

/* Delete function here */
del(struct dclist *s)
{
int n;
if(s!=NULL)
{
printf("Delete?");
scanf("%d",&n);
do
{
if(s->info==n)
{
if(s==st&&st->next==st)
st = NULL;
else
{
s->next->prev = s->prev;
s->prev->next = s->next;
if(s==st)
st = st->next;
}
free(s);
return;
}

s = s->next;

}while(s!=st);
printf("%d not found.",n);
}
getch();
}

/* to edited form and modify functins insert before function here */
ins_bef(struct dclist *s)
{
int n;
struct dclist *temp;

if(s!=NULL)
{
printf("Insert before? ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
temp->prev = s->prev;
temp->prev->next = temp;
s->prev = temp;

if(s==st)
st = temp;
return;
}
s = s->next;

}while(s!=st);
}

printf("%d not found.",n);
getch();
}

/* insert after */
ins_aft(struct dclist *s)
{
int n;
struct dclist *temp;
if(s!=NULL)
{
printf("Insert after : ");
scanf("%d",&n);
do
{
if(s->info == n)
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->prev = s;
temp->next = s->next;
temp->next->prev = temp;
s->next = temp;
return;
}
s = s->next;
}while(s!=st);
printf("%d not found.");
}
else printf("\nList not created yet.");
getch();
}

/* display function here */

disp_frd(struct dclist *s)
{
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->next;
}while(s!=st);
getch();
}
else
{ printf("List not create yet.");
getch();
}
}

disp_bck(struct dclist *s)
{
for(;st!=s;s=s->next);
if(s!=NULL)
{
do
{
printf("\n\t%d",s->info);
s = s->prev;
}while(s!=st);
getch();
}
else
printf("List is empty.");
}


disp(struct dclist *s)
{
printf("List are : \n");
printf("\n|\tAddress\t|\tInfo\t|\tNext\t|");
if(s==NULL)
{
printf("\n\n\tNo list created yet.");
getch();
return;
}
while(s!=NULL)
{

printf("\n|\t%5u\t|\t%d\t|\t%5u\t|",s,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

f_disp(struct dclist *s)
{
if(s!=NULL)
{
printf("List is : ");
do
{
printf("%d, ",s->info);
s = s->next;
}while(s!=st);
}
else printf("No list yet.");

}

save_list(struct dclist *s)
{
FILE *fp;
int n;
fp = fopen("dclist.txt", "w");
clrscr();
if(s==NULL)
{
printf("\nNo list is to save.");
fclose(fp);
return;
}
do
{
n = s->info;
fprintf(fp,"%d\n",n);
s = s->next;
}while(st!=s->next);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dclist *s)
{
FILE *fp;
struct dclist *temp;
int n;
fp = fopen("dclist.txt","r");
clrscr();
do
{
temp = (struct dclist *)malloc(sizeof(struct dclist));
fscanf(fp,"%d\n",&n);
temp->info = n;
temp->next = s;
if(s==NULL) st = s = temp;
else
{
for(;st!=s->next;s=s->next);
s->next = temp;
}
}while(!feof(fp));
for(;st!=s->next; s=s->next);
s->next = st;
printf("\nList imported sucessfull.");
getch();
}

Doubly Link List (Menu based program) - Sweta Mam

/* Doubly link list by sweta mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

struct dlist
{
int info;
struct dlist *next, *prev;
} *st;

void main()
{
int ch;
clrscr();
while(1)
{ clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete\n4. Insert After");
puts("5. Insert Before\n6. Display");
puts("7. Save list to disk.\n8. Read from disk\n9. Exit.");
printf("Enter your choice (1-9): ");
scanf("%d",&ch);
switch(ch)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_aft(st);break;
case 5: ins_bef(st); break;
case 6: disp(st);break;
case 7: save_list(st); break;
case 8: read_list(st);break;
case 9: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 9\n");
}
}
}

/* Add function here */
add(struct dlist *s)
{
struct dlist *temp;
int n;
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter info : ");
scanf("%d",&n);
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
temp->prev = s;
}

/* modify function here */
mod(struct dlist *s)
{
int n;
printf("Modify? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter new info: ");
scanf("%d",&n);
s->info = n;
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* Delete function here */
del(struct dlist *s)
{
int n;
printf("Delete?");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
s->prev->next = s->next;
if(s->next!=NULL)
s->next->prev = s->prev;

free(s);
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert before function here */
ins_bef(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert before? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->prev = s->prev;
temp->next = s;
s->prev = temp;
if(s==st)
st = temp;
else
temp->prev->next = temp;
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert after */
ins_aft(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->next = s->next;
temp->prev = s;

if(s->next!=NULL)
{
s->next->prev = temp;
s->next = temp;
return;
}
else
{
temp->next = NULL;
s->next = temp;
return;
}
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* display function here */
disp(struct dlist *s)
{
clrscr();
printf("List are : \n");
printf("\n|\tPrev\t|\tInfo\t|\tNext\t|");
if(s==NULL)
{
printf("\n\n\tNo list created yet.");
getch();
return;
}
while(s!=NULL)
{

printf("\n|\t%5u\t|\t%d\t|\t%5u\t|",s->prev,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

f_disp(struct dlist *s)
{ if(s!=NULL)
printf("The list is : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s=s->next;
}
return;
}

save_list(struct dlist *s)
{
FILE *fp;
int n;
fp = fopen("dlist.txt", "a+");
clrscr();
if(s==NULL)
{
printf("\nNo list is to save.");
fclose(fp);
return;
}
do
{
n = s->info;
fprintf(fp,"%d\n",n);
s = s->next;
}while(s!=NULL);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dlist *s)
{
FILE *fp;
struct dlist *temp;
int n;
if((fp = fopen("dlist.txt","r"))==NULL)
{
printf("File cannot open.");
getch();
return;
}

clrscr();
do
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
fscanf(fp,"%d\n",&n);
printf("\nRead form file is: %d",n);
temp->info = n;
temp->next = NULL;
if(s==NULL) st = s = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}while(!feof(fp));
printf("\nList imported sucessfull.");
getch();
}

Doubly Link List (Menu based program) - Sweta Mam

/* Doubly link list by sweta mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

struct dlist
{
int info;
struct dlist *next, *prev;
} *st;

void main()
{
int ch;
clrscr();
while(1)
{ clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete\n4. Insert After");
puts("5. Insert Before\n6. Display");
puts("7. Save list to disk.\n8. Read from disk\n9. Exit.");
printf("Enter your choice (1-9): ");
scanf("%d",&ch);
switch(ch)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_aft(st);break;
case 5: ins_bef(st); break;
case 6: disp(st);break;
case 7: save_list(st); break;
case 8: read_list(st);break;
case 9: exit(0);
default: printf("Invalid option \n\tPlease enter 1 - 9\n");
}
}
}

/* Add function here */
add(struct dlist *s)
{
struct dlist *temp;
int n;
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter info : ");
scanf("%d",&n);
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
temp->prev = s;
}

/* modify function here */
mod(struct dlist *s)
{
int n;
printf("Modify? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter new info: ");
scanf("%d",&n);
s->info = n;
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* Delete function here */
del(struct dlist *s)
{
int n;
printf("Delete?");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
s->prev->next = s->next;
if(s->next!=NULL)
s->next->prev = s->prev;

free(s);
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert before function here */
ins_bef(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert before? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info : ");
scanf("%d",&n);
temp->info = n;
temp->prev = s->prev;
temp->next = s;
s->prev = temp;
if(s==st)
st = temp;
else
temp->prev->next = temp;
return;
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* insert after */
ins_aft(struct dlist *s)
{
int n;
struct dlist *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info == n)
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
printf("Enter new info: ");
scanf("%d",&n);
temp->info = n;
temp->next = s->next;
temp->prev = s;

if(s->next!=NULL)
{
s->next->prev = temp;
s->next = temp;
return;
}
else
{
temp->next = NULL;
s->next = temp;
return;
}
}
s = s->next;
}
printf("%d not found.",n);
getch();
}

/* display function here */
disp(struct dlist *s)
{
clrscr();
printf("List are : \n");
printf("\n|\tPrev\t|\tInfo\t|\tNext\t|");
if(s==NULL)
{
printf("\n\n\tNo list created yet.");
getch();
return;
}
while(s!=NULL)
{

printf("\n|\t%5u\t|\t%d\t|\t%5u\t|",s->prev,s->info,s->next);
s = s->next;
}
getch();return;
}


/*f_disp function here */

f_disp(struct dlist *s)
{ if(s!=NULL)
printf("The list is : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s=s->next;
}
return;
}

save_list(struct dlist *s)
{
FILE *fp;
int n;
fp = fopen("dlist.txt", "a+");
clrscr();
if(s==NULL)
{
printf("\nNo list is to save.");
fclose(fp);
return;
}
do
{
n = s->info;
fprintf(fp,"%d\n",n);
s = s->next;
}while(s!=NULL);
printf("\nList imported.");
fclose(fp);
getch();
}


/* read list function here */
read_list(struct dlist *s)
{
FILE *fp;
struct dlist *temp;
int n;
if((fp = fopen("dlist.txt","r"))==NULL)
{
printf("File cannot open.");
getch();
return;
}

clrscr();
do
{
temp = (struct dlist *)malloc(sizeof(struct dlist));
fscanf(fp,"%d\n",&n);
printf("\nRead form file is: %d",n);
temp->info = n;
temp->next = NULL;
if(s==NULL) st = s = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}while(!feof(fp));
printf("\nList imported sucessfull.");
getch();
}

Saturday, April 9, 2011

Link List Program(Menu Based) By-Sweta Mam

#include"stdio.h"
#include"stdlib.h"

struct list
{
int info;
struct list *next;
}*st;

FILE *fp;
/* ----------------------- main function here --------------------------------*/
int main()
{
int n;
while(1)
{
clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete");
puts("4. Insert Before\n5. Insert After");
puts("6. Display\n7. Save the list");
puts("8. Read form disk\n9. Exit");
fflush(stdin);
printf("\nEnter your choice : ");
fflush(stdin);
scanf("%d",&n);
switch(n)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_bef(st); break;
case 5: ins_aft(st); break;
case 6: disp(st); break;
case 7: save_list(st); break;
case 8: read_list(st); break;
case 9: del_all(st); exit(0);break;
default: puts("\nInvalid Input\nPlease enter 1-9");
getch();
}

}
}
del_all(struct list *s)
{
struct list *t;
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}
}


/*------------------------Add function declaration here -----------------------------*/
add(struct list *s)
{
struct list *temp;
int n;
printf("Enter the info: ");
scanf("%d",&n);

temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}


/* --------------------------modification function declaration here ------------------------*/

mod(struct list *s)
{
int n;
printf("Modify ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info: ");
scanf("%d",&n);
s->info = n;
return;
}
s=s->next;
}
printf("%d not found in list.",n);
getch();
}


/*--------------------Delete function from here ----------------*/

del(struct list *s)
{
int n;
struct list *p=NULL;
printf("Delete ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
p->next = s->next;
free(s);
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.");
getch();
}

/*--------------Insert before function from here-----------*/
ins_bef(struct list *s)
{
int n;
struct list *p=NULL, *temp;
printf("Insert before : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
temp = (struct list *)malloc(sizeof(struct list));
printf("\nEnter the new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
if(s==st)
st = temp;
else
p->next = temp;
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.",n);
getch();
}

/*----------------------Insert After function from here--------------*/
ins_aft(struct list *s)
{
int n;
struct list *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info : ");
scanf("%d",&n);
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = s->next;
s->next = temp;
return;
}
s=s->next;

}
printf("%d not found in the list.",n);
getch();
}



/*--------------Display function from here --------------*/
disp(struct list *s)
{
int n =1,c=0;
clrscr();
printf("\t|---------------------------------------|");
printf("\n\t|Address|\t| Info |\t| Next |\n");
printf("\t|---------------------------------------|\n");
while(s!=NULL)
{
printf("\t|%6u\t|\t|%6d |\t|%6u |\n",s,s->info,s->next);
s=s->next;
c=1;
n++;
}
printf("\t|---------------------------------------|\n");
if(c!=1)printf("\tNo list is created yet.");
getch();
}

/*-----------f_disp-----------------*/
f_disp(struct list *s)
{ if(s!=NULL)
printf("The list is : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s=s->next;
}
return;
}



save_list(struct list *s)
{
int n;
fp = fopen("list.txt", "w");
clrscr();
for(;s!=NULL;s=s->next)
{
n = s->info;
fflush(stdin);
fprintf(fp," %d \n",n);
}
fclose(fp);
getch();
}


/* read list function here */
read_list(struct list *s)
{
struct list *temp;
int n,i;/*
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}*/
fp = fopen("list.txt","r");
clrscr();
do
{

fscanf(fp," %d \n",&n);
printf("\nInfo imported : %d",n);
getch();
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
s = st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}

}while(!feof(fp));
printf("List imported sucessfull.");
getch();
}

Link List Program(Menu Based) By-Sweta Mam

#include"stdio.h"
#include"stdlib.h"

struct list
{
int info;
struct list *next;
}*st;

FILE *fp;
/* ----------------------- main function here --------------------------------*/
int main()
{
int n;
while(1)
{
clrscr();
f_disp(st);
puts("\n\n1. Add\n2. Modify\n3. Delete");
puts("4. Insert Before\n5. Insert After");
puts("6. Display\n7. Save the list");
puts("8. Read form disk\n9. Exit");
fflush(stdin);
printf("\nEnter your choice : ");
fflush(stdin);
scanf("%d",&n);
switch(n)
{
case 1: add(st); break;
case 2: mod(st); break;
case 3: del(st); break;
case 4: ins_bef(st); break;
case 5: ins_aft(st); break;
case 6: disp(st); break;
case 7: save_list(st); break;
case 8: read_list(st); break;
case 9: del_all(st); exit(0);break;
default: puts("\nInvalid Input\nPlease enter 1-9");
getch();
}

}
}
del_all(struct list *s)
{
struct list *t;
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}
}


/*------------------------Add function declaration here -----------------------------*/
add(struct list *s)
{
struct list *temp;
int n;
printf("Enter the info: ");
scanf("%d",&n);

temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}
}


/* --------------------------modification function declaration here ------------------------*/

mod(struct list *s)
{
int n;
printf("Modify ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info: ");
scanf("%d",&n);
s->info = n;
return;
}
s=s->next;
}
printf("%d not found in list.",n);
getch();
}


/*--------------------Delete function from here ----------------*/

del(struct list *s)
{
int n;
struct list *p=NULL;
printf("Delete ? ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
if(s==st)
st = s->next;
else
p->next = s->next;
free(s);
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.");
getch();
}

/*--------------Insert before function from here-----------*/
ins_bef(struct list *s)
{
int n;
struct list *p=NULL, *temp;
printf("Insert before : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
temp = (struct list *)malloc(sizeof(struct list));
printf("\nEnter the new info : ");
scanf("%d",&n);
temp->info = n;
temp->next = s;
if(s==st)
st = temp;
else
p->next = temp;
return;
}
p=s;
s=s->next;
}
printf("%d not found in the list.",n);
getch();
}

/*----------------------Insert After function from here--------------*/
ins_aft(struct list *s)
{
int n;
struct list *temp;
printf("Insert after : ");
scanf("%d",&n);
while(s!=NULL)
{
if(s->info==n)
{
printf("Enter info : ");
scanf("%d",&n);
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = s->next;
s->next = temp;
return;
}
s=s->next;

}
printf("%d not found in the list.",n);
getch();
}



/*--------------Display function from here --------------*/
disp(struct list *s)
{
int n =1,c=0;
clrscr();
printf("\t|---------------------------------------|");
printf("\n\t|Address|\t| Info |\t| Next |\n");
printf("\t|---------------------------------------|\n");
while(s!=NULL)
{
printf("\t|%6u\t|\t|%6d |\t|%6u |\n",s,s->info,s->next);
s=s->next;
c=1;
n++;
}
printf("\t|---------------------------------------|\n");
if(c!=1)printf("\tNo list is created yet.");
getch();
}

/*-----------f_disp-----------------*/
f_disp(struct list *s)
{ if(s!=NULL)
printf("The list is : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s=s->next;
}
return;
}



save_list(struct list *s)
{
int n;
fp = fopen("list.txt", "w");
clrscr();
for(;s!=NULL;s=s->next)
{
n = s->info;
fflush(stdin);
fprintf(fp," %d \n",n);
}
fclose(fp);
getch();
}


/* read list function here */
read_list(struct list *s)
{
struct list *temp;
int n,i;/*
for(t=s;t!=NULL;t=s)
{
s = s->next;
free(s);
}*/
fp = fopen("list.txt","r");
clrscr();
do
{

fscanf(fp," %d \n",&n);
printf("\nInfo imported : %d",n);
getch();
temp = (struct list *)malloc(sizeof(struct list));
temp->info = n;
temp->next = NULL;
if(s==NULL)
s = st = temp;
else
{
for(;s->next!=NULL;s=s->next);
s->next = temp;
}

}while(!feof(fp));
printf("List imported sucessfull.");
getch();
}

Friday, April 1, 2011

IGNOU BCA Solved Assignment All Semesters 2011


Every course of each semester of IGNOU BCA consists of 2 parts - the first is semester wise examination and the other is the IGNOU Assignments. Both the parts are compulsory as well as important for the students to pass out successfully their semesters. It is very important for the students to submit the solved assignments to get promoted further. Therefore, I have posted some links to the solved assignments below.

IGNOU BCA Solved Assignment All Semesters 2011


Every course of each semester of IGNOU BCA consists of 2 parts - the first is semester wise examination and the other is the IGNOU Assignments. Both the parts are compulsory as well as important for the students to pass out successfully their semesters. It is very important for the students to submit the solved assignments to get promoted further. Therefore, I have posted some links to the solved assignments below.

Solved Assignment CS-60


There are five questions in this assignment. Answer all the questions.

Question 1:  (i) Find the complex conjugate of (3+5i)/(1+2i)

Here
z = (3 + 5i)(1 + 2i)
= 3 + 6i + 5i + 10(i x i)
= 3 + 11i – 10
= - 7 + 11i
Then Complex conjugate of – 7 + 11i
= - 7 – 11i

(ii) Differentiate (sin x)x w.r.t. x.


(iii) Find all the seventh roots of (3+4i).                                          (5 Marks)          


Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites