Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int data;
- Node* next;
- };
- struct List
- {
- Node* head;
- unsigned int length;
- };
- void addElemToList(List* list, unsigned int number)
- {
- Node*temp;
- temp=(Node*)malloc(sizeof (Node));
- temp->next=NULL;
- temp->data=number;
- if(list->head == NULL)
- {
- list->head=temp;
- }else{
- temp->next=list->head;
- list->head=temp;
- }
- list->length++;
- }
- void delEleInList(List* list, unsigned int number)
- {
- Node* current = list->head;
- if(number==1)
- {
- list->head=list->head->next;
- free(current);
- }else
- {
- for(unsigned int i=0;i<number-2;++i)
- current=current->next;
- Node*temp=current->next;
- current->next=temp->next;
- free(temp);
- }
- list->length--;
- }
- void reverse(List* list)
- {
- Node* current = list->head;
- Node* prev= NULL, *next=NULL;
- while (current!=NULL)
- {
- next = current->next;
- current->next=prev;
- prev = current;
- current=next;
- }
- list->head=prev;
- }
- void print(List* list)
- {
- Node*current=list->head;
- for(unsigned int i=0; i<list->length; ++i)
- {
- printf("%d ",current->data);
- current=current->next;
- }
- }
- int getItem(List* list, unsigned int index)
- {
- Node* current=list->head;
- unsigned int count = 0;
- while(current != NULL)
- {
- if(count == index)
- return(current->data);
- count++;
- current = current->next;
- }
- }
- void setValueItem(List* list, unsigned int index, int newData)
- {
- Node* current=list->head;
- unsigned int count = 0;
- while(current != NULL)
- {
- if(count == index)
- current->data=newData;
- count++;
- current = current->next;
- }
- }
- int main()
- {
- List list;
- list.length=0;
- list.head=NULL;
- for(int i=1;i<=5;++i)
- addElemToList(&list,i);
- print(&list); printf("%s","\n");
- delEleInList(&list,4);
- print(&list); printf("%s","\n");
- int temp=getItem(&list,1);
- printf("\n%d\n\n",temp);
- reverse(&list);
- print(&list); printf("%s","\n");
- setValueItem(&list,2,123);
- print(&list); printf("%s","\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement