C语言单向链表的实现(写的真好,不愧是超级大佬)

一个简单结点的结构体表示为:

    struct note

    {

       int  data;              /*数据成员可以是多个不同类型的数据*/

       struct  note  *next;      /*指针变量成员只能是-个*/

    };

 

一个简单的单向链表的图示

 

1.链表是结构、指针相结合的-种应用,它是由头、中间、尾多个链环组成的单方向可伸缩的链表,链表上的链环我们称之为结点。

2.每个结点的数据可用-个结构体表示,该结构体由两部分成员组成:数据成员与结构指针变量成员。

3.数据成员存放用户所需数据,而结构指针变量成员则用来连接(指向)下-个结点,由于每-个结构指针变量成员都指向相同的结构体,所以该指针变量称为结构指针变量。

4.链表的长度是动态的,当需要建立-个结点,就向系统申请动态分配-个存储空间,如此不断地有新结点产生,直到结构指针变量指向为空(NULL)。申请动态分配-个存储空间的表示形式为:

           (struct  note*)malloc(sizeof(struct  note))

 

链表的建立

   在链表建立过程中,首先要建立第一个结点,然后不断地

在其尾部增加新结点,直到不需再有新结点,即尾指针指向

NULL为止。

 设有结构指针变量   struct note *p,*p1,*head;

      head:用来标志链表头

  p:在链表建立过程中,p总是不断先接受系统动态分配的新结点地址

  p1->next:存储新结点的地址。

 

链表建立的步骤:

第一步:建立第一个结点

struct   node

{

    int   data;

    struct   node  *next;

 };

struct   note   *p,*p1,*head;

