單鏈表的創建、測長、打印、插入和刪除

/鏈表插入刪除參考1/
#include<stdio.h>
#include<malloc.h>


typedef struct node //定義鏈表
{
	int data;
	struct node *next;
}snode;

snode* creat() //創建鏈表的函數
{
	snode *head, *p, *q;
	head = (snode *)malloc(sizeof(snode));
	p = head;
	int x;
	printf("請輸入創建鏈表的值,用-1結束輸入 ");
	printf("x = ");
	scanf("%d", &x);
	while (x != -1)
	{
		q = (snode *)malloc(sizeof(snode));
		q->data = x;
		p->next = q;
		p = q;
		printf("x = ");
		scanf("%d", &x);
	}
	p->next = NULL;
	return head;
}
int length(snode *head)//測鏈表的結點數
{
	snode *p = head->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}
void display(snode *head) //依次輸出每個結點的值
{
	snode *p = head->next;
	for(int i = 0; i < length(head); i++)
	{
		printf("%4d", p->data);
		p = p->next;
	}
	printf(" ");
}

int locate(snode *head, int x) //測x在鏈表中的位置
{
	snode *p = head->next;
	int i = 1;
	while (p != NULL && x != p->data)
	{
		p = p->next;
		i++;
	}
	if (p == NULL) //什麼叫等於,兩個等號才叫等於.
		return 0;
	else
		return i;
}

int insnode(snode *head, int x, int i) //把x插入到鏈表的第i的位置
{
	snode *p = head->next, *s;
	int j;
	if(i < 1 || i > length(head) + 1)
		return 0;
	else if (i == 1)
	{
		s = (snode *)malloc(sizeof(snode));
		s->next = p;
		head->next = s;
		s->data = x;
	}
	else
	{
		for (j = 1; j < i - 1; j++)
			p = p->next;
		s = (snode *)malloc(sizeof(snode));
		s->next = p->next;
		p->next = s;
		s->data = x;
	}
	return 1;
}

int delnode(snode *head, int i)//刪除鏈表中第i個結點
{
	snode *p = head->next, *q = head;
	if(i < 1 || i > length(head))
		return 0;
	else if (i == 1)
	{
		head->next = p->next;
		free(p);
	}
	else
	{
		for (int j = 1; j < i; j++)
		{
			p = p->next;
			q = q->next;
		}
		q->next = p->next;
		free(p);
	}
	return 1;
}

void sort(snode *head) //把鏈表中每個結點的值按從小到大排列
{
	snode *p, *q;
	int k;
	for(p = head->next; p != NULL; p = p->next)
		for(q = p->next; q != NULL; q = q->next)
			if (p->data > q->data)
			{
				k = p->data;
				p->data = q->data;
				q->data = k;
			}
}

void insert(snode *head, int x) //在有序鏈表中插入x,插入後仍保持有序
{
	snode *p = head->next, *s, *q = head;
	while (p != NULL && p->data < x)
	{
		q = q->next;
		p = p->next;
	}
	s = (snode *)malloc(sizeof(snode));
	s->next = q->next;
	s->data = x;
	q->next = s;
}

void del_min_max(snode *head, int min, int max) //刪除有序鏈表中值min到值max中的結點
{
	snode *p = head->next, *q = head;
	while (p != NULL && p->data <= min)
	{
		q = p;
		p = p->next;
	}
	while (p != NULL && p->data < max)
	{
		q->next = p->next;
		free(p);
		p = q->next;
	}
}

void del_min(snode *head) // 刪除數據域最小的結點
{
	snode *p = head->next, *q = head;
	snode *p_min, *q_min;
	p_min = p;
	q_min = q;
	while (p != NULL)
	{
		q = p;
		p = p->next;
		if (p != NULL && p->data < p_min->data)
		{
			q_min = p_min;
			p_min = p;
		}
	}
	q_min->next = p_min->next;
	free(p_min);
}

int main(void)
{
	snode *headl = creat(); //創建鏈表
	printf("最初的鏈表如下: ");
	display(headl);
	int num, location;
	printf("請輸入您要查找的數:");
	scanf("%d", &num);
	if (locate(headl, num))
		printf("數字%d在鏈表中的位置爲%d ", num, locate(headl, num));
	else
		printf("數字%d在鏈表中不存在 ", num);
	printf("請分別輸入您要插入到鏈表中的數以及想插入的位置:");
	scanf("%d %d", &num, &location);
	if (insnode(headl, num, location))
	{
		printf("插入新值以後的鏈表如下: ");
		display(headl);
	}
	else
		printf("輸入有誤 ");
	printf("請輸入您想刪除的結點位置:");
	scanf("%d", &location);
	if (delnode(headl, location))
	{
		printf("刪除第%d個結點後的鏈表如下: ", location);
		display(headl);
	}
	else
	printf("輸入有誤! ");
	sort(headl); //排序
	printf("經過把結點數據按從小到大排序以後的鏈表如下: ");
	display(headl);
	printf("請輸入一個將被插入到有序鏈表中的數:");
	scanf("%d", &num);
	insert(headl, num);
	printf("將%d插入到有序鏈表中後,鏈表仍然有序,如下: ", num);
	display(headl);
	int min, max;
	printf("請輸入需要刪除的一段結點的頭和尾,他們之間的結點將被刪除:");
	scanf("%d %d", &min, &max);
	del_min_max(headl, min, max);
	printf("經過刪除以後的鏈表如下: ");
	display(headl);
	printf("經過刪除最小數據域的結點以後的鏈表如下: ");
	del_min(headl);
	display(headl);
	return 0;
}
/鏈表插入、刪除例程2/
/*-------------------------包含頭文件------------------------------------*/
#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,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");
    }
  }
}

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