数据结构之单向链表

 

 

 

结构体的定义,由于是单向链表,所以链表只有两个域,一个是值域,用来存储数据,另一个是指针域,用来指向下一个空间。

1 typedef int data_t;
2 typedef struct node{
3        data_t  data;
4        struct  node* next;        
5 }linklist;

由于结构体已经定义完成,下面开始给结构体开空间,并赋值。

 1 linklist *CreateLinklist()
 2 {
 3      linklist *head = (linklist *)malloc(sizeof(linklist));  
 4      if(NULL == head)
 5      {
 6          return NULL;  
 7      }
 8      head->data = -1;
 9      head->next = NULL;
10      return head;
11 }

对链表进行判空处理。

1 int  LinklistIsEmpty(linklist *head)
2 {
3       if(NULL != head)
4       {
5           return head->next == NULL?1:0;
6       }
7 }

获取链表的个数

 1 int  GetLinklistLength(linklist *head)
 2 {
 3       if(NULL == head)
 4       {
 5           return -1;
 6       }
 7 
 8       int len = 0;
 9       linklist *p = head->next;
10       while(p != NULL)
11       {
12           len++;
13           p = p->next;
14       }
15       return len;
16 }

插入元素

 1 int  InsertLinklistData(linklist *head,int pos, data_t data)
 2 {
 3       if(NULL == head)
 4       {
 5           return -1;
 6       }
 7       int len = GetLinklistLength(head);
 8       if(pos<0 || pos>len)
 9       {
10           return -1;
11       }
12 
13       linklist *p = head;
14       while(pos--) //找到要插入元素的前一个位置
15       {
16           p = p->next;
17       }
18       linklist *new = (linklist *)malloc(sizeof(linklist));
19       new->next = NULL;
20       new->data = data;
21 
22       new->next = p->next;
23       p->next = new;
24 
25       return 0;
26 }

删除元素

1、按位置删除元素

 1 int DeleteLinklistByPos(linklist *head,int pos)
 2 {
 3      if(NULL == head)
 4      {
 5            return -1;
 6      }
 7      if(LinklistIsEmpty(head))
 8      {
 9            return -1;
10      }
11      int len = GetLinklistLength(head);
12      if(pos<0 || pos>len)
13      {
14            return -1;
15      }
16      linklist *p = head;
17      linklist *q = NULL;
18      while(pos--)
19      {
20            p = p->next;
21      }
22      q = p->next;
23      
24      p->next = q->next;
25      free(q);
26      q =NULL;
27 
28      return 0;
29 }

2、按值删除元素

 1 int DeleteLinklistByData(linklist *head,data_t data)
 2 {
 3      if(NULL == head)
 4      {
 5            return -1;
 6      }
 7      if(LinklistIsEmpty(head))
 8      {
 9            return -1;
10      }
11      linklist *p = head;
12      linklist *q = p->next;
13      while(q != NULL)
14      {
15            if(q->data == data)
16            {
17                  p->next = q->next;
18                  free(q);
19            }
20            p = q;
21            q = q->next;
22      }
23      q =NULL;
24      return 0;
25 }

 

查询数据

 1 int FindLinklistDataByPos(linklist *head,int pos)
 2 {
 3      if(NULL == head)
 4      {
 5            return -1;
 6      }
 7      if(LinklistIsEmpty(head))
 8      {
 9            return -1;
10      }
11      int len = GetLinklistLength(head);
12      if(pos<0 || pos>len)
13      {
14            return -1;
15      }
16      linklist *p = head->next;
17      while(pos--)
18      {
19            p = p->next;
20      }
21      return p->data;
22 }

更新数据

 1 int UpdateLinklistByPos(linklist *head,int pos,data_t data)
 2 {
 3      if(NULL == head)
 4      {
 5            return -1;
 6      }
 7      if(LinklistIsEmpty(head))
 8      {
 9            return -1;
10      }
11      int len = GetLinklistLength(head);
12      if(pos<0 || pos>len)
13      {
14            return -1;
15      }
16      linklist *p = head->next;
17      while(pos--)
18      {
19            p = p->next;
20      }
21      p->data = data;
22      return 0;
23 }

打印链表

 1 void PrintfLinklist(linklist *head)
 2 {
 3     if(NULL == head)
 4     {
 5         return -1;
 6     }
 7     if(LinklistIsEmpty(head))
 8     {
 9         return -1;
10     }
11     linklist *p = head->next;
12     while(p != NULL)
13     {
14         printf("%d ",p->data);
15         p = p->next;
16     }
17     printf("\n");
18 }

主函数

 1  int main()
 2  {
 3      linklist *head = CreateLinklist();
 4      if(NULL == head)
 5      {
 6          return -1;
 7      }
 8 
 9      // 插入数据
10      int i = 0;
11      while(i<10)
12      {
13          InsertLinklistData(head,i,i+1);
14      }
15      PrintfLinklist(head);
16 
17      //按位置删除数据
18      DeleteLinklistByPos(head,2);
19      PrintfLinklist(head);
20 
21      //按值删除数据
22      DeleteLinklistByData(head,2);
23      PrintfLinklist(head);
24
25      //按位置查找数据
26      int ret = FindLinklistDataByPos(head,2);
27      printf("%d\n",ret);
28 
29      //按位置更新数据
30      UpdateLinklistByPos(head,2,32);
31      PrintfLinklist(head);
32
posted on 2019-12-27 17:22  缘随风烬  阅读(349)  评论(0编辑  收藏  举报