链表——单链表的实现

用单链表来记录用户输入的数值, 个数不确定。直到输入的数为-1为止

 

include <stdio.h>
#include <stdlib.h>

struct mylist {
    int data;

    struct mylist *next;
};

//用链表来记录用户输入的数值, 个数不确定。直到输入的数为-1为止

int main(void)
{
    int num;
    struct mylist *head = NULL,  //用于存放链表的头节点地址
              *new = NULL;   //用于存放新产生的节点地址

    while (1)
    {
        printf("input num:");
        scanf("%d", &num);

        if (num < 0)
            break;
        //产生一个链表的新节点,用于存放刚输入的值
        new = malloc(sizeof(*new));
        new->data = num;
        new->next = NULL;

        //加入链表去    
        if (NULL == head)
            head = new; //当没有头节点时,新节点则成为头节点
        else
        {
            new->next = head; //新的节点指向头节点        
            head = new;       //新节点作头节点
        }    
    }

    for (new = head; new != NULL; new = new->next)
    {
        printf("%d\n", new->data);
    }

    return 0;
}

现在要把1,2,3存到链表里。有两个节点,都指向NULL,head1的data为1(head == NULL),head1成了头节点,head1->next 还是为NULL。第二步, head2 的data 为2(head == new),头不为NULL,运行else,head2->next = head1. head1 =head2.

第三步,在第二步里 head1 = =head2 , head3->next = head2, head1 == head3.

/////////////////////记录字符串/////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct mylist_t {
    char name[64];
    struct mylist_t *next;
};

int main(void)
{
    struct mylist_t *head = NULL, *new = NULL;
    while(1)
    {
        new = malloc(sizeof(new));
        printf("name:");
        scanf("%s",new->name);
        if(strcmp("exit" ,new->name) == 0)
                {
                    free(new);
                    break;
                }
        new->next = NULL;
        if(head == NULL)
            head = new;
        else
        {    
            new->next = head;
            head = new;
            
        }

    }
     struct mylist_t *tmp;
    for(new = head; new != NULL;)
    {
        printf("name = %s \n",new->name);
        tmp = new->next;
        free(new);
        new =tmp;
    
    }

    return 0;

}

 

 ////////////////////指针记录字符串//////////////////////

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 struct mylist{
 6     char *line;
 7     struct mylist *next;
 8 };
 9 
10 int main(void)
11 {
12     struct mylist *head = NULL, *new = NULL;
13 
14     while(1)
15     {
16         new= malloc(sizeof(new));
17         new->line = malloc(sizeof(1024));
18         printf("input string:");
19         scanf("%s", new->line);
20         if(strcmp("null",new->line) == 0)
21         {
22                 free(new->line);
23                 free(new);
24 
25                 break;
26         }
27         new->next = NULL;
28 
29         if(head == NULL)
30                 head = new;
31         else
32         {
33             new->next = head;
34             head = new;
35         }
36 
37     }
38     struct mylist *tmp ;
39     for (new = head; new != NULL; )
40             {
41                         printf("%s\n", new->line);
42                             tmp =new->next;
43                             free(new->line);
44                             free(new);
45                             new = tmp;
46             }
47 
48                 return 0;
49 }
指针字符串

前面都是没头节点的单链表,下面是有头节点的单链表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct mylist_t {
    int data;
    struct mylist_t *next;
};

int main(void)
{
    struct mylist_t head = {0, NULL}, *new;
    int num;
    while(1)
    {
        printf("input num:");
        scanf("%d",&num);
        if(num < 0)
                break;
        new = malloc(sizeof(new));
        new->data = num;
        new->next = head.next;
        head.next = new;
    }
    struct mylist_t *tmp;
    for(new = head.next; new != NULL;)
    {
        printf("%d\n",new->data);
        tmp = new->next;
        free(new);
        new = tmp;
    }
    return 0;
}
有头的单链表

 先进先出的链表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct mylist {
    int data;
    struct mylist *next;
};

int main(void)
{
    int num;
    struct mylist *new ,*tail = NULL, head = {0,NULL};
    while(1)
    {
        printf("input num:");
        scanf("%d",&num);
        if(num < 0)
                break;
        new = malloc(sizeof(new));
        new->data = num;
        new->next = NULL;

        if(NULL == head.next)
            head.next = new;
        else
            tail->next = new;
        tail = new;
    }

    struct mylist *tmp;
    for(new = head.next; new != NULL ; tmp = new->next,free(new), new = tmp)
            printf("num:%d\n",new->data);

    return 0;
}
先进先出

单链表的删除

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 struct mylist {
 6     int data;
 7     struct mylist *next;
 8 };
 9 
10 
11 int add(struct mylist *head)
12 {
13     int num;
14     struct mylist *new;
15     printf("input data:");
16     scanf("%d",&num);
17     if(num < 0)
18         return -1;
19     new = malloc(sizeof(new));
20     new->data  = num;
21     new->next = head->next;
22     head->next = new;
23 
24     return 0;
25 
26 }
27 void show(struct mylist *head)
28 {
29     struct mylist *new;
30     for(new= head->next; new != NULL; new = new->next)
31         printf("data:%d\n", new->data);
32 }
33 
34 struct mylist * find_del(struct mylist *head,int num)
35 {
36     struct mylist *n,*tmp;
37     for(n = head->next; n != NULL;)
38     {
39 
40         if(num == n->data)
41         {
42             printf("%d\n",n->data);
43             tmp->next = n->next;
44             free(n);
45             return n;
46         }
47         tmp = n;
48         n=n->next;
49     }
50         return 0;
51 }
52 
53 int main(void)
54 {
55     struct mylist head = {0, NULL}, *new;
56     while(add(&head) == 0)
57         ;
58     show(&head);
59     new = find_del(&head, 3);
60     show(&head);
61         return 0;
62 
63 }
单链表的删除

 链表的倒置

include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct mylist {
    int data;
    struct mylist *next;

};

int main(void)
{
    int num;
    struct mylist *new, head= {.next = NULL};

    while(1)
    {
        printf("input data:");
        scanf("%d",&num);
        if(num < 0)
            break;
        new = malloc(sizeof(new));
        new->data = num;
        new->next= head.next;
        head.next = new;
        
    }
    for(new= head.next; new != NULL; new= new->next)
        printf("num:%d\n",new->data);

    ////////////////链表的倒置/////////////////////

    struct mylist *tmp , head2 = {0, NULL};
    for(new = head.next; new != NULL;)
    {
            tmp = new->next;//先把new->next存起来,要改new;
        new->next = head2.next;
        head2.next = new;
        new = tmp;
    }
    for(new= head2.next; new != NULL; new= new->next)
        printf("num:%d\n",new->data);
    return 0;
}
链表的倒置

 

posted on 2017-12-13 11:44  131927  阅读(176)  评论(0)    收藏  举报

导航