Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _node{
- struct _node *next;
- int element;
- }Node;
- /*
- Prints the data in the nodes.
- */
- void printData(Node* head){
- Node* temp;
- temp = head;
- while(temp!=NULL){
- printf("%d",temp->element);
- printf("->");
- temp=temp->next;
- }
- printf("NULL\n");
- free(temp);
- }
- /*
- Adds a new node to the front.
- */
- Node* push(Node* head,int data){
- Node* n = (Node*)malloc(sizeof(Node));
- n->element = data
- ; n->next = head;
- return n;
- }
- /*
- Adds a new node to the end.
- */
- Node* append(Node* head,int data){
- Node* beg = head;
- Node* n = (Node*)malloc(sizeof(Node));
- n->element = data;
- n->next = NULL;
- if(beg==NULL) return n;
- while(beg->next!=NULL){
- beg=beg->next;
- }
- beg->next = n;
- return head;
- }
- /*
- Remove first node.
- */
- Node* pop(Node* head){
- Node* t = head->next;
- free(head);
- return t;
- }
- /*
- Remove node at position @param(position);
- */
- Node* deletePosition(Node* head,int position){
- //If empty, do nothing
- if(head == NULL) return head;
- //If first position, pop
- if(position == 0) return pop(head);
- Node* t = head;
- int tempPost = 0;
- while(t!= NULL && position-1>tempPost){
- t=t->next;
- tempPost++;
- }
- if(t == NULL || t->next == NULL) return head;
- //Next node of the next node of temp
- Node* n = t->next->next;
- //Free the temp next
- free(t->next);
- //Connect the temp with the next of the next
- t->next = n;
- return head;
- }
- //------------------------------------------------------------------------------------------------------------
- /*
- Sum all elements from the LinkedList;
- */
- int sumNode(Node* head){
- Node* t = head;
- int rez = 0;
- while(t!=NULL){
- rez+=t->element;
- t=t->next;
- }
- free(t);
- return rez;
- }
- /*
- Sum all even numbers;
- */
- int sumEven(Node* head,int idx){
- if(head == NULL) return 0;
- if(idx%2 == 0) return head->element + sumEven(head->next,idx+1);
- return 0+sumEven(head->next,idx+1);
- }
- /*
- Sum all elements recursievly from the LinkedList;
- */
- int sumRec(Node* head){
- if(head == NULL)
- return 0;
- return head->element + sumRec(head->next);
- }
- /*
- Count all elements from the LinkedList;
- */
- int count(Node* head){
- Node* t = head;
- int len = 0;
- while(t!=NULL){
- len++;
- t=t->next;
- }
- free(t);
- return len;
- }
- /*
- Count all recursievly elements from the LinkedList;
- */
- int countRec(Node* head){
- if(head == NULL)
- return 0;
- return 1+countRec(head->next);
- }
- /*
- Concatenate two lists together;
- */
- Node* concatenate(Node* first,Node* second){
- if(first == NULL) return second;
- if(second == NULL) return first;
- Node* beg = first;
- while(beg->next!=NULL){
- beg=beg->next;
- }
- beg->next = second;
- return first;
- }
- /*
- Checks if two lists are equal;
- */
- int areEqual(Node* first,Node* second){
- int flag = 1;
- if(first == NULL && second != NULL) return 0;
- if(first != NULL && second == NULL) return 0;
- Node *p=first,*q=second;
- while(p!=NULL && q!=NULL){
- if(p->element != q->element){
- flag = 0;
- break;
- }
- p=p->next;
- q=q->next;
- }
- return flag;
- }
- /*
- Makes Cilular linked-list;
- */
- Node* cilular(Node* head){
- if(head==NULL) return head;
- Node *temp = head;
- while(temp->next!=NULL)
- temp=temp->next;
- temp->next = head;
- return head;
- }
- /*
- Reverses linked-list;
- */
- Node* reverse(Node* head){
- Node* prev = NULL;
- Node* curr = head;
- Node* next = head;
- while(curr!=NULL){
- next = curr->next;
- curr->next = prev;
- prev = curr;
- curr = next;
- }
- head = prev;
- return head;
- }
- /*
- Remove duplicates;
- */
- void removeDuplicates(Node* head){
- if(head == NULL || head->next == NULL) return;
- Node* temp = head;
- int lastValue = temp->element;
- while(temp->next!=NULL){
- if(lastValue == temp->next->element){
- //Next node of the next node of temp
- Node* n = temp->next->next;
- //Free the temp next
- free(temp->next);
- //Connect the temp with the next of the next
- temp->next = n;
- }
- else{
- lastValue = temp->element;
- temp=temp->next;
- }
- }
- }
- /*
- Returns middle node;
- */
- Node* middle(Node* head){
- Node *fast = head,
- *slow = head;
- while(fast!=NULL&&slow!=NULL){
- fast=fast->next->next;
- slow=slow->next;
- }
- return slow;
- }
- /*
- Split at middle;
- */
- Node* split(Node* head){
- Node* mid = middle(head);
- Node* helper = mid;
- helper->next = NULL;
- }
- /*
- Copy list;
- */
- Node* copy(Node* head){
- Node* temp = head;
- Node* new = NULL;
- while(temp!=NULL){
- new = append(new,temp->element);
- temp = temp->next;
- }
- free(temp);
- return new;
- }
- /*
- Delete all nodes;
- */
- void deleteAllRec(Node* head){
- if(head==NULL) return;
- deleteAllRec(head->next);
- free(head);
- }
- /*
- Detects loop;
- */
- int detectLoop(Node* head){
- Node *sp = head, *fp = head;
- while(sp!=NULL && fp!=NULL && fp->next!=NULL){
- sp = sp->next;
- fp=fp->next->next;
- if(sp == fp){
- printf("Found loop @ %d\n",sp->element);
- return 1;
- }
- }
- return 0;
- }
- /*
- Special delete case;
- */
- Node* filter(Node* head, int max){
- if(head==NULL)
- return NULL;
- if(head->element == max){
- free(head);
- return filter(head->next,max);
- }
- else{
- head->next = filter(head->next,max);
- return head;
- }
- }
- /*
- Loopify;
- */
- Node* loopify(Node* head){
- if(head == NULL) return NULL;
- if(head->next == NULL) return head;
- Node* last = head;
- while(last->next!=NULL){
- last=last->next;
- }
- while(head->element!=last->element){
- head = head->next;
- }
- last->next = head;
- return head;
- }
- Node* addSpecial(Node* head, int data){
- Node* new = malloc(sizeof(Node));
- new->element = data;
- new->next = NULL;
- if(head==NULL){
- return new;
- }
- if(head->element == data) return NULL;
- if(head->element>data){
- new->next = head;
- head = new;
- return head;
- }
- Node* temp = head;
- while(temp->next!=NULL){
- if(temp->next->element == data) return NULL;
- if(temp->next->element > data){
- new->next = temp->next;
- temp->next = new;
- return head;
- }
- temp = temp->next;
- }
- temp->next = new;
- return head;
- }
- Node* addSpecialRec(Node* head,int data){
- if(head==NULL || head->element >= data){
- Node* new = malloc(sizeof(Node));
- new->next = NULL;
- new->element = data;
- return new;
- }
- else{
- head->next = addSpecialRec(head->next,data);
- return head;
- }
- }
- Node* delRec(Node* head,int data){
- if(head == NULL) return NULL;
- if(head->element == data) {
- free(head);
- return delRec(head->next,data);
- }
- else{
- head->next = delRec(head->next,data);
- return head;
- }
- }
- Node* addEnd(Node* head,int data){
- if(head==NULL){
- Node* new = malloc(sizeof(Node));
- new->element=data;
- new->next = NULL;
- return new;
- }
- else{
- head->next = addEnd(head->next,data);
- return head;
- }
- }
- /*
- Test program;
- */
- int main(){
- Node* first = NULL;
- //int t; scanf("%d",&t);
- //int n = t;
- //while(n>0){
- // first = push(first,(n%10));
- // n=n/10;
- //}
- printData(first);
- first = addEnd(first,32);
- first = addEnd(first,2);
- printData(first);
- return 0;
- }
Add Comment
Please, Sign In to add comment