欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言 數(shù)據(jù)結構鏈表的實例(十九種操作)

 更新時間:2017年07月17日 09:59:49   投稿:lqh  
這篇文章主要介紹了C語言 數(shù)據(jù)結構鏈表的實例(十九種操作)的相關資料,需要的朋友可以參考下

C語言 數(shù)據(jù)結構鏈表的實例(十九種操作)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*************************************************************************************/
/* 第一版博主 原文地址 http://www.cnblogs.com/renyuan/archive/2013/05/21/3091506.html */
/* 第二版博主 原文地址 http://www.cnblogs.com/wireless-dragon/p/5170565.html */
/* 2.創(chuàng)建線性表,此函數(shù)輸入不為正時終止讀取數(shù)據(jù)*/
/* 3.打印鏈表,鏈表的遍歷 */
/* 4.查詢鏈表結點數(shù)并返回長度 */
/* 5.檢查單鏈表是否為空 */
/* 6.將線性表進行冒泡排序 */
/* 7.查找單鏈表中第n個結點中的元素 */
/* 8.從單鏈表中查找具有給定值number的第一個元素,返回該結點的地址 */
/* 9.把單鏈表中第n個結點的值修改為number的值 */
/* 10.向單鏈表的表頭插入一個元素 */
/* 11.向單鏈表的末尾添加一個元素 */
/* 12.向單鏈表中第n個結點位置插入元素為x的結點 */
/* 13.向有序單鏈表中插入元素x結點,使得插入后仍然有序 */
/* 14.從單鏈表中刪除表頭結點 */
/* 15.從單鏈表中刪除表尾結點 */
/* 16.從單鏈表中刪除第n個結點 */
/* 17.從單鏈表中刪除值為x的第一個結點 */
/* 18.交換2個元素的位置 */
/* 19.刪除列表 */
/*************************************************************************************/
typedef int elemType;
typedef struct NODE
{
  elemType element;
  struct NODE *next;
} Node;
/* 2.創(chuàng)建線性表,此函數(shù)輸入不為正時終止讀取數(shù)據(jù)*/
void creatList(Node **pHead)
{
  printf("Please enter the list:\n");
  Node *p1, *p2;
  p1 = p2 = (Node *)malloc(sizeof(Node));
  if (p1 == NULL || p2 == NULL)
    exit(0);
  memset(p1, 0, sizeof(Node));
  scanf("%d", &p1->element);
  p1->next = NULL;
  while(p1->element > 0)
  {
    if (*pHead == NULL)
      (*pHead) = p1;
    else
      p2->next = p1;
    p2 = p1;
    p1 = (Node *)malloc(sizeof(Node));
    if (p1 == NULL)
      exit(0);
    memset(p1, 0, sizeof(Node));
    scanf("%d", &p1->element);
    p1->next = NULL;
  }
}
/* 3.打印鏈表,鏈表的遍歷 */
void printList(Node *pHead)
{
  if (NULL == pHead)
    printf("The list is empty\n");
  else
    while(NULL != pHead)
    {
      printf("%d ", pHead->element);
      pHead = pHead->next;
    }
  printf("\n");
}
/* 4.查詢鏈表結點數(shù)并返回長度 */
int sizeList(Node *pHead)
{
  int size = 0;
  while(pHead != NULL)
  {
    size ++;
    pHead = pHead->next;
  }
  return size;
}
/* 5. 檢查單鏈表是否為空 */
void isEmptyList(Node *pHead)
{
  if (pHead == NULL)
  {
    printf("The list is empty\n");
    exit(0);
  }
}
/* 7.查找單鏈表中第n個結點中的元素 */
void getElement(Node *pHead, int num)
{
  for (int i = 1; i < num; ++i)
    pHead = pHead->next;
  printf("The value of the %dth element is:%d\n", num, pHead->element);
}
/* 8.從單鏈表中查找具有給定值number的第一個元素,返回該結點的地址 */
int getElemAddr(Node *pHead, int number)
{
  int i = 1;
  while(pHead != NULL)
  {
    if (pHead->element == number)
      return i;
    i++;
    pHead = pHead->next;
  }
  return 0;
}
/* 9.把單鏈表中第n個結點的值修改為number的值 */
void modifyElem(Node **pList, int addr, int number)
{
  Node *pHead; //在此處如果直接更改pList指向的話,主函數(shù)中調(diào)用printList就會從addr處開始打印
  int i = 1;
  pHead = *pList;
  while(pHead != NULL)
  {
    if (i == addr)
      break;
    pHead = pHead->next;
    i++;
  }
  pHead->element = number;
}
/* 10.向單鏈表的表頭插入一個元素 */
void insertHeadList(Node **pHead)
{
  Node *p1;
  p1 = (Node *)malloc(sizeof(Node));
  if (p1 == NULL)
    exit(0);
  memset(p1, 0, sizeof(Node));
  printf("Please enter a number to be inserted:");
  scanf("%d", &p1->element);
  p1->next = (*pHead);// 此時pHead指向的是第一個結點(有數(shù)據(jù)域的),所以新的結點要插入到頭結點前
  (*pHead) = p1; // pHead指向第一個結點
}
/* 11.向單鏈表的末尾添加一個元素 */
void insertLastList(Node **pHead, int n)
{
  Node *p1, *p2;
  p2 = (*pHead);
  int i;
  for (i = 1; i < n; ++i)
    p2 = p2->next;
  p1 = (Node *)malloc(sizeof(Node));
  if (p1 == NULL)
    exit(0);
  memset(p1, 0, sizeof(Node));
  printf("Please enter a number to be inserted:");
  scanf("%d", &p1->element);
  p1->next = NULL;
  p2->next = p1;
}
/* 12.向單鏈表中第n個結點位置插入元素為x的結點 */
void isAddPos(Node **pHead, int length)
{
  Node *p1, *p2;
  int position, i;
  printf("Please enter the insert position:");
  scanf("%d", &position);
  if (position > length || position <= 0)
  {
    printf("Input error, the program ends\n");
    exit(0);
  }
  p1 = (Node *)malloc(sizeof(Node));
  p2 = (*pHead);
  if (p1 == NULL)
    exit(0);
  memset(p1, 0, sizeof(Node));
  printf("Please enter a number to be inserted:");
  scanf("%d", &p1->element);
  for (i = 1; i < position - 1; ++i)
    p2 = p2->next;
  p1->next = p2->next;
  p2->next = p1;
}
/* 6.將線性表進行冒泡排序 */
void Arrange(Node **pHead, int length)
{
  Node *p1;
  p1 = (*pHead);
  int i, j, temp;
  for (i = length; i > 0; --i)
  {
    for(j = i - 1; j > 0; --j)
    {
      if ((p1->element) > (p1->next->element))
      {
        temp = p1->element;
        p1->element = p1->next->element;
        p1->next->element = temp;
      }
      p1 = p1->next;
    }
    p1 = (*pHead);
  }
}
int OrrderList(Node **pHead, int length)
{
  Node *p1, *p2;
  p1 = (*pHead);
  p2 = (Node *)malloc(sizeof(Node));
  if (p2 == NULL)
    exit(0);
  memset(p2, 0, sizeof(Node));
  printf("Enter the value of the element to be inserted:");
  scanf("%d", &p2->element);
  if (p2->element < p1->element)
  {
    p2->next = p1;
    (*pHead) = p2;
    return 1;
  }
  while(p1->next != NULL && p2->element > (p1->next->element))
    p1 = p1->next;
  if (p1->next == NULL)
  {
    p2->next = NULL;
    p1->next = p2;
    return 1;
  }
  else
  {
    p2->next = p1->next;
    p1->next = p2;
    return 1;
  }
}
/* 14.從單鏈表中刪除表頭結點 */
void DelHeadList(Node **pHead)
{
  Node *p1;
  p1 = (*pHead);
  (*pHead) = (*pHead)->next;
  free(p1);
}
/* 15.從單鏈表中刪除表尾結點 */
void DelLastList(Node **pHead)
{
  Node *p1, *p2;
  p1 = (*pHead);
  p2 = p1->next;
  while(p2->next != NULL)
  {
    p2 = p2->next;
    p1 = p1->next;
  }
  p1->next = NULL;
  free(p2);
}
/* 16.從單鏈表中刪除第n個結點 */
void DelPos(Node **pHead, int length)
{
  int n, i;
  Node *p1, *p2;
  p1 = (*pHead);
  p2 = p1->next;
  printf("Please enter the serial number number to delete:");
  scanf("%d", &n);
  if (n < 1 || n > length)
    exit(0);
  for (i = 1; i < n - 1; ++i)
  {
    p2 = p2->next;
    p1 = p1->next;
  }
  p1->next = p2->next;
  free(p2);
}
/* 17.從單鏈表中刪除值為x的第一個結點 */
int Delx(Node **pHead)
{
  Node *p1, *p2;
  p1 = (*pHead);
  p2 = p1->next;
  int number;
  printf("Please input is going to be deleted the value of x:");
  scanf("%d", &number);
  if (number == (*pHead)->element)
  {
    (*pHead) = (*pHead)->next;
    free(p1);
    return 1;
  }
  while(p2 != NULL)
  {
    if (p2->element == number)
    {
      break;
    }
    p2 = p2->next;
    p1 = p1->next;
  }
  if (p2 == NULL)
  {
    printf("X does not exist in the list\n");
    return 1;
  }
  else
  {
    p1->next = p2->next;
    free(p2);
    return 1;
  }
}
/* 18.交換2個元素的位置 */
void exchange2pos(Node **pHead, int length)
{
  Node *p1, *p2;
  int n1, n2, i, j, temp;
  printf("Please enter the first number:");
  scanf("%d", &n1);
  printf("Please enter the second number:");
  scanf("%d", &n2);
  if (n1 < 1 || n1 > length || n2 < 1 || n2 > length)
    exit(0);
  p1 = p2 = (*pHead);
  for (i = 1; i < n1; ++i)
  {
    p1 = p1->next;
  }
  for (j = 1; j < n2; ++j)
  {
    p2 = p2->next;
  }
  temp = p1->element;
  p1->element = p2->element;
  p2->element = temp;
}
/* 刪除列表 */
void clearList(Node **pHead)
{
  Node *p1;
  p1 = (*pHead);
  while(p1 != NULL)
  {
    p1 = p1->next;
    free((*pHead));
    (*pHead) = p1;
  }
}
int main(int argc, char const *argv[])
{
  /* 1.初始化線性表,即置單鏈表的表頭指針為空 */
  Node *pList = NULL;
  int length = 0, n, addr, number;
  /* 2.創(chuàng)建線性表,此函數(shù)輸入不為正時終止讀取數(shù)據(jù)*/
  printf("- - - - - - - - - 2 - - - - - - - -\n");
  creatList(&pList);
  /* 5. 檢查單鏈表是否為空 */
  isEmptyList(pList);
  printList(pList);
  /* 4.查詢鏈表結點數(shù)并返回長度 */
  printf("- - - - - - - - - 4 - - - - - - - -\n");
  length = sizeList(pList);
  printf("the Node length is:%d\n", length);
  /* 7.查找單鏈表中第n個結點中的元素 */
  printf("- - - - - - - - - 7 - - - - - - - -\n");
  printf("Please input node number (n):");
  scanf("%d", &n);
  if (n > length || n < 1)
  {
    printf("N is not within the scope of\n");
    exit(0);
  }
  getElement(pList, n);
  /* 8.從單鏈表中查找具有給定值number的第一個元素,返回該結點的地址 */
  printf("- - - - - - - - - 8 - - - - - - - -\n");
  addr = 0;
  number;
  printf("Please enter to find element value (number):");
  scanf("%d", &number);
  addr = getElemAddr(pList, number);
  if (addr == 0)
    printf("List the element\n");
  else
    printf("The location of the number is:%d\n", addr);
  /* 9.把單鏈表中第n個結點的值修改為number的值 */
  printf("- - - - - - - - - 9 - - - - - - - -\n");
  addr = 0;
  number = 0;
  printf("Please input to replace the serial number (n):");
  scanf("%d", &addr);
  if (addr > length || addr < 0)
  {
    printf("N is not within the scope of\n");
    exit(0);
  }
  printf("Please input to replace the contents of the (number):");
  scanf("%d", &number);
  modifyElem(&pList, addr, number);
  printf("The revised list is:\n");
  printList(pList);
  /* 10.向單鏈表的表頭插入一個元素 */
  printf("- - - - - - - - - 10 - - - - - - - -\n");
  insertHeadList(&pList);
  printList(pList);
  /* 11.向單鏈表的末尾添加一個元素 */
  printf("- - - - - - - - - 11 - - - - - - - -\n");
  insertLastList(&pList, length);
  printList(pList);
  /* 12.向單鏈表中第n個結點位置插入元素值為x的結點 */
  printf("- - - - - - - - - 12 - - - - - - - -\n");
  isAddPos(&pList, length);
  printList(pList);
  /* 6.將線性表進行冒泡排序 */
  printf("- - - - - - - - - 6 - - - - - - - -\n");
  Arrange(&pList, length);
  printList(pList);
  /* 13.向有序單鏈表中插入元素x結點,使得插入后仍然有序 */
  printf("- - - - - - - - - 13 - - - - - - - -\n");
  OrrderList(&pList, length);
  printList(pList);
  /* 14.從單鏈表中刪除表頭結點 */
  printf("- - - - - - - - - 14 - - - - - - - -\n");
  DelHeadList(&pList);
  printList(pList);
  /* 15.從單鏈表中刪除表尾結點 */
  printf("- - - - - - - - - 15 - - - - - - - -\n");
  DelLastList(&pList);
  printList(pList);
  /* 16.從單鏈表中刪除第n個結點 */
  printf("- - - - - - - - - 16 - - - - - - - -\n");
  DelPos(&pList, length);
  printList(pList);
  /* 17.從單鏈表中刪除值為x的第一個結點 */
  printf("- - - - - - - - - 17 - - - - - - - -\n");
  Delx(&pList);
  printList(pList);
  /* 18.交換2個元素的位置 */
  printf("- - - - - - - - - 18 - - - - - - - -\n");
  exchange2pos(&pList, length);
  printList(pList);
  /* 19.刪除列表 */
  printf("- - - - - - - - - 19 - - - - - - - -\n");
  clearList(&pList);
  printList(pList);
  return 0;
}

