Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- struct Node
- {
- void* key;
- struct Node* next;
- struct Node* pre;
- };
- struct List
- {
- struct Node* end;
- struct Node* current;
- struct Node* start;
- int start_index;
- int end_index;
- int current_index;
- };
- struct Node arr2[100];
- struct List arr1[5];
- int head_free_index=0;
- int node_free_index=0;;
- struct List* ListCreate()
- {
- struct List* ret;
- if(head_free_index+1>=5 || node_free_index+1>=100)
- return NULL;
- arr2[node_free_index].next=NULL;
- arr2[node_free_index].pre=NULL;
- arr2[node_free_index].key=NULL;
- arr1[head_free_index].start=&arr2[node_free_index];
- arr1[head_free_index].end=&arr2[node_free_index];
- arr1[head_free_index].current=&arr2[node_free_index];
- arr1[head_free_index].start_index=head_free_index;
- arr1[head_free_index].end_index=head_free_index;
- arr1[head_free_index].current_index=head_free_index;
- //if(arr1[head_free_index].start!=NULL) printf("hello");
- ret=&arr1[head_free_index];
- head_free_index++;
- node_free_index++;
- return ret;
- }
- int ListCount(struct List A)
- {
- //A.start=arr1[0].start;
- //A.end=arr1[0].end;
- //A.current=arr1[0].current;
- int count=1;
- /*while( arr1[0].current->next!=NULL)
- {
- //printf("hello0000");
- count++;
- if (count>=2)break;
- struct Node temp;
- arr1[0].current=&temp;//arr1[0].current->next;
- //A=A.current->next;
- }*/
- int ind = A.end_index - A.start_index;
- //if(arr1[0].current->next!=NULL)printf("adad");
- return ind;
- }
- void* ListFirst(struct List A)
- {
- A.current=A.start;
- return A.start;
- }
- void* ListLast(struct List A)
- {
- A.current=A.end;
- return A.end;
- }
- void* ListNext(struct List A)
- {
- if(A.current->next==NULL)
- return NULL;
- A.current=A.current->next;
- return A.current;
- }
- void* ListPrev(struct List A)
- {
- if(A.current->pre==NULL)
- return NULL;
- A.current=A.current->pre;
- return A.current;
- }
- void* ListCurr(struct List A)
- {
- return A.current;
- }
- int ListAdd(struct List A, void* item )
- {
- struct Node newNode;
- newNode.key=item;
- if(node_free_index>=100)
- return -1;
- if(A.start->next==NULL&&A.start->pre==NULL)
- {
- //printf("hello");
- newNode.next=arr1[0].start;
- //if(arr1[0].start!=NULL)printf("fassafds");
- newNode.pre=NULL;
- arr1[0].start->pre=&newNode;
- arr1[0].start=&newNode;
- arr1[0].end=newNode.next;
- arr1[0].current=&newNode;
- node_free_index++;
- arr1[0].end_index++;
- node_free_index++;
- return 0;
- //if(arr1[0].current->next!=NULL)printf("asd");
- }
- else if(A.current==NULL && A.current->next!=NULL)
- {
- newNode.next=A.start;
- A.start->pre=&newNode;
- newNode.pre=NULL;
- A.start=&newNode;
- A.current=&newNode;
- //arr1[0].end_index++;
- }
- else if(A.current==NULL && A.current->pre!=NULL)
- {
- newNode.pre=A.end;
- A.end->next=&newNode;
- newNode.next=NULL;
- A.end=&newNode;
- A.current=&newNode;
- //arr1[0].end_index++;
- }
- else
- {
- newNode.next=A.current->next;
- A.current->next=&newNode;
- newNode.pre=A.current;
- newNode.next->pre=&newNode;
- arr1[0].end_index++;
- }
- for(int i=node_free_index;i>A.current_index;i--)
- {
- arr2[i].key=arr2[i-1].key;
- arr2[i].next=arr2[i-1].next;
- arr2[i].pre=arr2[i-1].pre;
- }
- for(int i=0;i<head_free_index;i++)
- {
- if(i!=0)
- arr1[i].start=arr1[i].start->next;
- arr1[i].end=arr1[i].end->next;
- arr1[i].current=arr1[i].current->next;
- }
- node_free_index++;
- //if(arr1[0].current->next!=NULL)printf("asd");
- return 0;
- }
- int ListInsert(struct List A, void* item)
- {
- }
- int ListAppend (struct List A, void* item)
- {
- struct Node newNode;
- newNode.key = item;
- if(node_free_index>=100)
- return -1;
- if(A.start->next==NULL&&A.start->pre==NULL)
- {
- newNode.next=arr1[0].start;
- newNode.pre=NULL;
- arr1[0].start->pre=&newNode;
- arr1[0].start=&newNode;
- arr1[0].end=newNode.next;
- arr1[0].current=&newNode;
- //node_free_index++;
- arr1[0].end_index++;
- node_free_index++;
- return 0;
- }
- else
- {
- newNode.pre=A.end;
- newNode.next=NULL;
- A.end->next=&newNode;
- A.end=&newNode;
- }
- for(int i=node_free_index;i>A.current_index;i--)
- {
- arr2[i].key=arr2[i-1].key;
- arr2[i].next=arr2[i-1].next;
- arr2[i].pre=arr2[i-1].pre;
- }
- for(int i=1;i<head_free_index;i++)
- {
- arr1[i].start=arr1[i].start->next;
- arr1[i].end=arr1[i].end->next;
- arr1[i].current=arr1[i].current->next;
- }
- node_free_index++;
- arr1[0].end_index++;
- return 0;
- }
- void* ListRemove(struct List A)
- {
- if(A.current==A.end)
- {
- //printf("end");
- arr1[0].current->pre->next=NULL;
- arr1[0].end=arr1[0].end->pre;
- arr1[0].current->next=NULL;
- arr1[0].current->pre=NULL;
- struct Node* temp1=arr1[0].current;
- //detele A.current;
- arr1[0].end_index--;
- arr1[0].current=arr1[0].end;
- return temp1;
- }
- else if(A.current==A.start)
- {
- //printf("start");
- arr1[0].current->next->pre=NULL;
- arr1[0].start=arr1[0].start->next;
- arr1[0].current->next=NULL;
- arr1[0].current->pre=NULL;
- struct Node* temp2=arr1[0].current;
- //(A.current);
- arr1[0].current=arr1[0].start;
- arr1[0].start_index++;
- return temp2;
- }
- else
- {
- //printf("else");
- arr1[0].current->next->pre=arr1[0].current->pre;
- arr1[0].current->pre->next=arr1[0].current->next;
- struct Node* temp=arr1[0].current->next;
- arr1[0].current->next=NULL;
- arr1[0].current->pre=NULL;
- struct Node* temp3=arr1[0].current;
- //detele A.current;
- arr1[0].end_index--;
- arr1[0].current=temp;
- return temp3;
- }
- }
- void* ListTrim(struct List A)
- {
- arr1[0].end->pre->next=NULL;
- struct Node* temp=arr1[0].end->pre;
- if(A.current==A.end) arr1[0].current=temp;
- arr1[0].end->pre=NULL;
- arr1[0].end->next=NULL;
- struct Node* ret=arr1[0].end;
- //delete A.end;
- arr1[0].end= temp;
- arr1[0].end_index--;
- return ret;
- }
- void* ListConcat(struct List A, struct List B)
- {
- A.end->next = B.start;
- B.start = NULL;
- B.end = B.current = B.start;
- //Listfree B (but not yet made)
- }
- int main()
- {
- arr1[0]=*ListCreate();
- //if(arr1[0].current!=NULL)printf("hello");
- struct List C;
- C.start=arr1[0].start;
- C.end=arr1[0].end;
- C.current=arr1[0].current;
- void* abc=NULL;
- int a = ListCount(arr1[0]);
- printf("%d \n", a);
- int ads= ListAdd(arr1[0],abc);
- //if(arr1[0].current->next!=NULL)printf("adsafafa");
- //printf("%d \n", ads);
- //if(arr1[0].current->next!=NULL)printf("adsafafa");
- a = ListCount(arr1[0]);
- printf("%d \n", a);
- void* gef = NULL;
- int blah = ListAdd(arr1[0],gef);
- a = ListCount(arr1[0]);
- printf("%d \n", a);
- void* axe=ListRemove(arr1[0]);
- a = ListCount(arr1[0]);
- printf("%d \n", a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement