迴圈鏈表 節點釋放釋放

來源:互聯網
上載者:User
#include <stdio.h>

#include <iostream>
#include "define.h"

using namespace std;

int main(int argc, char *argv[])
{
//建立單迴圈鏈表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);

#include <stdio.h>
#include <iostream>
#include "define.h"

using namespace std;

int main(int argc, char *argv[])
{
//建立單迴圈鏈表
node *head_cycle = NULL;
cout<<"create the cycle list(by queue):"<<endl;
head_cycle = createlist_cycle();
deletelist(head_cycle);

//建立鏈表,並返回鏈表數目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);

//計算鏈表的長度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;

//尋找輸入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin); //清空標準輸入緩衝區,方便下次使用 如果有多個輸入語句時 加入fflush用來清空緩衝區
locate(head, x);

//在鏈表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);

//鏈表逆序輸出
printf("\nOutput the inversing list!\n");
inverselist(head);

deletelist(head);
return true;

}

struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
printf("malloc failed!\n"); //申請記憶體失敗
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申請,並釋放,成對出現
}
tail->next = head;

Node head_insert = head;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;

if(head_insert==head)
{
break;
}
}

return head;
}

struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed!\n"); //申請記憶體失敗
return false;
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;
}
tail->next = NULL; //每次都要重新申請,並釋放,成對出現
}
return head;
}

int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
//return(n);

}

void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else
{
flag = 1; //設定標誌位進行判斷
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}

printlist(head->next);
}
}

int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
{
head=head->next;
n++;
}
if(head==NULL)
return(-1);
else
cout<<"the location of the element is "<<n+1<<endl;
return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;

head = p;
while(head)
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;

return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把頭指標傳過來
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1) //要考慮到插入到頭結點和插入到尾結點的不同
{
p->next = head;
head = p;
head_insert = head;
}

else if(i==(len+1)) //多用小括弧,防止出錯
{
head_insert = head;

int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL; //不能保證tail指標也能傳進來 插入與建立鏈表是兩碼事
}

else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}

else //第三種情況 插入到中間位置
{
head_insert = head;

while(j<i && head->next!=NULL)
{
head=head->next;
j++;
if(j==i)
{
p->next = head->next;
head->next = p;
}
//else
// cout<<"The parameter of location is incorrect!"<<endl;

}

}

//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}

return 1;
}

void deletelist(node *head)
{
Node p, q;
p = head->next;

while(p!=head) //
{
q = p->next;
free(p); //
p = q;
}

free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}
//建立鏈表,並返回鏈表數目
node *head = NULL;
cout<<"create the single list(by queue):"<<endl;
head = createlist();
cout<<"print the single list:"<<endl;
printlist(head);

//計算鏈表的長度
int n = linklength (head);
cout<<"the length of the single list is:"<<n<<endl;

//尋找輸入元素的位置
char x;
printf("Please input an element and return the location of the element!\n");
scanf("%c", &x);
fflush(stdin);    //清空標準輸入緩衝區,方便下次使用 如果有多個輸入語句時 加入fflush用來清空緩衝區
locate(head, x);

//在鏈表中插入元素
cout<<"please input one char and one num(please insert space between the two chracters):"<<endl;
char i;
int num;
scanf("%c %d", &i, &num);
fflush(stdin);
insertlist(head, i, num, n);

//鏈表逆序輸出
printf("\nOutput the inversing list!\n");
inverselist(head);

deletelist(head);
return true;

}

struct node_link *createlist_cycle()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));
if(p == NULL)
{
  printf("malloc failed!\n");           //申請記憶體失敗
  return false;  
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;

}
tail->next = NULL;                       //每次都要重新申請,並釋放,成對出現
}
tail->next = head;

Node head_insert = head;
while(head_insert)      
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;

if(head_insert==head)
{
break;
}
}

return head;
}

struct node_link *createlist()
{
char c;
node *tail;
node *head = NULL;
while((c=getchar()) != '\n')
{
Node p = (Node )malloc(sizeof(node));
if(p == NULL)
{
  printf("malloc failed!\n");           //申請記憶體失敗
  return false;  
}
p->data = c;
if(head==NULL)
{
head = p;
tail = p;
}
else
{
tail->next = p;
tail = p;

}
tail->next = NULL;                       //每次都要重新申請,並釋放,成對出現
}
return head;
}

int linklength(node *head)
{
if(head == NULL)
return 0;
else
return( 1+linklength(head->next) );
//return(head);
  //return(n);

}

void printlist(node *head)
{
if(head==NULL && flag==0)
cout<<"the list is null!"<<endl;
else      
{
flag = 1;  //設定標誌位進行判斷
cout<<head->data<<"->";
if(head->next==NULL)
{
cout<<endl;
return;
}

printlist(head->next);
}
}

int locate(node *head, char x)
{
int n=0;
while (head!=NULL && head->data!=x)
  {
  head=head->next;
n++;
  }
  if(head==NULL)
  return(-1);
  else
cout<<"the location of the element is "<<n+1<<endl;
  return(n+1);
}
struct node_link *inverselist(node *head)
{
Node head_node;
Node p, q, r;
p = head;
q = p->next;
while(q!=NULL)      
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head_node = p;

head = p;
while(head)      
{
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;

return head_node;
}
int insertlist(node *head,char x, int i, int len) //一定要把頭指標傳過來
{
Node head_insert;
Node p = new node[];
p->data = x;
int j = 1;
if(i==1)  //要考慮到插入到頭結點和插入到尾結點的不同
{
p->next = head;
head = p;
head_insert = head;
}

else if(i==(len+1)) //多用小括弧,防止出錯
{
head_insert = head;

int cnt = 0;
while(cnt!=len-2)
{
head = head->next;
}
head->next = p;
p->next = NULL;  //不能保證tail指標也能傳進來 插入與建立鏈表是兩碼事
}

else if(i>=len+1)
{
cout<<"The parameter of location is incorrect!"<<endl;
return 0;
}

else  //第三種情況 插入到中間位置
{
head_insert = head;

while(j<i && head->next!=NULL)
{
    head=head->next;
    j++;
    if(j==i)
      {
p->next = head->next;
head->next = p;
      }
//else 
    //
cout<<"The parameter of location is incorrect!"<<endl;

}

}

//return head_insert;
cout<<"after inserting, print the single list:"<<endl;
while(head_insert)      
{
cout<<head_insert->data<<"->";
head_insert = head_insert->next;
}

return 1;
}

void deletelist(node *head)
{
Node p, q;
p = head->next;

while(p!=head)   // 
{
q = p->next;
free(p);     //
p = q;
}

free(head);
head = NULL;
//cout<<"head == NULL"<<endl;
}問題1:  申請多少記憶體,再釋放多少記憶體,雖然話是這麼說,但是一定要親力親為Node p = (Node )malloc(sizeof(node));
//Node p = (Node )malloc(sizeof(Node));導致節點無法釋放,申請的一個結構體,與申請一個結構體指標是不同的問題2:sanf函數的使用,要注意兩次使用需要清空緩衝區,
所示的錯誤並不是 不能無從下手的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.