// DSTC ---- CLIST.H Circular Link List .... Header File #include <stdio.h> #include <stdlib.h> typedef struct nd { int info; struct nd *next; }Node; typedef struct { Node *start; }Clinklist; Clinklist * GetClinklist() { Clinklist *temp; temp=(Clinklist *)malloc(sizeof(Clinklist)); temp->start=NULL; return temp; } Node * GetNode() { Node *n; n=(Node *)malloc(sizeof(Node)); n->next=NULL; return n; } int empty(Clinklist *l) { if(l->start==NULL) return 1; else return 0; } //Obtaining address of last node Node * LastNode(Clinklist *l) { Node *t; if(empty(l)) return NULL; t=l->start; while(t->next!=l->start) t=t->next; return t; } //Count number of nodes int CountNodes(Clinklist *l) { int c; Node *t; if(empty(l)) return 0; c=1; t=l->start; while(t->next!=l->start) { c++; t=t->next; } return c; } // Inserting a new node in beginning void InsBeg(Clinklist *l, int i) { Node *n,*t; n=GetNode(); n->info=i; if(!empty(l)) { t=LastNode(l); n->next=l->start; t->next=n; l->start=n; } else { l->start=n; n->next=n; } } // Deleting a node from beginning int DelBeg(Clinklist *l) { Node *t,*p; if(empty(l)) { printf("Clinklist already empty\n"); return 0; //unsussefull operation } t=l->start; if(CountNodes(l)==1) l->start=NULL; else { p=LastNode(l); l->start=t->next; p->next=l->start; } free(t); return 1; //successfull operation } // Inserting a new node at end void InsEnd(Clinklist *l, int i) { Node *n; Node *t; n=GetNode(); n->info=i; if(empty(l)) { l->start=n; n->next=n; } else { //first of all we will go to last node t=LastNode(l); t->next=n; n->next=l->start; } } // Deleting a node from end int DelEnd(Clinklist *l) { Node *t,*p; //if linklist is empty if(empty(l)) { printf("Clinklist already empty\n"); return 0; //unsussefull operation } t=l->start; //if linklist contain only one node if(l->start->next==l->start) { l->start=NULL; free(t); return 1; } //if linklist contains more than one node p=NULL; while(t->next!=l->start) { p=t; t=t->next; } p->next=l->start; free(t); return 1; //successfull operation } // Inserting a new node at specific positon int InsPos(Clinklist *l, int i, int pos) { Node *n; Node *t; if(pos>CountNodes(l)) { printf("Syntax error ... this many nodes does not exist"); return 0; //unsuccessull operation } //if node is to be inserted at first position if(pos==1) { InsBeg(l,i); return 1; } n=GetNode(); n->info=i; //obtain a node just before the inserted position t=l->start; while(pos>2) { t=t->next; pos--; } n->next=t->next; t->next=n; return 1; } // Deleting a node at specific position int DelPos(Clinklist *l,int pos) { Node *t,*p; //check number of nodes if(pos>CountNodes(l)) { printf("Syntax error ... this many nodes does not exist"); return 0; //unsuccessfull operation } //if node is to be deleted from first position if(pos==1) { DelBeg(l); return 1; } //obtain a node just before the deleted position t=l->start; while(pos>2) { t=t->next; pos--; } p=t->next; t->next=p->next; free(p); return 1; } void Displist(Clinklist *l) { Node *t; int i=1; int c; t=l->start; printf("\n----Link list details----\n"); c=CountNodes(l); printf("Total nodes are: %d \n\n", c); while(c>0) { printf("Node number %d is: %d\n", i, t->info); i++; t=t->next; c--; } printf("\n--------------------------------\n"); } |
No comments:
Post a Comment