// DSTC --- DLIST.H -- DOUBLE LINK LIST ... #include <stdio.h> #include <stdlib.h> typedef struct nd { int info; struct nd *next; struct nd *prev; }Node;
typedef struct { Node *start; }Dlinklist; Dlinklist * GetDlinklist() { Dlinklist *temp; temp=(Dlinklist *)malloc(sizeof(Dlinklist)); temp->start=NULL; return temp; } Node * GetNode() { Node *n; n=(Node *)malloc(sizeof(Node)); n->next=NULL; n->prev=NULL; return n; } int empty(Dlinklist *l) { if(l->start==NULL) return 1; else return 0; } //Obtaining address of last node Node * LastNode(Dlinklist *l) { Node *t; if(empty(l)) return NULL; t=l->start; while(t->next!=NULL) t=t->next; return t; }
//Count number of nodes int CountNodes(Dlinklist *l) { int c=0; Node *t; t=l->start; while(t) { c++; t=t->next; } return c; } // Inserting a new node in beginning void InsBeg(Dlinklist *l, int i) { Node *n; n=GetNode(); n->info=i; if(!empty(l)) { n->next=l->start; l->start->prev=n; } l->start=n; } // Deleting a node from beginning int DelBeg(Dlinklist *l) { Node *t; if(empty(l)) { printf("Dlinklist already empty\n"); return 0; //unsussefull operation } t=l->start; l->start=t->next; l->start->prev=NULL; free(t); return 1; //successfull operation } // Inserting a new node at end void InsEnd(Dlinklist *l, int i) { Node *n; Node *t; n=GetNode(); n->info=i; if(empty(l)) l->start=n; else { //first of all we will go to last node t=LastNode(l); t->next=n; n->prev=t; } } // Deleting a node from end int DelEnd(Dlinklist *l) { Node *t; //if linklist is empty if(empty(l)) { printf("Dlinklist already empty\n"); return 0; //unsussefull operation } t=l->start; //if linklist contain only one node if(l->start->next==NULL) { l->start=NULL; free(t); return 1; } //if linklist contains more than one node while(t->next!=NULL) t=t->next; t->prev->next=NULL; free(t); return 1; //successfull operation } // Inserting a new node at specific positon int InsPos(Dlinklist *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->prev=n; t->next=n; n->prev=t; return 1; } // Deleting a node at specific position int DelPos(Dlinklist *l,int pos) { Node *t; //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 to be deleted t=l->start; while(pos>=2) { t=t->next; pos--; } t->prev->next=t->next; t->next->prev=t->prev; free(t); return 1; } void Displist(Dlinklist *l) { Node *t; int i=1; t=l->start; printf("\n------------------Link list details----------------------\n"); printf("Total nodes are: %d \n\n", CountNodes(l)); while(t) { printf("Node number %d is: %d\n", i, t->info); i++; t=t->next; } printf("\n---------------------------------------------------------\n"); } |
No comments:
Post a Comment