Ⅰ c語言:刪除單鏈表的話是用free()嗎頭結點釋放了就行了嗎謝謝啊
當然亮羨渣要逐個free。因為每一個節敬悄點都是申請出來的,當用完了要釋放。
不派型能只釋放頭節點,那樣就是內存泄漏。
Ⅱ 菜鳥請教:c語言中怎麼刪除單鏈表中的某個節點
//L為敗罩租頭結點指針;察兆i為要刪除的結點序號
void  DelList(LinkList L,int i)//刪除指定位悶州置結點
{
 Node *pre,*r;
 int k=0;
 pre=L;
 if(i<1) return;
 while (pre->next!=NULL&&k<i-1)
 {
  pre=pre->next;
  k=k+1;
 }
 if (!(pre->next))
 {
  printf("刪除結點位置不合法");
  return;   
 }
 else
    {
  r=pre->next;
  pre->next=r->next;
free(r);
  printf("刪除成功\n");
  
  return;
 } 
}
Ⅲ C語言單鏈表中刪除所有值為a的元素
void deleteL(LinkNode *head,char a)//此鏈表假定帶頭結點,否則還要加一條對第一個結點的操作。LinkNode為定義的鏈表類型,a為指定的要刪除的字元。
{
ListNode *p,*q;
p=head;
while (p->next!=NULL)
    {if (p->液困next->data==a)
        {q=p->謹頃next;
         p->next=p->next->next;//刪除指定的字元
         free(q);
     else 
        p=p->next;/祥埋陸/不刪除,只是指針後移
    }
}
Ⅳ C語言單鏈表的刪除指定元素操作。
DelElem函數戚裂雹源判里
else
{
p=p->next;
q=p->next;
}
如果是你那樣q=p;q與p指向同一個地址怎麼會對呢?
希望你高帆採納。
Ⅳ 數據結構代碼(用C語言) 單鏈表的插入和刪除
單鏈表功能大全,嘿嘿
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
 int nDate;
 struct node *pstnext;
}Node;
//鏈表輸出
void output(Node *head)
{
 Node *p = head->pstnext;
 while(NULL != p)
 {
  printf("%d  ", p->nDate); 
  p = p->pstnext;
 }
 printf("\r\n");
}
//鏈表建立
Node* creat()
{
 Node *head = NULL, *p = NULL, *s = NULL;
 int Date = 0, cycle = 1;
 head = (Node*)malloc(sizeof(Node));
 if(NULL == head)
 {
  printf("分配內存失敗\r\n");
  return NULL;
 }
 head->好敏和pstnext = NULL;
 
 p = head;
 while(cycle)
 {
  printf("請輸入數據且當輸入數據為0時結束輸入\r\n");
  scanf("%d", &Date);
  if(0 != Date)
  {
   s = (Node*)malloc(sizeof(Node));
   if(NULL == s)
   {
    printf("分配內存失敗\r\友盯n");
    return NULL;
   }
   s->nDate = Date;
   p->pstnext = s;
   p = s;
  }
  else
  {
   cycle = 0;
  }
 }
 p->pstnext = NULL;
 return(head);
}
//單鏈表測長
void length(Node *head)
{
 Node *p = head->pstnext;
 int j=0;
 while(NULL != p)
 {
  p = p->pstnext;
  j++;
 }
 printf("%d\r\n", j);
}
//鏈表按值查找
void research_Date(Node *head, int date)
{
 Node *p;
 int n=1;
 p = head->pstnext;
 while(NULL != p && date != p->nDate)
 {
  p = p->pstnext;
  ++n;
 }
 if(NULL == p)
 {
  printf("鏈表中沒有找到該值");
 }else if(date == p->nDate)
 {
  printf("要查找的值%d在鏈表中第%d個位置\r\n", date, n);
 }
 return;
}
//按序號查找
void research_Number(Node *head, int Num)
{
 Node *p=head;
 int i = 0;
 while(NULL != p && i < Num)
 {
  p = p->pstnext;
  i++;
 }
 if(p == NULL)
 {
  printf("查找位置不合法\r\n");
 }else if(i == 0)
 {
  printf("查找位置為頭結點\r\n");
 }else if(i == Num)
 {
  printf("第%d個位置數據為%d\r\n", i, p->nDate);
 }
}
//在指定元素之前插入新結點
void insert_1(Node *head, int i, int Newdate)
{
 Node *pre = head, *New = NULL;
 int j = 0;
 while(NULL != pre && j < i-1)
 { 
  pre = pre->pstnext;
  j++;
 }
 if(NULL == pre || j > i-1)
 {
  printf("插拿鬧入位置不存在\r\n");
 }else
 {
  New = (Node*)malloc(sizeof(Node));
  if(NULL == New)
  {
   printf("分配內存失敗\r\n");
   return;
  }
  New->nDate = Newdate;
  New->pstnext = pre->pstnext;
  pre->pstnext = New;
 }
 
}
//在指定元素之後插入新結點
void insert_2(Node *head, int i, int Newdate)
{
 Node *pre = head, *New = NULL;
 int j = 0;
 while(NULL != pre->pstnext && j < i)
 {
  pre = pre->pstnext;
  j++;
 }
 if(j == i)
 {
  New = (Node*)malloc(sizeof(Node));
  if(NULL == New)
  {
   printf("分配內存失敗\r\n");
   return;
  }
  New->nDate = Newdate;
  New->pstnext = pre->pstnext;
  pre->pstnext = New;
 }else
 {
  printf("插入位置不存在\r\n");
 }
}
//刪除指定結點
void Delete_1(Node *head, int i3)
{
 Node *p = head, *pre = NULL;
 int j = 0;
 while(NULL != p && j < i3)
 {
  pre = p;
  p = p->pstnext;
  j++;
 }
 if(NULL == p)
 {
  printf("刪除位置不存在\r\n");
 }else
 {
  pre->pstnext = p->pstnext;
  free(p);
 }
}
//指定刪除單鏈表中某個數據,並統計刪除此數據的個數
int Delete_2(Node *head, int Delete_date)
{
 int count = 0;
 Node *p = head, *q;
 while(NULL != p->pstnext)
 {
  q = p->pstnext;
  if(q->nDate == Delete_date)
  {
   p->pstnext = q->pstnext;
   free(q);
   ++count;
  }
  else
  {
   p = q;
  }
 }
 return count;
}
//鏈表逆置
void Reverse_list(Node *head)
{
 Node *q, *s;
 if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
 {
  return;
 }
 q = head->pstnext->pstnext;
 head->pstnext->pstnext = NULL;
 while(NULL != q)
 {
  s = q->pstnext;
  q->pstnext = head->pstnext;
  head->pstnext = q;
  q = s;
 }
}
//單鏈表的連接
void connect_list(Node *head, Node *head_New)
{
 Node *p = head;
 while(NULL != p->pstnext)
 {
  p = p->pstnext;
 }
 p->pstnext = head_New->pstnext;
}
//單鏈表銷毀
void destroy_list(Node* head)
{
    while (NULL != head)
 {
  Node* temp = head;
  head = head->pstnext;
  free(temp);
 }
}
void main()
{
 int date, num;    //待查找數據
 int i3;     //指定刪除元素的位置
 int i1, i2, Newdate_1, Newdate_2;    //待插入的新數據
 int Delete_date, k;    //待刪除的數據與其個數
 Node *Head = NULL;   //定義頭結點
 Node *Head_New = NULL;
 //鏈表建立
 Head = creat();
 printf("輸出建立的單鏈表\r\n");
 output(Head);
 //單鏈表測長
 printf("單鏈表長度為\r\n");
 length(Head);
 //鏈表按值查找
 printf("請輸入待查找的數據\r\n");
 scanf("%d", &date);
    research_Date(Head, date);
 //鏈表按序號查找
 printf("請輸入待查找序號\r\n");
 scanf("%d", &num);
 research_Number(Head, num);
 //在指定第i1個元素之前插入新元素Newdate
 printf("在指定第i個元素之前插入新元素Newdate");
 printf("請輸入i與元素且以逗號間隔\r\n");
 scanf("%d,%d", &i1, &Newdate_1);
 insert_1(Head, i1, Newdate_1);
 printf("插入後新鏈表\r\n");
 output(Head); 
 //在指定第i2個元素之後插入新元素Newdate
 printf("在指定第i個元素之後插入新元素Newdate");
 printf("請輸入i與元素且以逗號間隔\r\n");
 scanf("%d,%d", &i2, &Newdate_2);
 insert_2(Head, i2, Newdate_2);
 printf("插入後新鏈表\r\n");
 output(Head); 
 //指定刪除i3元素
 printf("刪除元素的位置\r\n");
 scanf("%d", &i3);
 Delete_1(Head, i3);
 printf("刪除後新鏈表\r\n");
 output(Head);
 //指定刪除單鏈表中某個數據,並統計刪除此數據的個數
 printf("請輸入待刪除的元素\r\n");
 scanf("%d", &Delete_date);
 k = Delete_2(Head, Delete_date);
 printf("刪除後新鏈表\r\n");
 output(Head);
 printf("刪除指定元素在鏈表中的個數為:");
 printf("%d\r\n", k);
 //單鏈表逆置
 Reverse_list(Head);
 printf("逆置後輸出\r\n");
 output(Head);
 //單鏈表的連接
 printf("建立一個新鏈表\r\n");
 Head_New = creat();
 printf("輸出新鏈表");
 output(Head);
 printf("將新鏈表連接到原來鏈表的尾部並輸出\r\n");
 connect_list(Head, Head_New);
 output(Head);
         destroy_list(Head);
 return;
}
Ⅵ C語言鏈表 刪除
structLNode*delete(LNode*head)
{
LNode*node=head;
LNode**parent=&head;
doublemin,max;
printf("請輸入min和max:");
scanf("%lf%lf",&min,&max);
while(node)
{
//大於min和小於max,則刪除節點
if(node->data>min&&node->data<max)
{
*parent=node->next;
//如果你的LNode是malloc出來的,需要free(node);
node=*parent;
}
else
{
node=node->next;
parent=&node->next;
}
}
returnhead;
//這個邏輯不需要給你太多注釋,你只需要拿一支筆,手動畫一個3個節點的鏈表,
//然後從函數執行開始,一步一步去推怎麼執行的就懂了。
//至於你的程序的錯誤,沒幫你看
}
Ⅶ C語言中關於鏈表的刪除
所謂鏈表,就是用指針將內存中動態分配的結點空間,鏈接起來成一個表。
所以,建表的過程即是每次為新結點分配內存;因此,釋放空間的話,也要從頭到尾,一個一個結點的釋放,這樣才能全部釋放掉。
這段代碼釋放了整個鏈表空間內存;while循環的作用是從頭到尾釋放後續結點,如果直接free(pHead)則後面的結點將無法找到,那麼造成內存空間泄露。
另外,你的while循環存在一個錯誤,假設釋放了倒數第一個結點後,pHead指向最後一個結點,而最後一個結點的next為NULL,那麼這樣最後一個結點也沒有釋放掉,while就退出了。
while循環應該更正為:
while(pHead!=NULL)
{
pNext=pHead->next;
free(pHead);
pHead=pNext;
}
Ⅷ c語言單鏈表刪除操作 求大神指出錯在哪······並有詳細的刪除注意事項···謝了·······
目測代碼除了第一個int m;沒有分開之外沒有什麼錯誤
可能是你鏈表的其他操作除了錯誤,比如建立出錯
你可以把鏈表建立的函數也拿上來
Ⅸ C語言:寫一個函數,實現單鏈表的節點刪除功能
struct student *del(struct student *head,long num)
{
 struct student *p1,*p2;
 long s=num;
 int n=0;
 p1=p2=head;
 if(head==NULL)
 {
  goto s;
 }
 else
 {
  if(s==p1->num)
  {
     p1=p1->氏亮冊next;
     n=n+1;
     head=p1;
  }
     else
  {
     p1=p1->鍵激next;
     while(p1!=NULL)
     {
       if(s==p1->num)
    {
       p2->next=p1->next;
       n=n+1;
       break;
    }
       p2=p2->next;
    p1=p1->next;
     }
  }
 }
s:
 return(head);
}刪除單向鏈表結殲宏點!
Ⅹ C語言,單向鏈表 不知道前驅指針,如何刪除某一節點。 情況考慮詳盡一些!
偽代碼:
if node.age ==30
delete(node)
delete是封裝好了的代碼
刪除一個節點的步驟:
如我有一個鏈表:A B C D 四個元素
我要刪除B,則有
p =A.next
A.next = p.next
free(p)
主要是思想對就行,一定要封裝
你這個屬於那種在線刪除
就是先查找,如果符合條件,就刪除。信野
刪除操作,必須知道有刪除的節點的前一個節點的地址才行。
那麼你應散中該每次查找的時候不是判斷當前節點是否符合要求,而是判斷下一個節點。如果符合就按照刪除節點的刪除來。
刪除操作的沖坦山三部曲:
第一步:保存刪除節點的地址
第二步:將刪除節點的前一個節點的指針域指向刪除節點的下一個
第三部:free 需要刪除節點
我現在直接在這里寫代碼了
void del(head,age)
{
p = head;
while(p)
{
if( p->next && p->next->age == age )//刪除操作的三步曲
{
q=p->next;
p->next = q ->next;
free(q);
}//end if
p = p->next;
}//end while
}
代碼就是這樣的。很簡單。這段代碼並不能編譯,你加上類型就好了。我強調的是方法。