head=p1=p=(struct  node  *)malloc(sizeof(struct node);

 

第二步:

      给第-个结点成员data赋值并产生第二个结点

scanf(“%d”,&p->data);  /*输入10*/

p=(struct  node  *)malloc(sizeof(struct node);

 

第三步:将第-个结点与第二个结点连接起来

p1-> next=p;

 

第四步:产生第三个结点

p1=p;

scanf(“%d”,&p->data);/*输入8*/

p=(struct  node  *)malloc(sizeof(struct node);

以后步骤都是重复第三、四步,直到给出-个结束条件,不再建新的结点时,要有

   p->next=NULL;它表示尾结点。

 

 

代码

#include <stdio.h>  
#include<stdlib.h>  
#define  LEN  sizeof(struct node)  
struct node  
{  
int data;  
struct node  *next;  
};  
main()  
{     struct  node  *p, *pl,* head;  
          head=p=(struct node * )malloc(LEN);  
          scanf("%d",&p->data);/*头结点的数据成员*/  
          while(p->data!=0)   /*给出0结束条件,退出循环*/  
          {    pl=p;  
               p=(struct node * )malloc(LEN);  
                  scanf("%d",&p->data);/*中间结点数据成员*/  
                  pl->next=p;/*中间结点的指针成员值*/  
          }  
          p-> next=NULL;/*尾结点的指针成员值*/  
          p=head;/*链表显示*/  
          printf("链表数据成员是:");  
          while(p->next!=NULL)  
          {  
          printf("%d",p->data);  
          p=p->next;  
          }  
          printf("%d\n",p->data);  
}  
 

C语言——单向链表实现

05-18

自己用C语言写的单项链表的实现,贴出来,跟大家一起讨论下。rn[code=c]rnrn/*rn 代码来源:rn http://avr.cnta.net/forum.php?mod=viewthread&tid=291rn 以test为前缀的函数都是测试函数.rn*/rnrn#include rn#include rn#include rn#include rnrnrntypedef unsigned char snl_u8;rntypedef signed char snl_s8;rnrntypedef unsigned short int snl_u16;rntypedef signed short int snl_s16;rnrntypedef unsigned int snl_u32;rntypedef signed int snl_s32;rnrntypedef char snl_char;rnrntypedef enum snl_false, snl_true snl_bool;rnrnrntypedef struct single_node_listrn struct single_node_list *next;rn void *vp;rnSingleNodeList_t;rnrnrn//定义一些接口, 方便使用rn#define snl_trace printfrn#define snl_malloc mallocrn#define snl_mfree freern#define snl_assert assertrn#define snl_memset memsetrn#define snl_memcpy memcpyrn#define snl_memcmp memcmprnrn/***************************************************************************rn** 函数: snl_alloc_newrn** 功能: 申请一个新的节点内存rn** 作者: avrbase_leirn*******/rnstatic SingleNodeList_t *snl_alloc_new(void)rnrn SingleNodeList_t *newnode = NULL;rnrn newnode = (SingleNodeList_t*)snl_malloc(sizeof(SingleNodeList_t));rn snl_assert(NULL != newnode);rn snl_memset((void*)newnode, 0, sizeof(SingleNodeList_t));rnrn return newnode;rnrnrn/*******************************************************************************rn** 函数: snl_free_nodern** 功能: 释放一个节点内存rn** 作者: LeiFaYurn*******/rnstatic void snl_free_node(SingleNodeList_t *node)rnrn snl_mfree((void*)node);rnrnrn/***************************************************************************rn** 函数: snl_insertrn** 功能: 在链表上插入一个新的节点.rn** 入参: rootpp 指向根节点的指针.rn** addvp 需要插入链表的数据地址.rn** front_insert 是否插入在匹配到的节点的前面.rn** private_compare 比较函数, 比较成功后依front_insert决定插入位置.rn** 若直到链表尾都未匹配成功则默认插入链表为尾.rn** 若private_compare为空则插入链表尾.rn** 返回: 返回插入结果.rn** 作者: avrbase_leirn*******/rnstatic snl_bool snl_insert(rn SingleNodeList_t **rootpp,rn void *addvp,rn void *compare_vp,rn snl_bool front_insert,rn snl_bool (*private_compare)(void *, void *))rn rn snl_bool inserted = snl_false;rn snl_bool is_root_pos = snl_true;rn SingleNodeList_t *newnode = NULL;rn SingleNodeList_t *currnode = NULL, *prevnode = NULL;rnrn dorn if(NULL == rootpp)rn break;rnrn newnode = snl_alloc_new();rn if(NULL == newnode)rn break;rn newnode->vp = addvp;rnrn if(NULL == private_compare || NULL == compare_vp)rn rn if(NULL == *rootpp)rn rn *rootpp = newnode;rn rn elsern rn currnode = *rootpp;rn while(NULL != currnode->next)rn currnode = currnode->next;rnrn currnode->next = newnode;rn rnrn inserted = snl_true;rn break;rn rn rn currnode = *rootpp;rn inserted = snl_false;rn is_root_pos = snl_true;rn while(NULL != currnode && !inserted)rn rn if(private_compare(currnode->vp, compare_vp))rn rn //如果是根节点匹配到了, 则根节点要指向下一个节点rn if(is_root_pos)rn rn if(front_insert)rn rn newnode->next = *rootpp;rn *rootpp = newnode;rn rn elsern rn newnode->next = (*rootpp)->next;rn (*rootpp)->next = newnode;rn rn rn elsern rn if(front_insert)rn rn newnode->next = currnode;rn prevnode->next = newnode;rn rn elsern rn newnode->next = currnode->next;rn currnode->next = newnode;rn rn rnrn inserted = snl_true;rn rn elsern rn prevnode = currnode;rn currnode = currnode->next;rn is_root_pos = snl_false;rn rn rnrn if(!inserted)rn rn prevnode->next = newnode;rn inserted = snl_true;rn rnrn while(0);rnrn return inserted;rnrnrn/***************************************************************************rn** 函数: snl_deletern** 功能: 删除链表上snl_compare匹配到的指定的节点rn** 如果比较函数为空则执行全部删除功能.rn** 入参: rootpp 根节点rn** com_node_data_p 用于参与匹配的数据rn** traval_del 是否遍历删除链表中的节点. 否则删除离根节点最近的节点rn** private_compare 数据匹配函数, 其返回值用于决定是否删除rn** private_delete 数据删除函数, 本参数必须非空, 否则无法删除任何数据rn** 返回: 返回删除的节点个数rn** 作者: avrbase_leirn*******/rnstatic snl_u32 snl_delete(rn SingleNodeList_t **rootpp, rn void *cmp_node_data_p,rn snl_bool travel_del,rn snl_bool (*private_compare)(void *, void *),rn void (*private_delete)(void *))rnrn snl_u32 deleted_ct = 0;rn snl_bool is_del_root = snl_true;rn SingleNodeList_t *currnode = NULL, *prevnode = NULL, *delnode = NULL;rnrn do rn if(NULL == rootpp)rn break;rnrn if(NULL == private_delete)rn break;rnrn if(NULL == private_compare)rn rn currnode = *rootpp;rn while(NULL != currnode)rn rn delnode = currnode;rn currnode = currnode->next;rn private_delete(delnode->vp);rn snl_mfree(delnode);rn deleted_ct ++;rn rnrn *rootpp = NULL;rn break;rn rnrn if(NULL == cmp_node_data_p)rn break;rnrn currnode = *rootpp;rn is_del_root = snl_true;rn while(NULL != currnode)rn rn if(private_compare(currnode->vp, cmp_node_data_p))rn rn //如果是根节点匹配到了, 则根节点要指向下一个节点rn if(is_del_root)rn rn *rootpp = prevnode = currnode->next;rn rn elsern rn prevnode->next = currnode->next;rn is_del_root = snl_false;rn rnrn //当前节点指向下一个节点rn delnode = currnode;rn currnode = currnode->next;rnrn //删除节点rn private_delete(delnode->vp);rn snl_mfree(delnode);rn deleted_ct ++;rnrn //非遍历删除则中断循环rn if(!travel_del)rn break;rn rn elsern rn prevnode = currnode;rn currnode = currnode->next;rn is_del_root = snl_false;rn rn rnrn while(0);rnrn return deleted_ct;rnrnrn[/code]

C语言实现单向链表

10-09

#include rn#include "stdlib.h"rnusing namespace std;rnstruct NODErnrn struct NODE* linker;rn int value;rn;rntypedef struct NODE link;rn//int Node_insert(NODE* p,int input)rnvoid printf_link(link* head);rnlink* creat_link(void);rnlink* add_link(link* head);rnlink* paixu_link(link* head);rnlink* insert_order_element(link* head);rnint size_link(link* head);rnlink* paixu_link_pointer(link* head);rnint main()rnrn link *start;rn start = creat_link();rn start = add_link(start);rn start = add_link(start);rn printf_link(start);rn start = insert_order_element(start);rn printf_link(start);rn cout << size_link(start) << endl;rn start = paixu_link(start);rn printf_link(start);rnrnrnrn/* struct NODE stu1,stu2,stu3,stu4,*p;rn stu1.value = 10;rn stu2.value = 20;rn stu3.value = 30;rn stu4.value = 15;rn stu1.linker = &stu2;rn stu2.linker = &stu3;rn stu3.linker = NULL;rn p = &stu1;rn rn rn rn while(p != NULL)rn rn if(p->value < stu4.value && p->linker->value > stu4.value )rn rn stu4.linker = p->linker;rn p->linker = &stu4;rn rn elsern rn p = p->linker;rn rn rn p = &stu1;rn while(p != NULL)rn rn cout << p->value << endl;rn p=p->linker;rn rn */rn /*int i;rn int result[3];rn int mulitiply(int x,int y);rn int minus(int x,int y);rn int add(int x,int y);rn //int i=2,j=3; //循环计数rn int (*point[3])(int x,int y) = &mulitiply,&minus,&add;rn //j= point(i,j);rn //cout << setw(5) << j;rn for(i=0;i<3;i++)rn rn result[i] = point[i](2,3);rn cout<< result[i];rn rn */rn /* rn int a[] = 1,2,3,1,4,5,65,4;rn cout << sizeof a / sizeof a[0] << endl; rn cout<<"五年级一班数学成绩表\n";rn cout<<"首先是第一名许凡的成绩:\t"<<100;rn cout<value < input && p->linker->value > input)rn rn new = (NODE*)malloc(sizeof NODE);rn new ->linker = p->linker;rn p->linker = new ;rn rn rn else p = p->linker; rn rn return 1;rnrn*/rnint mulitiply(int x,int y)rnrn return x*y;rnrnrnint minus(int x,int y)rnrn return abs(x-y);rnrnrnint add(int x,int y)rnrn return x+y;rnrnrnvoid printf_link(link* head)rnrn link* p;rn p = head;rn while(p->linker != NULL)rn rn cout << p->value << endl;rn p = p->linker;rn rn cout << p->value << endl;rnrnlink* creat_link(void)rnrn link *head;rn head = (link *)malloc(sizeof link);rn cout << "head->value :";rn cin >> head->value;rn head -> linker = NULL;rn return head;rnrnlink* add_link(link* head)rnrn link* new_element;rn link* pointer;rn pointer = head;rn while(pointer->linker != NULL)rn rn pointer = pointer->linker;rn rn new_element = (link *)malloc(sizeof link);rn pointer->linker = new_element;rn cout << "new_element->value :";rn cin >> new_element->value;rn new_element -> linker = NULL;rn return head;rnrnrnlink* paixu_link(link* head)rnrn link *pointer,*minimum;rn int temp;rn if(head->linker == NULL)rn rn return head;rn rn elsern rn for(pointer = head;pointer->linker != NULL;pointer=pointer->linker)rn rn for(minimum = pointer;minimum->linker != NULL;minimum=minimum->linker)rn rn if(minimum->value > minimum->linker->value)rn rn temp = minimum->value;rn minimum->value = minimum->linker->value;rn minimum->linker->value = temp;rn rn rn rn rn return head;rn rnrnrnrnlink* paixu_link_pointer(link* head)rnrn link *new_head,*p,*q,*start,*temp,*new_link;rn new_head->linker = head;rn start = new_head;rn for(p=new_head;p->linker->linker!=NULL;p=p->linker)rn rn rn for(q=p->linker,start=p;q->linker!=NULL;q=q->linker,start=start->linker)rn rn if(q->value > q->linker->value)rn rn new_link = q->linker;rn rnrn rn rn rnrnrn rnint size_link(link* head)rnrn link* pointer;rn int size = 0 ;rn pointer = head;rn while(pointer->linker != NULL)rn rn size += 1;rn pointer = pointer->linker;rn rn size += 1;rn return size;rnrnrnrnvoid free_link(link* head)rnrn free(head);rnrnrnrnrnrnlink* insert_order_element(link* head)rnrn link *pointer,*new_elemnet;rn pointer = head;rn new_elemnet = (link*) malloc(sizeof link);rn if(new_elemnet == NULL)rn rn cout << "memory error";rn exit(0);rn rn elsern rn cout << "please input the value of new element: ";rn cin >> new_elemnet->value;rn new_elemnet->linker = NULL; //完成新成员的初始化rn rn //判断插入的值是否小于根节点rn if(new_elemnet->value <= pointer->value)rn rn new_elemnet->linker = pointer;rn return new_elemnet;rn rn elsern rn rn while(pointer->linker != NULL)rn rn if(pointer->value < new_elemnet->value && pointer->linker->value >= new_elemnet->value)rn rn new_elemnet->linker = pointer->linker;rn pointer->linker = new_elemnet;rn return head;rn rn elsern rn pointer= pointer->linker;rn rn rn if(pointer->linker == NULL)rn rn pointer->linker= new_elemnet;rn return head; rn rn rn rnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试