Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Generic Singly Linked List
#include<stdio.h> #include<stdlib.h> #define TRUE 1 #define True 1 #define true 1 #define FALSE 0 #define False 0 #define false 0 #define bool int #define boolean int typedef struct __$_tm_sll_node { void *ptr; struct __$_tm_sll_node *next; }SinglyLinkedListNode; typedef struct __$_tm_sll { struct __$_tm_sll_node *start; struct __$_tm_sll_node *end; int size; }SinglyLinkedList; typedef struct __$__tm_sll_iterator { SinglyLinkedListNode *node; }SinglyLinkedListIterator; SinglyLinkedList * createSinglyLinkedList(bool *success); void destroySinglyLinkedList(SinglyLinkedList *singlyLinkedList); int getSizeOfSinglyLinkedList(SinglyLinkedList *singlyLinkedList); void addToSinglyLinkedList(SinglyLinkedList *singlyLinkedList,void *ptr,bool *success); void insertIntoSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,void *ptr,bool *success); void * removeFromSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,bool *success); void clearSinglyLinkedList(SinglyLinkedList *singlyLinkedList); void appendToSinglyLinkedList(SinglyLinkedList *targetSignlyLinkedList,SinglyLinkedList *sourceSinglyLinkedList,bool *success); void * getFromSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,bool *success); SinglyLinkedListIterator getSinglyLinkedListIterator(SinglyLinkedList *singlyLinkedList,bool *success); bool hasNextInSinglyLinkedList(SinglyLinkedListIterator *singlyLinkedListIterator); void * getNextElementFromSinglyLinkedListIterator(SinglyLinkedListIterator *singlyLinkedListIterator,bool *success); struct student { int rollNumber; char name[24]; }; int main() { int i1,i2,i3,i4,i5,i6,y; char a,name[33]; int *x; char *p; bool succ; struct student *t,*e; SinglyLinkedList *list1; list1=createSinglyLinkedList(&succ); a='A'; addToSinglyLinkedList(list1,(void *)&a,&succ); p=(char *)getFromSinglyLinkedList(list1,0,&succ); printf("%c\n",*p); a='B'; addToSinglyLinkedList(list1,(void *)&a,&succ); p=(char *)getFromSinglyLinkedList(list1,1,&succ); printf("%c\n",*p); a='C'; addToSinglyLinkedList(list1,(void *)&a,&succ); p=(char *)getFromSinglyLinkedList(list1,2,&succ); printf("%c\n",*p); i1=100; i2=200; i3=300; addToSinglyLinkedList(list1,(void *)&i1,&succ); addToSinglyLinkedList(list1,(void *)&i2,&succ); addToSinglyLinkedList(list1,(void *)&i3,&succ); x=(int *)getFromSinglyLinkedList(list1,3,&succ); printf("%d\n",*x); x=(int *)getFromSinglyLinkedList(list1,4,&succ); printf("%d\n",*x); x=(int *)getFromSinglyLinkedList(list1,5,&succ); printf("%d\n",*x); strcpy(name,"Rias Gremory"); addToSinglyLinkedList(list1,(void *)&name,&succ); p=(char *)getFromSinglyLinkedList(list1,6,&succ); printf("%s\n",p); t=(struct student *)malloc(sizeof(struct student)); strcpy(t->name,"Chizato"); t->rollNumber=101; addToSinglyLinkedList(list1,(void *)t,&succ); e=(struct student *)getFromSinglyLinkedList(list1,7,&succ); printf("RollNumber: %d, Name: %s\n",e->rollNumber,e->name); return 0; } SinglyLinkedList * createSinglyLinkedList(bool *success) { if(success) *success=false; SinglyLinkedList *singlyLinkedList; singlyLinkedList=(SinglyLinkedList *)malloc(sizeof(SinglyLinkedList)); singlyLinkedList->start=NULL; singlyLinkedList->end=NULL; singlyLinkedList->size=0; if(success) *success=true; return singlyLinkedList; } void destroySinglyLinkedList(SinglyLinkedList *singlyLinkedList) { if(singlyLinkedList==NULL) return; clearSinglyLinkedList(singlyLinkedList); free(singlyLinkedList); } int getSizeOfSinglyLinkedList(SinglyLinkedList *singlyLinkedList) { if(singlyLinkedList==NULL) return 0; return singlyLinkedList->size; } void clearSinglyLinkedList(SinglyLinkedList *singlyLinkedList) { SinglyLinkedListNode *node; if(singlyLinkedList==NULL) return; if(singlyLinkedList->start!=NULL) { node=singlyLinkedList->start; singlyLinkedList->start=singlyLinkedList->start->next; free(node); } singlyLinkedList->size=0; } void addToSinglyLinkedList(SinglyLinkedList *singlyLinkedList,void *ptr,bool *success) { SinglyLinkedListNode *node; if(success) *success=false; if(singlyLinkedList==NULL) return; node=(SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode)); if(node==NULL) printf("Unable to allocated memory\n"); node->ptr=ptr; node->next=NULL; if(singlyLinkedList->start==NULL) { singlyLinkedList->start=node; singlyLinkedList->end=node; } else { singlyLinkedList->end->next=node; singlyLinkedList->end=node; } singlyLinkedList->size++; if(success) *success=true; } void insertIntoSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,void *ptr,bool *success) { SinglyLinkedListNode *node,*p1,*p2; int x; if(success) *success=false; if(singlyLinkedList==NULL) return; if(index<0 || index>singlyLinkedList->size) return; if(singlyLinkedList->size==index) { addToSinglyLinkedList(singlyLinkedList,ptr,success); return; } node=(SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode)); node->ptr=ptr; node->next=NULL; if(index==0) { node->next=singlyLinkedList->start; singlyLinkedList->start=node; singlyLinkedList->size++; if(success) *success=true; return; } p1=singlyLinkedList->start; x=0; while(x<index) { p2=p1; p1=p1->next; x++; } node->next=p1; p2->next=node; singlyLinkedList->size++; if(success) *success=true; } void * removeFromSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,bool *success) { void *ptr; SinglyLinkedListNode *p1,*p2; int x; if(success) *success=false; if(singlyLinkedList==NULL) return NULL; if(index<0 || index>=singlyLinkedList->size) return NULL; p1=singlyLinkedList->start; x=0; while(x<index) { p2=p1; p1=p1->next; x++; } ptr=p1->ptr; if(singlyLinkedList->start==p1 && singlyLinkedList->end==p1) { singlyLinkedList->start=NULL; singlyLinkedList->end=NULL; }else if(singlyLinkedList->end==p1) { singlyLinkedList->end=p2; singlyLinkedList->end->next=NULL; }else if(singlyLinkedList->start==p1) { singlyLinkedList->start=singlyLinkedList->start->next; } else { p2->next=p1->next; } free(p1); singlyLinkedList->size--; if(success) *success=true; return ptr; } void * getFromSinglyLinkedList(SinglyLinkedList *singlyLinkedList,int index,bool *success) { SinglyLinkedListNode *node; int x; if(success) *success=false; if(singlyLinkedList==NULL) return NULL; if(index<0 || index>=singlyLinkedList->size) return NULL; x=0; node=singlyLinkedList->start; while(x<index) { node=node->next; x++; } if(success) *success=true; return node->ptr; } void appendToSinglyLinkedList(SinglyLinkedList *targetSinglyLinkedList,SinglyLinkedList *sourceSinglyLinkedList,bool *success) { SinglyLinkedListNode *s,*e,*t,*node; bool done; if(success) *success=false; if(targetSinglyLinkedList==NULL) return; if(sourceSinglyLinkedList==NULL || sourceSinglyLinkedList->size==0) { if(success) *success=true; return; } s=NULL; e=NULL; done=true; t=sourceSinglyLinkedList->start; while(t!=NULL) { node=(SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode)); if(node==NULL) { done=false; break; } node->ptr=t->ptr; node->next=NULL; if(s==NULL) { s=node; e=node; } else { e->next=node; e=node; } t=t->next; } if(done==false) { while(s!=NULL) { node=s; s=s->next; free(node); } return; } if(targetSinglyLinkedList->start==NULL) { targetSinglyLinkedList->start=s; targetSinglyLinkedList->end=e; targetSinglyLinkedList->size=sourceSinglyLinkedList->size; } else { targetSinglyLinkedList->end->next=s; targetSinglyLinkedList->end=e; targetSinglyLinkedList->size+=sourceSinglyLinkedList->size; } if(success) *success=true; } SinglyLinkedListIterator getSinglyLinkedListIterator(SinglyLinkedList *singlyLinkedList,bool *success) { SinglyLinkedListIterator singlyLinkedListIterator; singlyLinkedListIterator.node=NULL; if(success) *success=false; if(singlyLinkedList==NULL) return singlyLinkedListIterator; if(singlyLinkedList->start==NULL) { if(success) *success=true; return singlyLinkedListIterator; } if(success) *success=true; singlyLinkedListIterator.node=singlyLinkedList->start; return singlyLinkedListIterator; } bool hasNextInSinglyLinkedList(SinglyLinkedListIterator *singlyLinkedListIterator) { if(singlyLinkedListIterator==NULL) return false; if(singlyLinkedListIterator->node==NULL) return false; return true; } void * getNextElementFromSinglyLinkedListIterator(SinglyLinkedListIterator *singlyLinkedListIterator,bool *success) { void *ptr=NULL; if(singlyLinkedListIterator==NULL || singlyLinkedListIterator->node==NULL) return ptr; ptr=singlyLinkedListIterator->node->ptr; singlyLinkedListIterator->node=singlyLinkedListIterator->node->next; if(success) *success=true; return ptr; }
run
|
edit
|
history
|
help
0
es collar
Double data type.c
Programação em C, testes
gets.c
binary to txt
18BCE2182 ASSESS_1 Q2-1
bitmask
segundo avance de la lista enlazada
Lab6 v1.3Sin,cos,tan,cot
bit shift and mask