Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct Element{
- struct Element *next;
- int data;
- }Element;
- int removeL(Element *elem);
- Element *head, *tail;
- int removeL(Element *elem){
- Element *cur=head;
- if (!elem)
- return 1;
- if(elem==head){
- head=elem->next;
- free(elem);
- if (!head)
- tail=NULL;
- return 0;
- }
- while(cur){
- if (cur->next ==elem){
- cur->next=elem->next;
- free(elem);
- if (cur->next==NULL)
- tail=cur;
- return 0;
- }
- cur=cur->next;
- }
- return 1;
- }
- void printL(){
- Element *cur=head;
- printf("List:");
- while(cur){
- printf(" %d ",cur->data);
- cur=cur->next;
- }
- printf("\n");
- }
- int main(){
- /* create a 3 element linked list */
- head=(Element *)malloc(sizeof(Element *));
- head->data=5;
- head->next=(Element *)malloc(sizeof(Element *));
- head->next->data=6;
- head->next->next=(Element *)malloc(sizeof(Element *));
- head->next->next->data=7;
- tail=head->next->next;
- /* print out the list */
- printL();
- /* remove element (try head, head->next or head->next->next in the argument) */
- removeL(head->next);
- /* reprint the modified list */
- printL();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement