链表的操作

来源:互联网 发布:南宁php招聘 编辑:程序博客网 时间:2024/05/17 22:51

链表是面试中常考的类型,因为只有几行就可以了。

下面是一些链表代码

// keshan.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdio.h>#include <malloc.h>#define NULL 0#define LEN sizeof(struct Node)struct Node{int value;Node * next;};//打印链表void print_link(Node *head){Node *p = head;printf("%d\n",head->value);while(p->next!=NULL){p = p->next;printf("%d\n",p->value);}}int _tmain(int argc, _TCHAR* argv[]){//创建Node * head = (Node *) malloc(LEN);head->value = 0;//增加Node *p1 = (Node *) malloc(LEN); //增加一个p1->value = 1;head->next = p1;Node *p2 = (Node *) malloc(LEN);//再增加一个p2->value = 2;p1->next = p2;p1 = (Node *) malloc(LEN); //再增加一个p1->value = 3;p2->next = p1;p1->next = NULL;//遍历查询printf("遍历查询\n");Node * p = head;printf("%d\n",p->value);do{p = p->next;printf("%d\n",p->value);}while(p->next != NULL);//查询第2个,第一个即为0printf("遍历第2查询\n");p = head;for(int i=1;i<3;i++){p = p->next;}printf("%d\n",p->value);//修改第3个,数值改为30printf("修改第3个\n");p = head;for(int i=0;i<3;i++){p = p->next;}p->value = 30;print_link(head);//删除getchar();return 0;}


0 0