鏈表(創建,插入,刪除和打印輸出

/*-----------------------------------------------------------------------------
時間:2011年9月28日
文件功能:實現了動態建立一個學生信息的鏈表包括鏈表的
創建、插入、刪除、和打印輸出學生信息包括姓名和分數
本鏈表是帶有頭結點的,頭結點的內容爲空內容
-----------------------------------------------------------------------------*/
/*-------------------------包含頭文件------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

/*-------------------------結構體定義部分------------------------------*/
struct Node
{
 char name[10];
 int score;
 struct Node *next;
};

typedef struct Node ListNode;
/*----------------------------函數聲明部分------------------------------*/


/*---------------------------函數實現部分-------------------------------*/
/*-----------------------------創建鏈表---------------------------------*/
/*在鏈表的末端插入新的節點,建立鏈表*/
ListNode *CreateList(int n)
{
 ListNode *head;//指向頭結點指針
 ListNode *p,*pre;
 int i;
 head=(ListNode *)malloc(sizeof(ListNode));//爲頭節點分配內存空間
 head->next=NULL;//將頭結點的指針域清空
 pre=head;//先將頭結點首地址賦給中間變量pre
 for(i=1;i<=n;i++)//通過for循環不斷加入新的結點
  {
   printf("input name of the %d student:",i);//打印出第幾個人的名字
   p=(ListNode *)malloc(sizeof(ListNode));//爲要插入的節點分配
   //內存空間p指向新插入結點的首地址
   scanf("%s",&p->name);//輸入姓名
   printf("input score of the %d student:",i);
   scanf("%d",&p->score);//輸入分數
   pre->next=p;//將p指向新結點插入鏈表也就是頭結點指針域指向
   //下個結點
   //第一個結點就是p指向的,因爲頭結點內容爲空
   pre=p;//這個起着指向下一個結點的作用
  }
 p->next=NULL;//最後將最後一個結點的指針域清空了
 return head;//返回這個鏈表的首地址
}
/*-------------------------輸出鏈表-----------------------------------*/
void PrintList(ListNode *h)
{
 ListNode *p;
 p=h->next;
 while(p)
  {
   printf("%s,%d",p->name,p->score);
   p=p->next;
   printf("\n");
  }
}
/*----------------------插入鏈表結點--------------------------*/
/*--------------------------------------------------------------------
函數名稱:InsertList(ListNode *h,int i,char name[],int e,int n)
函數功能:插入鏈表結點
入口參數: h: 頭結點地址 i:插入到第幾個結點 name:插入
結點的姓名 e:插入結點的分數 n:
鏈表中結點的個數
除下頭結點外的個數

出口參數:
--------------------------------------------------------------------*/
void InsertList(ListNode *h,int i,char name[],int e,int n)
{
 ListNode *q,*p;//先定義2個指向一個結點的指針
 int j;
 if(i<1 || i>n+1)
  printf("Error! Please input again.\n");
 else
  {
   j=0;
   p=h;//將指針p指向要鏈表的頭結點
   while(j<i-1)
    {
     p=p->next;
     j++;
    }
   q=(ListNode *)malloc(sizeof(ListNode));/*爲要插入的
   結點分配內存空間*/
    
   //----賦值操作--------- 
   strcpy(q->name,name); //將名字拷到要插入的節點內
   q->score=e; //將要插入的節點中分數賦值

   //調整指針域
   
   q->next = p->next; /*這個是將新插入的結點指針域指向
   上一個結點指針域指向的結點地址即爲p->next*/
    
   p->next=q;/*將要插入結點位置前面的結點指針域
   指向現在插入的結點首地址*/
  }
}

/*--------------------------------------------------------------------
函數名稱:DeleteList(ListNode *h, int i, int n)
函數功能:刪除鏈表結點
入口參數: h: 頭結點地址 i:要刪除的結點所在位置
n:
鏈表中結點的個數除下頭結點外的個數

出口參數:
--------------------------------------------------------------------*/
void DeleteList(ListNode *h, int i, int n)
{
 ListNode *p,*q;//首先定義2個指向結點型結構體的指針
 int j;
 char name[10];
 int score;
 if(i<1 || i>n)//如果位置超出了1和n的範圍的話則打印出錯誤信息
  printf("Error! Please input again.\n");
 else//沒有超出除頭結點外的1到n 的範圍的話那麼執行刪除操作
  {
   j=0;
   p=h;//將指針指向鏈表的頭結點首地址
   while(j<i-1)
    {
     p=p->next;
     j++;
    }
   q=p->next; /*q指向要刪除的位置之前的那個結點指針域指向的
   地址q指向的結點就是要刪除的結點*/
    
   p->next=q->next;/*這個就是將要刪除的結點的前面那個結點
   的指針域指向要刪除的結點指針域中存放的下個結點的
   首地址從而實現了刪除第i個結點的作用*/

   strcpy(name,q->name);
   score=q->score;
   
   free(q);//釋放q指向的結點
   printf("name=%s,score=%d\n",name,score);
  }
}

/*--------------------------主函數-------------------------------*/
void main()
{
 ListNode *h;//h指向結構體NODE
 int i = 1, n, score;
 char name [10];

 while ( i )
  {
   /*輸入提示信息*/
   printf("1--建立新的鏈表\n");
   printf("2--添加元素\n");
   printf("3--刪除元素\n");
   printf("4--輸出當前表中的元素\n");
   printf("0--退出\n");

   scanf("%d",&i);
   switch(i)
    {
     case 1:
      printf("n=");   /*輸入創建鏈表結點的個數*/
      scanf("%d",&n);
      h=CreateList(n);/*創建鏈表*/
      printf("list elements is : \n");
      PrintList(h);
      break;

     case 2:
      printf("input the position. of insert element:");
      scanf("%d",&i);
      printf("input name of the student:");
      scanf("%s",name);
      printf("input score of the student:");
      scanf("%d",&score);
      InsertList(h,i,name,score,n);
      printf("list elements is:\n");
      PrintList(h);
      break;

     case 3:
      printf("input the position of delete element:");
      scanf("%d",&i);
      DeleteList(h,i,name,score,n);
      printf("list elements in : \n");
      PrintList(h);
      break;

     case 4:
      printf("list element is : \n");
      PrintList(h);
      break;
     case 0:
      return;
      break;
     default:
      printf("ERROR!Try again!\n");
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章