Implementation of Reversal of a Singly Linked list
of student marks
1.Linked list is a data structure consisting of
nodes which together represents a sequence
2.Each node in the sequence consists of a data and
the link to the next node in the sequence
3.This structure allows for efficient insertion or
removal of the elements from any position in the sequence.
IN THIS MODULE :
Implementation of Reversal of a Singly Linked list
of student marks
1)To store the marks of the student of any number of
subjects using dynamic allocation i.e. Linked lists and to display the marks of
the student.
2)To Reversal the Student marks and display the
marks of the student
1.2 Stack ADT using
singly linked list:
1. The Principle of Stack is Last-in First-out.
2. Using this principle we use to store the
names of the Students using Dynamic memory allocation i.e. Linked List..
3. The name of the student we enter at the last stage will be displayed first and
the process continuestill the end of the stack.
To
Implement Stack ADT using singly linked list, for student names. These are the
following steps.
1. Prepare
a list of student names .
2. Prepare
a routine for inserting any one student’s name into the list
3. Construct
a linked list of strings using the
insert routine
4. Implement
the Push() and Pop() routines
5. Test
the above coded routines for stack
6. First we need
to have required student names on piece of paper and then
we have to enter the student names into the stack. If stack is full
we have to make it empty and then insert . Suppose if we entered
wrong name, we have to delete from stack and reinsert into stack. Later we have
to display the names and also the top most name in stack. In stack push means
insert and pop means to delete.The output can be obtained as above figure(1.2).
1.3 Reversal of contents of stack:
1)To store the address of the student
using stack and print them.
2)To Reversal the address of the student
and print theM.
Generally
if we use Linked Lists , we give the
Input as :
implementation
of ‘Reversal of contents of a stack’, for student addresses can be obtained by
performing the below steps.
1. Prepare
a list of student addresses
2. Prepare
a routine for inserting “student address” into the stack (array-based)
3. Construct
a stack of addresses by using Push() operation
4. Prepare
an algorithm for reversal of stack contents
5. Write
the code implementing the algorithm
6. Test
the above code, by giving various addresses
1.4Towers of Hanoi problem:
1.It
is a mathematical game or puzzle.
2.It
consists of 3 rods and the number of discs of different sizes which can slide
onto the any rod.
3.The Objective is to transfer the entire tower to one
of the other pegs. However you can only move one disk at a time and you can
never stack a larger disk onto a smaller disk. Try to solve it in fewest
possible moves.
Implementation
of Towers of Hanoi problem can be performed by the following ways:
1.An
Iterative Solution.
2.A
Recursive Solution.
2.ALGORITHMS FOR THE
GIVEN MODULES:
2.1 Reversal of singly linked list:
Struct
student
{
Int marks;
Struct student *next;
}
void main()
{
struct student *p;
p=NULL;
p=create(p);
insend(p);
reverse(p);
}
2.2 Implementation of the Stack ADT
using Single Linked List to Store the Names of the Students:
Struct stack
{
Char
name[20];
Struct
stack *next;
}
void main()
{
struct stack *p;
p=NULL;
p=create(p);
insend(p);
reverse(p);
}
2.3 Implementation Reversal of content of
stack for Student Address:
Struct stack
{
Int
marks;
Struct
stack *next;
}
void main()
{
struct stack *p;
p=NULL;
p=create(p);
insend(p);
reverse(p);
}
2.4 TOWERS
OF HANOI:
2.4.1.ITERATIVE METHOD:
int hanoi(int n,char a, char b,
char c)
{
while(n!=0)
{
hanoi(n-1,a,c,b);
printf("Moving disk %d from %c tower
to %c tower\n",n,a,c);
hanoi(n-1,b,a,c);
break;
}
return 0;
}
2.4.2RECURSIVE METHOD:
void towers(int num, char
frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n
Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to
peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
3.CODE FOR MODULES:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int hanoi(int,char,char,char);
/* ******* MODULE 1******* */
struct std
{
int
n;
struct
std *next;
}*curr,*last,*temp;
struct std *create(struct std *x)
{
if(x==NULL)
{
x=(struct
std*)malloc(sizeof(struct std));
printf("\nEnter
the First Subject Marks");
scanf("%d",&x->n);
x->next=NULL;
return
x;
}
else
{
printf("\n
The Node Already Created\n");
return
x;
}
}
void display(struct std *x)
{
curr=x;
while(curr!=NULL)
{
printf("\n%d-->",curr->n);
curr=curr->next;
}
}
void insend(struct std *x)
{
curr=x;
while(curr->next!=NULL)
{
curr=curr->next;
}
temp=(struct
std *)malloc(sizeof(struct std));
printf("\nEnter
the Subject Marks:");
scanf("%d",&temp->n);
temp->next=NULL;
curr->next=temp;
}
void reverse(struct std *x)
{
curr=x;
while(curr->next!=NULL)
{
curr=curr->next;
}
printf("\n%d-->",curr->n);
last=curr;
do
{
curr=x;
while(curr->next!=last)
{
curr=curr->next;
}
last=curr;
printf("\n%d-->",curr->n);
}while(last!=x);
}
/* ******** MODULE 2 ********* */
struct node
{
char
strele[30];
struct
node *next;
}*t,*p;
struct node *top = NULL;
void push(char *strp)
{
t
= (struct node*)malloc(sizeof(struct node));
t->next=NULL;
strcpy(t->strele,strp);
if(top==NULL)
{
top
= t;
}
else
{
t->next
= top;
top
= t;
}
}
char *pop()
{
if(top==NULL)
{
printf("\n************WARNING
STACK IS EMPTY**********");
return
NULL;
}
else
{
t
= top;
top
= top->next;
return
t->strele;
}
}
void display2()
{
t
= top;
while(t!=NULL)
{
printf("\n%s",t->strele);
t=t->next;
}
}
/* ******** MODULE 3 ******** */
struct Node
{
char
strele[30];
struct
Node *next;
struct
Node *pre;
}*w,*e;
struct Node *top1 = NULL;
void push1(char *strp)
{
w
= (struct Node*)malloc(sizeof(struct Node));
w->next=NULL;
w->pre
= NULL;
strcpy(w->strele,strp);
if(top1==NULL)
{
top1
= w;
}
else
{
top1->pre
= w;
w->next
= top1;
top1
= w;
}
}
char *pop1()
{
if(top1==NULL)
{
printf("\n************WARNING
STACK IS EMPTY**********");
return
NULL;
}
else
{
w
= top1;
top1
= top1->next;
top1->pre
= NULL;
return
w->strele;
}
}
void display3()
{
w
= top1;
while(w!=NULL)
{
printf("\n%s",w->strele);
w=w->next;
}
}
void rdisplay3()
{
w
= top1;
while(w->next!=NULL)
w=w->next;
while(w!=NULL)
{
printf("\n%s",w->strele);
w
= w->pre;
}
}
/* ********* TOWERS OF HANOI
********* */
/* ******** ITERATIVE METHOD
********* */
int hanoi(int n,char a, char b,
char c)
{
while(n!=0)
{
hanoi(n-1,a,c,b);
printf("Moving disk %d from %c tower
to %c tower\n",n,a,c);
hanoi(n-1,b,a,c);
break;
}
return 0;
}
/* ********* RECURSIVE METHOD
********* */
void towers(int num, char
frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n
Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to
peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
void main()
{
int
m,opt,i,n,ch,num;
char
str[30];
struct
std *s;
s=NULL;
clrscr();
do
{
printf("\n********************MENU*****************\n");
printf("\nEnter
one of the Following Options:\n");
printf("\n1.Reversal
Of Student Marks\n2.Displaying the Student Names using Stack\n3.Display of
Reversal of Stack Content containing Student Address\n4.Towers of
Hanoi\n5.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case
1:
s=create(s);
do
{
printf("\n************** REVERSAL OF STUDENT MARKS
************** \n");
printf("\nEnter
one of the Following Options");
printf("\n1.Insert\n2.Display\n3.Reverse
of a Linked
List\n4.Exit\n");
scanf("%d",&opt);
switch(opt)
{
case
1:
insend(s);
break;
case
2:
display(s);
break;
case
3:
reverse(s);
break;
case
4:
break;
default:
printf("\nWrong Option ENtered");
}
}while(opt!=4);
break;
case
2:
do
{
printf("\n*****************
DISPLAYING THE STUDENT NAMES
USING STACK **************
\n");
printf("\n1.To
Enter the Student Details\n2.To delete a Detail\n3.Display
the Student
Details\n4.exit\n");
scanf("%d",&opt);
switch(opt)
{
case
1:
printf("\nenter
Number of Students Details to be Taken:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter
%dth student Name to push into stack\n",i);
scanf("%s",str);
push(str);
}
break;
case
2:
printf("\n%s
is deleted",pop(top));
printf("\nThe
Contents of Stack:\n");
display2();
break;
case
3:
printf("\n
Contents of stack:\n");
display2();
break;
case
4:
break;
default:
printf("\nWrong
Option Entered\n");
}
}while(opt!=4);
break;
case
3:
printf("\nenter
n value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter
%dth string to push into stack\n",i);
scanf("%s",str);
push1(str);
}
printf("\n
content of stack top to bottom\n");
display3();
printf("\n\ncontent
of stack from bottom to top");
rdisplay3();
break;
case
4:
do
{
printf("\n
************* TOWERS OF HANOI
*************\n");
printf("\1.Enter
one of the Following Options:\n");
printf("\n1.Iterative
Method\n2.Recursive Method\n3.Exit\n");
scanf("%d",&opt);
switch(opt)
{
case
1:
printf("Enter the no of disk:");
scanf("%d",&n);
hanoi(n,'a','b','c');
break;
case
2:
printf("Enter
the number of disks : ");
scanf("%d",
&num);
printf("The
sequence of moves involved in the Tower of Hanoi
are :\n");
towers(num,
'A', 'C', 'B');
break;
case
3:
break;
default:
printf("\nWrong
Option Entered\n");
}
}while(opt!=3);
break;
case
5:
exit(0);
default: printf("\Wrong Option
Entered\n");
}
}while(ch!=5);
}
========== Hacking Don't Need Agreements ==========
Just Remember One Thing You Don't Need To Seek Anyone's To Hack Anything Or Anyone As Long As It Is Ethical, This Is The Main Principle Of Hacking Dream
Thank You for Reading My Post, I Hope It Will Be Useful For You
I Will Be Very Happy To Help You So For Queries or Any Problem Comment Below Or You Can Mail Me At Bhanu@HackingDream.net
No comments:
Post a Comment