以上就是C語言數(shù)據(jù)結構單鏈表的操作,所有基礎操作都包含在內(nèi),很全面,本站對于數(shù)據(jù)結構的文章還很多,希望大家搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • C++小知識:用++i替代i++

    C++小知識:用++i替代i++

    今天小編就為大家分享一篇關于C++小知識:用++i替代i++,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Qt利用QDrag實現(xiàn)拖拽拼圖功能詳解

    Qt利用QDrag實現(xiàn)拖拽拼圖功能詳解

    QDrag類為MIME-based拖拽數(shù)據(jù)轉(zhuǎn)換提供支持。本文為大家主要介紹如何利用QDrag類實現(xiàn)拖拽拼圖功能。左邊是打散的圖,拖動到右邊進行復現(xiàn),此外程序還支持手動拖入原圖片,感興趣的可以了解一下
    2022-07-07
  • C++結構體作為函數(shù)參數(shù)傳參的實例代碼

    C++結構體作為函數(shù)參數(shù)傳參的實例代碼

    這篇文章主要介紹了C++結構體作為函數(shù)參數(shù)傳參的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • C++異常處理的方式總結

    C++異常處理的方式總結

    C++有一套獨立的異常處理機制,相信大家一定聽說過try,catch這兩
    個詞,今天就來做詳細的介紹,文中通過代碼示例給大家介紹的非常詳細,具有一定參考價值,需要的朋友可以參考下
    2023-12-12
  • C++11中隱式類型轉(zhuǎn)換的實現(xiàn)示例

    C++11中隱式類型轉(zhuǎn)換的實現(xiàn)示例

    C++類型轉(zhuǎn)換分為:隱式類型轉(zhuǎn)換和顯式類型轉(zhuǎn)換,本文主要介紹了C++11中隱式類型轉(zhuǎn)換的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細介紹

    解析wprintf 中使用%I64d格式化輸出LONGLONG的詳細介紹

    本篇文章是對wprintf 中使用%I64d格式化輸出LONGLONG進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++中四種對象生存期和作用域以及static的用法總結分析

    C++中四種對象生存期和作用域以及static的用法總結分析

    以下是對C++中四種對象生存期和作用域以及static的用法進行了詳細的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C++ pair方法與vector方法案例詳解

    C++ pair方法與vector方法案例詳解

    這篇文章主要介紹了C++ pair方法與vector方法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • C++11 上下文關鍵字的具體實踐

    C++11 上下文關鍵字的具體實踐

    本文主要介紹了C++11 上下文關鍵字的具體實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • C語言 詳細講解#pragma的使用方法

    C語言 詳細講解#pragma的使用方法

    #pragma 指令對每個編譯器給出了一個方法,在保持與C和C++語言完全兼容的情況下,給出主機或操作系統(tǒng)專有的特征。依據(jù)定義,編譯指示是機器或操作系統(tǒng)專有的, 且對于每個編譯器都是不同的
    2022-04-04

最新評論