1鏈表相關操作

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.創建一個鏈表 2.向鏈表中添加一個節點 3.向指定的位置添加一個節點 4.刪除指定位置...
    Bury丶冬天閱讀 234評論 0 0
  • 1。頭插法和尾插法創建鏈表
    Pitfalls閱讀 322評論 0 0
  • 1 序 2016年6月25日夜,帝都,天下著大雨,拖著行李箱和同學在校門口照了最后一張合照,搬離寢室打車去了提前租...
    RichardJieChen閱讀 5,152評論 0 12
  • 樹的概述 樹是一種非常常用的數據結構,樹與前面介紹的線性表,棧,隊列等線性結構不同,樹是一種非線性結構 1.樹的定...
    Jack921閱讀 4,480評論 1 31
  • 今天的主題課:血液。當問到如果我們不小心受傷了,會怎樣?楷楷回答:會流血的。那么血液是什么顏色呢?“紅色!”楷楷馬...
    ic班閱讀 329評論 0 0