题目描述
建立一个长度为n的带头结点的单链表,查找该链表中数据元素为item的结点,若找到,输出该结点在链表中的位序;没找到,输出0。(处理数据原始类型为整型,链表计算从1开始。)
输入
第一行为链表的长度n; 第二行为链表中的数据元素的值; 第三行为数据item。输出
找到就输出该结点的位序,没有找到输出0。样例输入
10 1 2 3 4 5 6 7 8 9 10 3样例输出
3#include<stdio.h> #include<string.h> #include<math.h> #include<malloc.h> #define INF 0x3f3f3f3f #define MAX(x,y) (x>y?x:y) #define MIN(x,y) (x>y?y:x) #define M 10001 typedef struct LNode { int data; struct LNode* next; }LNode; void search(LNode* L,int n) { int m; scanf("%d", &m); LNode* temp = L; int i = 1; int pos; while (temp->next) { temp = temp->next; if (temp->data == m) { pos = i; printf("%d", pos); return; } i++; } printf("0"); } int main() { LNode* L; L = (LNode*)malloc(sizeof(LNode)); int n; scanf("%d", &n); L->next = NULL; LNode* temp = L; for (int i = 0; i < n; i++) { LNode* p = (LNode*)malloc(sizeof(LNode)); scanf("%d", &p->data); p->next = NULL; temp->next = p; temp= p; } search(L,n); return 0; }
SWUST OJ958: 单链表的定位操作
最新推荐文章于 2024-05-30 21:35:46 发布