C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 5th, 2008, 04:13 AM
ambarisha.kn ambarisha.kn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 6 ambarisha.kn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 4 sec
Reputation Power: 0
Doble circular linked list..

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

struct dclinklist
{
    struct dclinklist *prev;  /** Stores address of previous node **/
    int no;                      
    struct dclinklist *next;  /** stores address of next node **/
};

/** Redefining dclinklist as node **/
typedef struct dclinklist node;

void init(node*);     /** Input function **/
void ins_aft(node*);  /** Function inserting before **/
node* ins_bef(node*); /** Function inserting after **/
node* del(node*);
//node* del_front(node*);
//node* del_rear(node*);
void disp(node*);
//void ins_rear(node*);  
//void ins_front(node*);

node *head;
void main()
{
    
    char ch;                 
    int opt, item;                 
    static int flag=0;       
   
    head=(node*)malloc(sizeof(node));
    head->next=NULL; 
    head->prev=NULL;
    do
    {
again:
    printf("\nEnter your option\n");
    printf("\n1. To intialise first element \n");
	printf("\n2. to add at front end after first element\n");
	printf("\n3. to add at rear end \n");
    printf("\n4. To add before a specified node\n");
    printf("\n5. To add after a specified node\n");
    printf("\n6. Delete a particular node\n");
  
    printf("\n7. Display all the nodes\n");
	
	
 
    scanf("%d",&opt);
    if(flag==0 && opt!=1)
    {
        printf("\nNo. You must first initialize at least one node\n");
        goto again;
    }
   
    if(opt==4 && head->next==head)
    {
        printf("\nYou cannot delete the one and only the single node\n");
        goto again;
    }
    if(flag==0 && opt==1)
        flag=1;
    switch(opt)
    {
    case 1:
        init(head);
        break;
		case 2:
		ins_front(head);      
        break;
    case 3:
		 ins_rear(head);      
        break;
    case 4:
		  head=ins_bef(head);        
        break;
    case 5:
		ins_aft(head);       
        break;	
    case 6:
		 head=del(head);       
        break;
	case 7:
		 disp(head);
		break;
	case 8:
		head=del_front(head);
		break;
	case 9:
		head=del_rear(head);
		break;	 
	default:
		printf("Choose correct option\n");
		break;
    }
    printf("\nDo you wish to continue[y/n]\n");
    ch=(char)getche();
    }while(ch=='Y' || ch=='y');    
    printf("\nPress any key to exit\n");
    getch();
}

/*** Function for inputing data ***/
void init(node *start)
{

start->prev=start;

    printf("\nEnter a  number\n");
    scanf("%d",&start->no);
    start->next=start;

	
//while(start->next!=NULL)
//start=start->next;
   /* start->prev=start;*/

   
    //start->next=start;
//node *newnode;            
  //  newnode=(node*)malloc(sizeof(node));
//newnode=start;

 //printf("\nEnter a  number\n");
   // scanf("%d",&newnode->no);
	//newnode->next=NULL;
	//newnode->prev=start;
}

