六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 32|回复: 0

链表的C语言实现

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-2-1 09:38:14 | 显示全部楼层 |阅读模式
 
 
#include<stdio.h>typedef struct Node{int id;int value;struct Node* next;}Node;Node* creatSingleList(int singleListLength);Node* getSingleListTail(Node* singleListHead);void printSingleList(Node* singleListHead);void printNodeIDandValue(Node* singleListPointer);int getSingleListLength(Node* singleListHead);int insertNodeAfterId(Node* singleListHead, int id);int insertNodeAfterValue(Node* singleListHead, int value);int searchIdFromList(Node* singleListHead, int id);int searchValueFromList(Node* singleListHead, int value);int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer);int main(int argc, char** argv){Node* singleListHead;Node* singleListTail;int singleListLength;int i;singleListLength = 20;singleListHead = creatSingleList(singleListLength);printSingleList(singleListHead);singleListTail = getSingleListTail(singleListHead);printNodeIDandValue(singleListTail);singleListLength = getSingleListLength(singleListHead);printf("List length is : %d \n", singleListLength);updateSingleListLength(singleListHead, &singleListLength);printf("List length is : %d \n", singleListLength);i = searchIdFromList(singleListHead, 6);if(i != -1){printf("id position  is : %d \n", i);}i = searchValueFromList(singleListHead, 6);if(i != -1){printf("value position  is : %d \n", i);}return 0;}void printSingleList(Node* singleListHead){Node* singleListPointer;singleListPointer = singleListHead;while(singleListPointer != NULL){printf("Node ID: %d \tvalue: %d ",singleListPointer->id,singleListPointer->value);printf("\n");singleListPointer = singleListPointer->next;}}Node* creatSingleList(int singleListLength){Node* singleListHead;Node* singleListPointer;int i;singleListHead = (Node*)malloc(sizeof(Node));singleListPointer = singleListHead;srand(time(NULL));for(i=0;i<singleListLength-1;i++){singleListPointer->id = rand()%singleListLength;singleListPointer->value = 2*i;singleListPointer->next = (Node*)malloc(sizeof(Node));singleListPointer = singleListPointer->next;}singleListPointer->id = rand()%singleListLength;singleListPointer->value = 2*i;singleListPointer->next = NULL;return singleListHead;}Node* getSingleListTail(Node* singleListHead){Node* singleListTail;Node* singleListPointer;singleListPointer = singleListHead;while(singleListPointer->next != NULL){singleListPointer = singleListPointer->next;}singleListTail = singleListPointer;return singleListTail;}void printNodeIDandValue(Node* singleListPointer){printf("\n");printf("Node ID: %d\tvalue: %d",singleListPointer->id,singleListPointer->value);printf("\n");}int getSingleListLength(Node* singleListHead){int i;Node* singleListPointer;singleListPointer = singleListHead;i = 1;while(singleListPointer->next != NULL){i++;singleListPointer = singleListPointer->next;}return i;}int insertNodeAfterId(Node* singleListHead, int id){return 0;}int insertNodeAfterValue(Node* singleListHead, int value){return 0;}int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer){int length;length = getSingleListLength(singleListHead);*singleListLengthPointer = length;return 0;}int searchIdFromList(Node* singleListHead, int id){Node* singleListPointer;int position;position = 0;singleListPointer = singleListHead;while(singleListPointer->id != id){singleListPointer = singleListPointer->next;position++;if(position >= getSingleListLength(singleListHead)){fprintf(stderr,"Error! can not find id in the list!\n");position = -1;break;}}return position;}int searchValueFromList(Node* singleListHead, int value){Node* singleListPointer;int position;position = 0;singleListPointer = singleListHead;while(singleListPointer->value != value){singleListPointer = singleListPointer->next;position++;if(position >= getSingleListLength(singleListHead)){fprintf(stderr,"Error! can not find id in the list!\n");position = -1;break;}}return position;return 0;} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表