Created
September 19, 2012 08:54
-
-
Save syshack/3748520 to your computer and use it in GitHub Desktop.
Link with Head
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| typedef struct LNode | |
| { | |
| int data; | |
| struct LNode *next; | |
| }LNode,*Node; | |
| typedef struct Hnode | |
| { | |
| struct LNode *next; | |
| struct LNode *end; | |
| int len; | |
| }Hnode,*HEAD; | |
| HEAD init(HEAD H){ | |
| H = (HEAD)malloc(sizeof(Hnode)); | |
| H->end = NULL; | |
| H->next = NULL; | |
| return H; | |
| } | |
| int add_r(HEAD H,int data){ | |
| Node newnode,p = NULL; | |
| p = H->next; | |
| if (p!=NULL){ | |
| printf("ADD Node:%d\n",data); | |
| while(p&&p->next!=NULL){ | |
| p = p->next; | |
| printf("loop\n"); | |
| } | |
| p->next = newnode = (Node)malloc(sizeof(LNode)); | |
| newnode->data = data; | |
| newnode->next = NULL; | |
| } | |
| else{ | |
| printf("ADD First Node:%d\n",data); | |
| H->next = newnode = (Node)malloc(sizeof(LNode)); | |
| newnode->data = data; | |
| newnode->next = NULL; | |
| } | |
| H->end = newnode; | |
| return 1; | |
| } | |
| int add_l(HEAD H,int data){ | |
| Node newnode,p; | |
| if (H->next!=NULL){ | |
| printf("ADD Node:%d\n",data); | |
| p=H->next; | |
| H->next = newnode =(Node)malloc(sizeof(LNode)); | |
| newnode->data = data; | |
| newnode->next = p; | |
| } | |
| else{ | |
| printf("ADD First Node:%d\n",data); | |
| H->next = newnode =(Node)malloc(sizeof(LNode)); | |
| newnode->data = data; | |
| newnode->next = NULL; | |
| } | |
| return 1; | |
| } | |
| int insert(HEAD H,int i,int data){ | |
| Node p = H->next,newnode; | |
| printf("Insert Node:%d=>%d\n",i,data); | |
| int j=0; | |
| while(p&&j<i-1){ | |
| p = p->next; | |
| ++j; | |
| } | |
| if (!p||j>i-1){ | |
| printf("Error\n"); | |
| return 0; | |
| } | |
| newnode=(Node)malloc(sizeof(LNode)); | |
| newnode->data = data; | |
| newnode->next = p->next; | |
| p->next = newnode; | |
| return 1; | |
| } | |
| int rev(HEAD H){ | |
| Node p=H->next,s,q; | |
| if (p&&p->next!=NULL) | |
| { | |
| q = p->next; | |
| while(q){ | |
| s = q->next; | |
| q->next = p; | |
| p = q; | |
| q = s; | |
| } | |
| H->next->next = NULL; | |
| H->next = p; | |
| return 1; | |
| } | |
| else{ | |
| return 0; | |
| } | |
| } | |
| void GetElem(HEAD H){ | |
| Node p = NULL,q = NULL; | |
| printf("Head:next[%p],end[%p]\n",H->next,H->end); | |
| p = H->next; | |
| int i = 1; | |
| if(p!=NULL){ | |
| printf("%d:%d\n",i,p->data ); | |
| q = p->next; | |
| while (q){ | |
| printf("%d:%d\n",++i,q->data); | |
| q = q->next; | |
| } | |
| } | |
| } | |
| int main(int argc, char const *argv[]) | |
| { | |
| HEAD head = NULL; | |
| head = init(head); | |
| add_r(head,233); | |
| add_r(head, 345); | |
| add_l(head, 111); | |
| insert(head, 2, 222); | |
| rev(head); | |
| GetElem(head); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment