Sunday, 6 July 2014

Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Push And Pop characters In Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Insert And Delete Data In Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Displaying the linked list elements

/*
Function to display the student with highest CGPA in a linked list containing student data.
 Use the following node structure for your linked list.
*/
struct node{
int stNo;
float CGPA;
struct node *next;
};

typedef struct node *NODEPTR;
void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist;
/*current node*/
if(p == NULL){
printf(“no node/data is available in the list\n”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %d\n”, maxstNo);
printf(“The student’s CGPA: %d\n”, maxCGPA);
}

C Program For insert , delete ,display a new item in the Linked List

#include<stdio.h>
#include<conio.h>
/*
you can insert a new item to the end of the Linked List, delete the last
item from the linked list and display all the list items in the linked list. Note that the Linked List
contains integer items.      */

/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}

/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/

int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}

/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/

void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}

Linear linked list structure In C Programming

#include<stdio.h>
#include<conio.h>

/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}

/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/

int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}

/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/

void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}

Wednesday, 11 June 2014

stack implementation using Linked List In C Programing

#include<stdio.h>
#include<conio.h>
void push_stack ();
void pop_stack ();
void display_stack ();

typedef struct node
{
int data_stack ;
struct node *link;
}d;
d*top=NULL;


main()
{
int d;
printf("\tMENU\n1.PUSH_STACK \n2.POP_STACK \n3.DISPLAY_STACK \n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&d);
switch(d)
{
case 1:
push_stack ();
break;
case 2:
pop_stack ();
break;
case 3:
display_stack ();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(d!=4);
}


void push_stack ()
{
int item;
d *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(d*)malloc(sizeof(d));
temp->data_stack =item;
temp->link=top;
top=temp;
}

void pop_stack ()
{
d *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data_stack );
top=top->link;

free(temp);
}
}

void display_stack ()
{
d *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save->data_stack );
save=save->link;
}
printf("\nTopmost element = %d\n",top->data_stack );
}
}