/*** Function for inserting a node after a specified node ***/
void ins_aft(node *start)
{
    int rno;                  /* number for inserting a node */
    int flag=0;
    node *newnode;            /* New inputed node*/
    node *current;            /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the  number after which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    current=start;
    while(current->next!=start)
    {
        /***  Insertion checking for all nodes except last  ***/
        if(current->no==rno)
        {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==start && current->no==rno)
    {
        /***  Insertion checking for last node  ***/
        newnode->next=current->next;     /* Start is being copied */
        current->next->prev=newnode;
        current->next=newnode;
        newnode->prev=current;
        flag=1;
    }
    if(flag==0 && current->next==NULL)
        printf("\nNo match found\n");
}

/*** Function for inserting a node before a specified node ***/
node* ins_bef(node *start)
{
    int rno;                  /* Roll number for inserting a node*/
    node *newnode;            /* New inputed node */
    node *current;            /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the number before which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    current=start;
    if(current->no==rno)
    {
        /*** Insertion checking for first node ***/
        newnode->next=current;
        current->prev=newnode;
        while(current->next!=start)
            current=current->next;
        newnode->prev=current;
        current->next=newnode;
        start=newnode;
        return(start);
    }
    while(current->next!=start)
    {
        /*** Insertion checking for all node except first ***/
        if(current->next->no==rno)
        {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            return(start);
        }
        current=current->next;
    }
    /*
    If the function does not return from any return statement.
    There is no match to insert before the input  roll number.
    */
    printf("\nMatch not found\n");
    return(start);
}

/*** Function for deleting a specified node ***/
node* del(node *start)
{
    int rno;                  /* number for deleting a node*/
    node *delnode;            /* Node to be deleted */
    node *current;            /* Node for travelling the linked list*/
    printf("\nEnter the number whose node you want to delete\n");
    scanf("%d",&rno);
    current=start;
    if(current->no==rno)
    {
        /***  Checking condition for deletion of first node  ***/
        delnode=current; 
        while(current->next!=start)
            current=current->next;
        current->next=start->next;
        start->next->prev=current;
        start=start->next;
        free(delnode);
        return(start);
    }
    else
    {
        while(current->next->next!=start)
        {
            /***  Checking condition for deletion of   ***/
            /*** all nodes except first and last node  ***/
            if(current->next->no==rno)
            {
                delnode=current->next;
                current->next=current->next->next;
                current->next->prev=current;
                free(delnode);
                return(start);
            }
            current=current->next;
        }
        if(current->next->next==start && current->next->no==rno)
        {
            /***  Checking condition for deletion of last node  ***/
            delnode=current->next;
            free(delnode);
            current->next=start;
            return(start);
        }
    }
    printf("\nMatch not found\n");
    return(start);
}

/*** Function for displaying the linked list ***/
void disp(node *start)
{
    node *current;            /* Node for travelling the linked list*/
    current=start;
    while(current->next!=start)
    {
        printf("\n %d",current->no);
        current=current->next;
    }
    printf("\n %d",current->no);
	
}



/*** Function for inserting a node at rear end ***/
//void ins_rear(node *start)
//{
//   
//    node *newnode;           
//    node *current;            
//    newnode=(node*)malloc(sizeof(node));
//    
//    init(newnode);
//    current=start;
//    while(current->next!=start)
//		current=current->next;
//    
//        current->next=newnode;
//
//        newnode->next=start;
//	   newnode->prev=current;           
//    
//}

void ins_rear(node *start)
{
   
    node *newnode;           
    node *current;            
    newnode=(node*)malloc(sizeof(node));
init(newnode);
  current=start;
    while(current->next!=start)
		current=current->next;
    
        current->next=newnode;

        newnode->next=start;
	   newnode->prev=current;           
    
}
//void ins_front(node *head)
//{
//	node *newnode;
//	node *cur;
//	newnode=(node*)malloc(sizeof(node));
//	
//	init(newnode);
//	head->prev=newnode;
//	newnode->next=head;
//
//	newnode->prev=head->prev;
//
//
//}

void ins_front(node *head)
{
	int item;
	node *newnode;
	node *cur;
	newnode=(node*)malloc(sizeof(node));
	init(newnode);
/*
	printf("Enter the no to be insert\n");
	scanf("%d",&item);*/	
	cur=head->prev;
	head->prev=newnode;
	newnode->next=head;
	newnode->prev=cur;
	 /*head=newnode;
	 return(head);*/

	/*cur=head->next;
	head->next=newnode;
newnode->prev=head;
newnode->next=cur;
cur->prev=newnode;*/
}

node* del_front(node *head)
{
	node *cur;
	node *nextnode;
	
	cur=head->prev;
	nextnode=cur->prev;
	head->prev=nextnode;
	nextnode->next=head;
	printf("The node to be deleted is %d\n", cur->no);
	free(cur);
	return head;
}

node* del_rear(node *head)
{
	node *cur;
	node *prevnode;
	if(head->prev==head)
	{
		printf("List is empty\n");
		return head;
	}
	cur=head->prev;
	prevnode=cur->prev;
	head->prev=prevnode;
	prevnode->next=head;
	printf("The node to be deleted is %d\n", cur->no);
	free(cur);
	return head;
}


i did a progrm for doublew circular linked is..

i am not getting output for 3 fumctions..

1)Insert_frontt
2)Delete from front emd.
3)Delete at rear en.

