Cチェーンテーブル関連

2846 ワード


#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"

typedef struct linknode{
	int data;
	struct linknode *next;
}node;
node *create(){
	node *head,*p,*s;
	int x, cycle=1;
	head = (node*)malloc(sizeof(node));
	p = head;
	while(cycle){
		printf("
please input the data:"); scanf("%d",&x); if(x != 0){ s = (node *)malloc(sizeof(node)); s->data = x; printf("
%d",s->data); p->next = s; p = s; } else cycle = 0; } head = head->next; p->next = NULL; printf("
head->data %d",head->data); return(head); } void print(node *head){ node *p; p = head; while(p != NULL){ printf("
print... %d",p->data); p = p->next; } } node *reverse(node *head){ node *prev, *pcur,*pnext; if(head == NULL || head->next ==NULL) return head; prev = head, pcur = prev->next;// 2 2 while(pcur){ pnext = pcur->next; pcur->next = prev; prev = pcur; pcur = pnext; } head->next = NULL; head = prev; return head; } node *insert(node *head, int num){ node *p0,*p1,*p2; p1 = head; p0 = (node *)malloc(sizeof(node)); p0->data = num; while(p0->data > p1->data &&p1->next != NULL) {p2 = p1; p1=p1->next;} if(p0->data <= p1->data){// if(head ==p1){// p0->next = p1; head = p0; } else{// p2->next = p0; p0->next = p1; } } else{// p1->next = p0; p0->next = NULL; } return head; } node *del(node *head,int num){ node *pcur,*prev; pcur = prev = head ; while(pcur->data != num && pcur->next != NULL){ prev = pcur; pcur = pcur->next; } if(pcur->data == num){// if(pcur == head){// head = head->next; free(pcur); return head; }else{ prev->next = pcur->next; free(pcur); return head; } }else{// return head; } } int main(int argc, char* argv[]) { node *head; head = create(); print(head); head = reverse(head); printf("after reverse
"); print(head); head = insert(head,2); print(head); head = del(head,2); print(head); printf("Hello World!
"); return 0; }