#include<stdio.h>structnode{intdata;structnode*next;};structnode*buildThreeElemList(inta,intb,intc);structnode*push(intdata,structnode*head);intLength(structnode*head);voidprintList1(intn,structnode*head);intcar(structnode*head);structnode*cdr(structnode*head);intisEmpty(structnode*head);//Fun Concatstructnode*concat(structnode*head,structnode*head1);//Main functionintmain(){structnode*lst1=buildThreeElemList(1,2,3);structnode*lst2=buildThreeElemList(4,5,6);printList1(Length(lst1),lst1);push(7,lst1);printList1(Length(lst1),lst1);}//End of mainstructnode*buildThreeElemList(inta,intb,intc){structnode*head=NULL;structnode*second=NULL;structnode*third=NULL;head=malloc(sizeof(structnode));second=malloc(sizeof(structnode));third=malloc(sizeof(structnode));head->data=a;head->next=second;second->data=b;second->next=third;third->data=c;third->next=NULL;returnhead;}voidPush(intdata,structnode**headRef){structnode*newNode=malloc(sizeof(structnode));newNode->data=data;newNode->next=*headRef;*headRef=newNode;}intLength(structnode*head){structnode*current=head;intcount=0;while(current!=NULL){count++;current=current->next;}returncount;}voidprintList1(intn,structnode*head){structnode*current=head;intcount=0;while(current!=NULL){count++;printf("lst %d = %d\n",count,current->data);current=current->next;}}intcar(structnode*head){structnode*current=head;returncurrent->data;}structnode*cdr(structnode*head){structnode*current=head->next;returncurrent;}intisEmpty(structnode*head){structnode*current=head->next;if(current==NULL)return1;elsereturn0;}/*void PushTest() {struct node* head = BuildTwoThree();// suppose this returns the list {2, 3}Push(&head, 1); // note the &Push(&head, 13);// head is now the list {13, 1, 2, 3}}*/
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.