can any one help me in regarig link list... Please thanks im advanc

Reply With Quote
  #2  
Old May 5th, 2008, 05:18 AM
Peter_APIIT Peter_APIIT is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 22 Peter_APIIT Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 40 m 10 sec
Reputation Power: 0
Since you didn't define a display, how can u sure that u have logic error ?

I hope this help.
Comments on this post
nattylife disagrees: ...uh what?

Reply With Quote
  #3  
Old May 5th, 2008, 08:11 AM
nattylife nattylife is offline
Closet coder
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2005
Location: Plantation, FL <---south florida
Posts: 1,215 nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level)nattylife User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 17 h 21 m 14 sec
Reputation Power: 70
Send a message via AIM to nattylife
what exactly are you getting for the output and what output do you expect?
__________________

Reply With Quote
  #4  
Old May 5th, 2008, 09:42 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,071 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 16 h 35 m 21 sec
Reputation Power: 1446
You have the prototypes for those functions commented out. You should be getting compiler warnings, at least.

Your code is a mess. I would suggest that you clean it up before asking people to wade through it and figure out what problems you have that you didn't bother to explain properly.

If you look at your own code you will see that del_front and del_rear do the same thing.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread).

Reply With Quote
  #5  
Old May 6th, 2008, 08:00 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,083 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 47 m 7 sec
Reputation Power: 448
A few minor points noted at a casual glance:
  • In C, the standard return type for main() is int; while some compilers supports void main(), and the standard allows it, it isn't portable and is strongly discouraged. Provisions for non-int main functions included in the standard only for embedded systems which don't have an OS to return and integer to.
  • It's helpful to recall that a circular list is, well, circular; it doesn't have a head and tail, per se, just a current location in the ring which you are pointing to. As long as the list is actually circular, it shouldn't matter which element your handle points to as long as it points to a valid element.
  • Similarly, there's no need for separate head and tail operations; on a ring structure, these amount to the same thing, as SizeableGrin has already pointed out.
  • Because they aren't nested into other loops, you can replace the two gotos with continues with no other changes, which if nothing else would avoid losing a few points for bad programming style.
  • Treating the first element of the ring separately from the others by initializing it in main() is both unnecessary and a bad idea; it is an element just like any other. Also, the next and prev pointers for the first shouldn't be NULL, they should point to itself. You can use this fact to know when you have only one element in the list, because next and prev would have the same value. You probably don't want to initialize it until you have your first inserted element from the user in any case - better to leave the handle pointer (head in the current version) NULL until then. The allocation and insertion functions (which should be two different things probably) should handle the case where the ring is empty. Since you need that first initialization before doing anything else, that is a special case, so you may want to do it before printing the main menu.
  • There isn't much point in declaring a variable in main() as static; the purpose of a static (in C) is to ensure that the variable retains it's value even when the function it is local to returns - that is, it keeps it's current value the next time you call it. Since you shouldn't be calling main() directly at all, this doesn't actually do anything useful.
  • These two last facts combine together to eliminate the need for the flag variable entirely: you can just check to see if head in NULL. It shouldn't come up in any case, as it should be initialized before you get there in the first place.
  • I'm assuming that you're using Dev-C++, because of the inclusion of the getch() - it prevents the program window from closing before you're finished with it. Most current IDEs don't really need that, as they'll keep the program window open until you explicitly close it. Given that Dev-C++ is apparently a dead project (no updates since 2005), you may want to get a more recent IDE such as Code::Blocks or wxDev-C++. That way, you could eliminate both the getch() and the #include <conio.h> (which is DOS/Windows specific).
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Doble circular linked list..


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway