【数据结构作业】2.13

#include
using namespace std;

typedef struct Node
{
	int data;
	struct Node * next;	
}*node;

struct Node* createlist()
{
	struct Node*headNode = (struct Node*)malloc(sizeof(struct Node));
	headNode->next = NULL;
	headNode->data = NULL;
	return headNode; 
}

struct Node* createNode(int data)
{
	struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->next = NULL;
	newNode->data = data;
	return newNode;
}

void insertByTail(struct Node*headNode,int data)
{
	struct Node*newNode = createNode(data);
	struct Node*tailNode = headNode;
	while(tailNode->next != NULL)
	{
		tailNode = tailNode->next;
	}
	tailNode->next = newNode;
}

void printList(node list)
{
	node i = list->next;
	for(;i != NULL; i = i->next)
	{
		printf("%d ", i->data);
	}
	puts("");
}

int locate(node list, int data)
{
	int j = 1;
	for(node i = list->next; i != NULL; i = i->next, j ++ )
	{
		if(i->data == data)
		{
			return j;
		}
	}
	return 0; 
}

int main()
{
    node list = createlist();
    int i;
    
    printf("输入链表的数据:\n");
    while(cin >> i)
    {
        insertByTail(list, i);
        if(cin.get() == '\n')
        break;
    }
    
    printf("链表为:\n");
    printList(list);
    
    printf("输入locate的数据:\n");
    int data;
	cin >> data;
    printf("locate结果为: %d", locate(list, data));
	return 0;
}

balabalabalabalabalabalabalabalabalabalabalabalabalabalabalabalabalabalabalabalabala

你可能感兴趣的:(数据结构作业)