单链表的操作
来源:互联网 发布:软件技术支持面试题 编辑:程序博客网 时间:2024/05/17 02:30
Talk is cheap, show the code
#include <stdio.h>#include <stdlib.h>#define ERROR -404typedef struct Node {int data;struct Node* next;} Node;Node* InitList (void);void add (Node* head,int data);void PrintList (Node* head);int Length (Node* head);int Get (Node* head, int l); //按位查找 int Locate (Node* head, int n); //按值查找 void Insert (Node* head, int i, int x);void Delete (Node* head, int i);void FreeL (Node* head);int main (){Node* head= InitList();int num =1,i,x;scanf("%d",&num);while (num!= -1) {add(head,num);scanf("%d",&num);}PrintList(head);printf("The length is %d\n",Length(head));scanf("%d",&i);printf("Get: %d\n",Get(head,i));PrintList (head);scanf("%d",&i);printf("Locate: %d\n",Locate(head,i));PrintList (head);scanf("%d %d",&i,&x);Insert(head,i,x);PrintList (head);scanf("%d",&i);Delete(head,i);PrintList (head);FreeL(head);return 0;}Node* InitList (void){Node* head = (Node*)malloc(sizeof(Node));head ->next = NULL; // 设置哨兵节点 return head;}void add(Node* head,int data) {Node *p,*end=head,*q;for ( p=head; p ; p=p->next) {end =p;}q =(Node*)malloc(sizeof(Node));end->next =q;q->data =data;q->next= NULL;}void PrintList (Node* head) {Node* p;for (p =head->next; p; p=p->next) {printf("%d ",p->data);}printf("\n");}int Length (Node* head){Node* p;int sum=0;for (p=head->next; p; p=p->next) {sum++;}return sum;}int Get (Node* head, int l) // 按位查找 {Node* p;int cnt=0;for (p=head->next; p ; p=p->next) {cnt++;if (cnt==l) {return p->data;break;}}return ERROR;} int Locate (Node* head, int n){Node* p;int cnt=0;for (p=head->next ; p; p=p->next) {cnt++;if (p->data == n) {return cnt;}} return ERROR;} void Insert (Node* head, int i, int x) {Node *p,*q,*r;int cnt=0;for (p=head->next,q=head; q; q=p,p=p->next) {cnt++;if (cnt==i) {r=(Node*)malloc(sizeof(Node));r->data =x;r->next =p;q->next =r;return ;} }printf("ERROR\n");}void Delete (Node* head, int i) {int cnt=0;Node *p,*q;for (p=head->next,q=head; p; q=p, p=p->next) {cnt ++;if (cnt==i) {q->next= p->next;free(p);return ;}}printf("ERROR\n");}void FreeL (Node* head) {Node *p,*q;for (p=head->next, q=head; p; q=p, p=p->next) {free(q);}free(p);}
0 0
- 简单的单链表操作
- 单链表的基本操作
- 简单的单链表操作
- 单链表的基本操作
- 单链表的操作(C#)
- 单链表的操作
- 单链表的各种操作
- 单链表的操作实现
- 单链表的 操作
- 单链表的基本操作
- 单链表的一些操作
- 单链表的操作
- 关于单链表的操作
- 单链表的各种操作
- 单链表的常用操作
- 单链表的一些操作
- 单链表的一些操作
- 单链表的操作
- msb - 最高有效位
- 链表的重要总结 系列四
- JVM 内存管理
- 解决安装SVN时报2503,2502的问题
- 儲存格式理解筆記(一)WAV格式分析2,理解含意。
- 单链表的操作
- 线性时间排序--计数排序、基数排序、桶排序
- 【诗】《丧钟为谁而鸣》
- Homebrew套件软件管理-ps入职美团第一天
- 防火墙
- 系统应用 第一章课后5
- Visual Assist X 非常实用的Rrfactor功能
- Ubuntu 16.04 一系列软件安装命令,包括QQ、搜狗、Chrome、vlc、网易云音乐安装方法
- gui - 图形用户界面