美文网首页C语言
1链表相关操作

1链表相关操作

作者: Micason | 来源:发表于2016-06-08 19:53 被阅读145次

1.创建带头节点的链表,并且遍历输出。

题目

Paste_Image.png
#include    <stdio.h>
#include    <stdlib.h>
#define    N    8
typedef  struct list
{  int  data;
   struct list  *next;
} SLIST;

SLIST *creatlist(int  *a)
{
 SLIST *h,*p,*q;
    int i;
    h = p = (SLIST *)malloc(sizeof(SLIST));
    for(i = 0; i < N; i++) {
        q = (SLIST *)malloc(sizeof(SLIST));
        q->data = a[i];
        p->next = q;
        p = q;
    }
    p->next = 0;
    return  h;
 }
void outlist(SLIST  *h)
{
 SLIST *p = h->next;
    while(p) {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
}
int main(void)
{
   SLIST  *head;
  int a[N],i;
   for(i=0;i<N;i++)
   {
       scanf("%d",&a[i]);
   }
  head=creatlist(a);
  outlist(head);
  return 0;
}

2.插入新链表,保持顺序,并遍历输出链表。

Paste_Image.png
#include    <stdio.h>
#include    <stdlib.h>
typedef  struct list
{  int  data;
   struct list  *next;
} SLIST;

SLIST *creatlist()
{  
SLIST *head,*tail,*cnew;
     int num;
    head=NULL;
  
  while(1)
    {
     scanf("%d",&num);
     if(num==-1)
       break;
     cnew=(SLIST*)malloc(sizeof(SLIST));
     cnew->data=num;
     cnew->next=NULL;
     if(head==NULL)
        head=cnew;
     else
        tail->next=cnew;
     tail=cnew;
    }
    return head;
 }
void outlist(SLIST  *h)
{  
SLIST   *p;
       for(p=h;p!=NULL;p=p->next)
       printf("%d ",p->data);
    printf("\n");
}
int main(void)
{  
   SLIST  *head;
  head=creatlist();
  outlist(head);
  return 0;
}

3.插入新的链表,并遍历输出。

Paste_Image.png
#include    <stdio.h>
#include    <stdlib.h>
#define    N    8
typedef  struct list
{  int  data;
   struct list  *next;
} SLIST;
SLIST *insertlist(SLIST * p,int *pvalue)
{
    SLIST *tmp=p,*tmp_p;
    int t=*pvalue;
    while(tmp->next!=NULL&&tmp->next->data<t)
    tmp=tmp->next;
    tmp_p=(SLIST *)malloc(sizeof(SLIST));
    tmp_p->next=tmp->next;
    tmp_p->data=t;
    tmp->next=tmp_p;
    return p;
 }


SLIST *creatlist(int  *a)
{
 SLIST  *h,*p,*q;      int  i;
   h=p=(SLIST *)malloc(sizeof(SLIST));
   for(i=0; i<N; i++)
   {  q=(SLIST *)malloc(sizeof(SLIST));
      q->data=a[i];
   p->next=q;
   p=q;
   }
   p->next=0;
   return  h;
 }
void outlist(SLIST  *h)
{
SLIST *tmp =h->next;
while(tmp!=NULL){
    printf("%d ",tmp->data);
    tmp=tmp->next;
}
}
int main(void)
{
   SLIST  *head;
   int a[N];
int nvalue,i;
   for(i=0;i<N;i++)
   {
       scanf("%d",&a[i]);
   }
  scanf("%d",&nvalue);//插入的结点数据
  head=creatlist(a);
  head=insertlist(head,&nvalue);
  outlist(head);
  return 0;
}

4.删除指定节点,并遍历输出。

Paste_Image.png
#include    <stdio.h>
#include    <stdlib.h>
#define    N    8
typedef  struct list
{  int  data;
   struct list  *next;
} SLIST;
SLIST *deletelist(SLIST * p,int *pvalue)
{  
 SLIST *todel, *head = p;
    while(p && p->next) {
        if (p->next->data == *pvalue) {
            todel = p->next;
            p->next = todel->next;
            free(todel);
        } else {
            p = p->next;
          }
    }
    return head;
 }


SLIST *creatlist(int  *a)
{  
 SLIST  *h,*p,*q;      int  i;
   h=p=(SLIST *)malloc(sizeof(SLIST));
   for(i=0; i<N; i++)
   {  q=(SLIST *)malloc(sizeof(SLIST));
      q->data=a[i];
   p->next=q; 
   p=q;
   }
   p->next=0;
   return  h;
 }
void outlist(SLIST  *h)
{  
  h = h->next;
    while (h) {
        printf("%d ", h->data);
        h = h->next;
   //     if (h) {
  //          printf(" ");
  //      }
    }
    printf("\n");
}
int main(void)
{  
   SLIST  *head;
int nvalue,i;
int a[N];
   for(i=0;i<N;i++)
   {
       scanf("%d",&a[i]);
   }
  scanf("%d",&nvalue);//删除的结点数据
  head=creatlist(a);
  head=deletelist(head,&nvalue);
  outlist(head);
  return 0;
}

相关文章

  • 1链表相关操作

    1.创建带头节点的链表,并且遍历输出。 题目 2.插入新链表,保持顺序,并遍历输出链表。 3.插入新的链表,并遍历...

  • Java常用类库与技巧-集合

    一 数据结构常见问题 数组和链表的区别;链表的操作,如反转,链表环路检测,双向链表,循环链表相关操作;队列,栈的应...

  • 链表相关

    总结一下链表相关的操作 单链表节点的定义 实现单向链表的反向 删除单链表的所有节点

  • 链表的相关操作

  • 链表相关操作

    1。头插法和尾插法创建链表

  • 链表与二叉树

    1. 链表 链表是最基本的数据结构,面试官也常常用链表来考察面试者的基本能力,而且链表相关的操作相对而言比较简单,...

  • 数据结构与算法总结

    1. 链表 链表是最基本的数据结构,面试官也常常用链表来考察面试者的基本能力,而且链表相关的操作相对而言比较简单,...

  • 05 (1)| javascript实现单链表

    在了解链表的基本结构和相关操作的原理,我们可以使用Javascript来实现链表以及其相关的增删该查工作了。 下面...

  • 静态链表的相关操作

    优点:在插入和删除操作时,只需要修改游标,不需要移动元素,从而改进了在顺序存储结构中的插入和删除操作需要移动大量元...

  • 单链表相关操作

    在面试中,链表的操作一直是个很常见的考察点,之前也一直觉得链表相关操作很难,但是只要多动手练一练,没有那么难,最怕...

网友评论

    本文标题:1链表相关操作

    本文链接:https://www.haomeiwen.com/subject/fdjjdttx.html