练手,自己憋了个单链表,c语言写的。
#include#include typedef struct linknode{ int data; struct linknode* next;}node,*nodep;nodep insert_head(nodep head,nodep node){ node->next = head; head = node; return head;}nodep insert_tail(nodep head,nodep node){ nodep end = head; if(head==NULL) { head = node; } else { while(end->next!=NULL) { end = end->next; } end->next = node; } return head;}int search_node(nodep head,int num){ nodep temp = head; while(temp) { if(temp->data==num) { return 1; } temp = temp->next; } return -1;}nodep delete_node(nodep head,int data){ nodep temp = head; nodep prev = head; while(temp) { if(temp->data == data) { prev->next = temp->next; free(temp); } prev = temp; temp = temp->next; } return head;}void output(nodep head){ nodep temp = head; while(temp!=NULL) { printf("%d ",temp->data); temp = temp->next; } printf("\n");}int main(){ nodep head = NULL; srand(time(NULL)); int i = 0; for(;i<10;i++) { nodep newnode = (nodep)malloc(sizeof(node)); newnode->data = rand()%100; newnode->next = NULL; head = insert_tail(head,newnode); output(head); } int is_have = 0; nodep del = NULL; int num = 0; printf("请输入一个整数:"); scanf("%d",&num); del = delete_node(head,num); output(del); /* is_have = search_node(head,num); printf("is_have num : %d\n",is_have); */ return 0;}