Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node {
- int value;
- struct node * next;}node_t;
- node_t* addHead(){
- node_t* h = malloc(sizeof(node_t));
- if(h == NULL) return NULL;
- h->value=1;
- h->next=NULL;
- return h;
- }
- void addNode (int val,node_t* head)
- {
- node_t * newNode =NULL;
- newNode = malloc(sizeof(node_t));
- if(newNode==NULL) return;
- newNode->value=val;
- newNode->next=NULL;
- while(head->next!=NULL)
- head=head->next;
- head->next=newNode;
- }
- void printNodes (node_t* head)
- {
- if(head==NULL)return;
- while(head)
- {
- printf("%d ",head->value);
- head=head->next;
- }
- printf("\n");
- }
- void removeNode(int val,node_t* head)
- {
- node_t * second;
- second = head->next;
- node_t * third;
- third = second->next;
- while(third){
- if(second->value==val)
- {
- head->next=third;
- free(second);
- return;
- }
- head=second;
- second=head->next;
- third=second->next;
- }
- if(second->value==val)
- {
- head->next=NULL;
- free(second);
- return;
- }
- }
- node_t* removeHead(node_t* head){
- node_t * second;
- second=head;
- head=head->next;
- free(second);
- return head;
- }
- int main()
- {
- node_t * head;
- head=addHead();
- addNode(5,head);
- addNode(7,head);
- printNodes(head);
- head=removeHead(head);
- addNode(9,head);
- addNode(10,head);
- addNode(2,head);
- removeNode(7,head);
- printNodes(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement