面试题6:查找某数在链表中的位置
2016-03-27 22:57 Keiven_LY 阅读(1491) 评论(0) 编辑 收藏 举报基本思路:声明一个结点p指向链表头结点,然后用p遍历链表,当p->data与要查询的数据相等时,返回其位置即可。
功能函数:
/* 查找指定元素所在位置 */ int LocateElem(Node *head, int elem) { Node *p = head; //声明一个结点p,指向头结点 int i = 0; Node *q = p->next; while(q) { i++; if(q->data == elem) return i; q=q->next; } return 0; }
完整可执行程序:
#include<iostream> #include<stdlib.h> #include<time.h> using namespace std; typedef struct node { int data; struct node *next; }Node; /* 创建含有n个结点的单链表 */ Node *CreateListHead(int n) { Node *head; head=(Node *)malloc(sizeof(Node)); /*创建头结点*/ Node *q = head; /* 初始化随机数种子 */ srand(time(0)); //srand函数在stdlib.h头文件中,time函数在time.h头文件中 for(int i=0; i < n; i++) { Node *p = (Node *)malloc(sizeof(Node)); p->data = rand()%100+1; //随机生成100以内的数字 p->next = q->next; q->next = p; q = p; } q->next = NULL; return head; } /* 在链表的pos位置插入元素elem */ void InsertNode(Node *head, int pos, int elem) { Node *p = head; //声明一个结点p,指向头结点 int i = 1; while(p && i < pos) //寻找插入的位置 { p = p->next ; ++i; } if(!p || i > pos) { cout << "要插入的位置不存在" << endl; } Node *q = (Node *)malloc(sizeof(Node)); q->data = elem; q->next = p->next; p->next = q; } /* 删除链表pos位置的元素 */ void DeleteNode(Node *head, int pos) { Node *p = head; //声明一个结点p,指向头结点 int i = 1; while(p->next && i < pos) //寻找插入的位置 { p = p->next; ++i; } if(!(p->next) || i > pos) { cout << "要删除的位置不存在" << endl; } Node *q = p->next; p->next = q->next; free(q); // 让系统回收此结点,释放内存 } /* 获取指定位置的元素 */ int GetElem(Node *head, int pos) { Node *p = head; //声明一个结点p,指向头结点 int i = 0; while(p && i < pos) { p = p->next; ++i; } if(!p || i > pos) { cout << "第" << pos << "个元素不存在" << endl; } int getData = p->data; return getData; } /* 查找指定元素所在位置 */ int LocateElem(Node *head, int elem) { Node *p = head; //声明一个结点p,指向头结点 int i = 0; Node *q = p->next; while(q) { i++; if(q->data == elem) return i; q=q->next; } return 0; } /****打印单链表******/ void print(Node *head) { Node *p; if(head->next==NULL) { cout << "The LinkList is Empty !" <<endl; return; } p=head->next; while(p!=NULL) { cout << p->data << " " ; p=p->next; } } int main() { Node *SingleLinkList = NULL; int length; cout << "Please input the length of LinkList: " <<endl; cin >> length; SingleLinkList = CreateListHead(length); cout << "The new created LinkList as below: " <<endl; print(SingleLinkList); cout << endl; int position_insert, position_delete,position_get,element; int data; cout << "请输入要插入的元素及其插入位置: " <<endl; cin >> data >> position_insert; InsertNode(SingleLinkList, position_insert, data); cout << "插入新元素后的链表如下: " <<endl; print(SingleLinkList); cout << endl; cout << "请输入要删除元素的位置: " <<endl; cin >> position_delete; DeleteNode(SingleLinkList, position_delete); cout << "删除" << position_delete <<"位置的元素后,链表如下: " <<endl; print(SingleLinkList); cout << endl; cout << "请输入要获取元素的位置: " <<endl; cin >> position_get; cout << "链表第" << position_get << "个元素为:" << GetElem(SingleLinkList, position_get) <<endl; cout << "输入要查找的元素: " <<endl; cin >> element; cout << "元素" << element << "是链表的第" << LocateElem(SingleLinkList, element) << "个元素" << endl; system("pause"); return 0; }
运行结果: