Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Circular linked list (http://www.go4expert.com/articles/circular-linked-list-t1282/)

shabbir 5Mar2009 16:36

Re: Circular linked list
 
Change the loop condition of 'y' for a variable looping 5 times.

harris 5Mar2009 22:06

Re: Circular linked list
 
Code:


/* Circular Queues */
#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
const int MAX = 5;
class cqueue
{
int a[MAX],front,rear;
public :
cqueue()
{
front=rear=-1;
}
void insert(char);
int deletion();
void display();
};
void cqueue :: insert(char TaxLic)
{
if((front==0 && rear==MAX-1) || (rear+1==front))
cout<<" Circular Queue is Full";
else
{
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=TaxLic;
}
if(front==-1)
front=0;
}
int cqueue :: deletion()
{
int k;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
k=a[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
}
return k;
}
void cqueue :: display()
{
int i;
if(front==-1)
cout<<"Circular Queue is Empty";
else
{
if(rear < front)
{
for(i=front;i<=MAX-1;i++)
cout<<a[i]<<" ";
for(i=0;i<=rear;i++)
cout<<a[i]<<" ";
}
else
{
for(i=front;i<=rear;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
}
int main()
{
cqueue c1;
char TaxLic [7];
char Dname [20];
char TaxTyp [15];
int ch,val;
char op;
do
{
//clrscr();
system("cls");
cout<<"-----------Menu-------------\n";
cout<<"1.Insertion\n";
cout<<"2.Deletion\n";
cout<<"3.Display\n";
cout<<"4.Exit\n";
cout<<"Enter Your Choice <1..4> ?";
cin>>ch;
switch(ch)
{
case 1 : cout<<"Enter Taxi License Number: ";
fflush(stdin);
gets(TaxLic);
//cin>>val;
 
c1.insert(TaxLic);
break;
case 2 : val=c1.deletion();
cout<<"Deleted Element :"<<val<<endl;
break;
case 3 : c1.display();
break;
}
cout<<"Do you want to continue<Y/N> ?";
fflush(stdin);
cin>>op;
}while(op=='Y' || op=='y');
getch();
}

Help me out, i'm stuck.. Well, it's running ok but when i change the input from int type to char, then it shows error..

shabbir 6Mar2009 08:49

Re: Circular linked list
 
Again the same question. What kind of error. Also if you have some problem with your code try creating a new thread rather than into this thread.

Also intend your code so that its easy to understand

pandaren23 7Mar2009 19:46

Re: Circular linked list
 
Code:

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

                /*COE116L - C1
          Machine Problem: Implementation of a
            circular linked list!*/

struct Prof
{
    int node_no,nNo,ctr,m,p,nCount,ctr2;
    char name[20],*list[];
    Prof *next;
};

Prof trans;

void Initialize(Prof *start)
{
    cout<<"\nEnter node number: ";
    cin>>start->node_no;
    cout<<"\nEnter the name of the student: ";
    gets(start->name);
    start->next=start;
    trans.nCount++;
}

Prof* ins_bef(Prof *start)
{
    Prof *newnode, *current;
    newnode = new Prof;
    current = start;
    cout<<"\nEnter the node number before which you want to insert a node: ";
    cin>>trans.nNo;
    Initialize(newnode);
    if(current->node_no == trans.nNo)
    {
      newnode->next = start;
      while(current->next != start)
        current = current->next;
      current->next = newnode;
      start = newnode;
      return(start);
    }
    while(current->next!=start)
    {
      if(current->next->node_no == trans.nNo)
      {
        newnode->next=current->next;
        current->next=newnode;
        return(start);
      }
      current = current->next;
    }
    clrscr();
    cout<<"\trans.nNo match found!\n";
    return(start);
}

void ins_aft(Prof *start)
{
    int  x = 0;
    Prof *newnode, *current;
    newnode = new Prof;
    cout<<"\nEnter the node number after which you want to insert a node: ";
    cin>>trans.nNo;
    Initialize(newnode);
    current = start;
    while(current->next != start)
    {
      if(current->node_no == trans.nNo)
      {
        newnode->next = current->next;
        current->next = newnode;
        x = 1;
      }
      current = current->next;
    }
    if(x == 0 && current->next == start && current->node_no == trans.nNo)
    {
      newnode->next = current->next;
      current->next = newnode;
      x = 1;
    }
    if(x == 0 && current->next == start)
    {
      clrscr();
      cout<<"\trans.nNo match found!\n";
    }
}


Prof* Delete(Prof *start)
{
    Prof *delnode, *current;
    current = start;
    delnode = current;
    while(current->next != start)
      current = current->next;
    current->next = start->next;
    start = start->next;
    trans.list[trans.ctr] = delnode -> name;
    trans.ctr++;
    trans.ctr2 = trans.ctr;
    trans.nCount--;
    delete(delnode);
    return(start);
   
}

void Display(Prof *start)
{
    Prof *current;
    int x = 0;
    current = start;
    while(current->next != start && x < 5)
    {
      cout<<"\nNode number "<<current->node_no<<": "<<current->name;
      current = current->next;
      x++;
    }
    cout<<"\nNode number "<<current->node_no<<": "<<current->name;
}


void Order()
{
    int i = 0;
    if (trans.ctr2 == 0)
      cout<<"\nPlease start the count first!";
    else
    {
      cout<<"ORDER OF STUDENTS:\n";
      while (trans.ctr != 0 && i != trans.ctr2)
      {
        if (i != trans.ctr2-1)
          cout<<i+1<<".) "<<trans.list[i]<<endl;
        else
          cout<<endl<<trans.list[i]<<" is the last student!"<<endl;
        i++;
        trans.ctr--;
      }
      trans.ctr = trans.ctr2;
    }
}

void Simulation(Prof *start)
{
    Prof *current;
    current = start;
    trans.p = 0;
    while(current->next != NULL && trans.nCount > 0)
    {
      if (trans.p == trans.m)
      {
        Delete(current);
        trans.p = 0;
      }
      current = current->next;
      trans.nNo = current->node_no;
      trans.p++;
    }
}

void main()
{
    Prof *head;
    char ch;
    int opt, x = 0;
    trans.nCount = 0;
    clrscr();
    head = new Prof;
    head->next = NULL;
    mama:
    cout<<"\nChoose from the menu: \n"
        <<"\n1. Initialize the node\n"
        <<"\n2. Insert before a specified node\n"
        <<"\n3. Insert after a specified node\n"
        <<"\n4. Delete a particular node\n"
        <<"\n5. Order of Students\n"<<"\n6. Start the count\n"
        <<"\n7. Display\n"<<"\n8. Exit\n"<<"\t>> ";
    cin>>opt;
    if(x == 0 && opt != 1)
    {
      if (opt == 8)
      exit(0);
      else if(opt > 8)
      cout<<"\nEntered value is not in the choices!";
      else
      cout<<"\nYou must initialize at least one node!\n";
      goto mama;
    }
    if(x == 1 && opt == 1)
    {
      cout<<"\nInitialization can occur only once.\n"
          <<"Now you can insert a node\n";
      goto mama;
    }
    if((opt == 4 || opt == 6) && head->next == head)
    {
      cout<<"\nYou catrans.nNot delete the last node!\n";
      goto mama;
    }
    if(x == 0 && opt == 1)
      x = 1;
    switch(opt)
    {
      case 1:  clrscr();
          cout<<"\nEnter a value for m: ";
          cin>>trans.m;
          Initialize(head);
          goto mama;
      case 2:  clrscr();
          head=ins_bef(head);
          goto mama;
      case 3:  clrscr();
          ins_aft(head);
          goto mama;
      case 4:  clrscr();
          head = Delete(head);
          goto mama;
      case 5:  clrscr();
          Order();
          goto mama;
      case 6:  clrscr();
          Simulation(head);
          goto mama;
      case 7:  clrscr();
          Display(head);
          goto mama;
      case 8:  exit(0);
      default: clrscr();
          cout<<"\nEntered value is not in the choices!";
          goto mama;
    }
}

sir shabbir can you help me out with this? it's really urgent i need to pass this on wednesday. i have a problem on displaying the last node..

the problem is this:

The students decided themselves to be arranged in a circle and excuse the Mth person around the circle - with the size of the circle being reduced by one each time a person is excused. The problem is to find out which person will be the last remaining, or more generally, to find the order in which the people are excused.

here's an example:

if the circle contains : JIM JANE JACK JUNE JASON JOE JEREMY JANELLE and M = 5,
then the order will be : JASON JIM JOE JACK JEAN JANELLE JANE with JEREMY as the last person/node that will approach the professor.

i hava a problem in displaying the last node, but the order is correct though.. choose the 6th menu in the program if you want to do the operations after choosing it and i want to display the last node it produces an infinite loop so i placed an iteration to stop the loop ,can you please help me out?

xpi0t0s 8Mar2009 05:54

Re: Circular linked list
 
You know where shabbir said post your own thread? That didn't apply only to harris. Want help? POST YOUR OWN THREAD.

pandaren23 9Mar2009 16:01

Re: Circular linked list
 
sorry, after i've posted it yesterday i thought about what shabbir said to harris and i wanted to delete what i've posted but there is no delete function anyway then i got disconnected while posting another thread yesterday.. my bad.. sorry :p

jacob1kao 8Jul2009 00:12

Re: Circular linked list
 
PETER_APIIT
What is the source code for the "Link_List.h" in the statement of {#include "Link_List.h"}?
What is the environment that your program run?
I thank you very much!
jacob1kao@gmail.com

enerst 18May2010 20:14

To All Great And Sincere Hackers
 
Hello all I am Enerst, I am new hear my email id is (jaccy_luv@yahoo.com) please add me to you yahoo list there are tools i want to be buying from to time for working, after you have added me i shall speak with you for more enligtnement of what i am saying please add me all, it really hurt when people calls them self hackers and rip you off, please *** me all, i believe no matter how ignorant i may be you can also learn from me.

Regards to you all hackers expecting to chat with you via yahoo.

thanks King Enerst


All times are GMT +5.5. The time now is 18:06.