Linked list

Singly Linked list

In a singly linked list, we omit the prev pointer in each element
Address of the head node give us access of the complete list

Array VS linked list

Array Linked list
Cost of accessing an element Ο(1) Ο(n) in average case
Memory requirement fixed size no unused memory , extra memory for pointer variables, more flexible
Cost of Inserting element at beginning Ο(n) Ο(1)
Inserting element at the end ① Ο(1) if array isn't full. ② Ο(n) if array is full. Ο(n)
Inserting element at ith element Ο(n) Ο(n)

How to create a linked list

Implement in C :
1.create a node first:

struct Node
{
    int data;
    struct Node* next;   
}* head;            

2.Put your element in the list:

void Put(int x)
{
    struct Node* temp =(struct Node*)malloc(sizeof(struct Node));  //create a node
    temp->data=x;  //put in the data
    temp->next=head;  //make next node as head
    head=temp;  //let head point to the next node
}

3.Traverse the linked list
psudocode first:

node* temp=A
while( temp -> link != NULL)
{
   temp=temp -> link;
   print" temp ->data
}

In C code:

void Display()
{
    struct Node* temp=head;
    printf("Current list is ");
    while(temp !=NULL)  //traverse
    {
        printf("%d",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

4.Free memory
psudocode first:

node = head              
while node != null:     
    temp = node         
    node = node.next    
    free temp          
head = null 

In C code:

void Free(struct Node*head)
{
    struct Node*temp;
    while(head != NULL)  
    {
        temp=head;  
        head=head->next;  
        free(temp);  
    }
    head=NULL;  
}

View complete C code of create linked list here.

Inserting into a linked list

LIST -Insert (L, x)
next [x ]←head [L]
if head [L]≠NIL 
then prev [head [L ]]←x 
head [L]←x 
prev [x]←NIL

Implement in C:

void Insert(int data,int n)
{
    struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));
    temp1->data=data;
    temp1->next=NULL;
    if(n == 1)  
    {
        temp1->next=head;  
        head=temp1;  
        return;
    }
    struct Node* temp2=(struct Node*)malloc(sizeof(struct Node));
    temp2=head;
    int i;
    for(i=0; i<n-2; i++)  
    {
        temp2 = temp2->next;
    }
    temp1->next = temp2->next;
    temp2->next = temp1;
}

View complete C code of insertion here.

Searching a linked list

List _ Search(L,  k)
x ←head[L]
   while x≠NIL and key[x]≠k
   do x←next[x]
return x

It takesΘ(n) time in the worst case.
**Implement in C: **

void Search(int n)
{
    while(head != NULL)
    {
        if(head ->data == n)
        {
            printf("element found\n");
            return;
        }
        head =head->next;
    }
    printf("element don't exist in this list");
}

View complete C code of Search particular element in linked list here.

Delete

Delete at nth position :
void Delete(int n)
{
    struct Node* temp1=head;  
    if(n==1)
    {
        head=temp1->next;   
        free(temp1);  
        return;
    }
    int i;
    for(i=0; i<n-2; i++)
    {
        temp1=temp1->next;  
    }
    struct Node*temp2 = temp1-> next;  
    temp1->next = temp2->next;  
    free(temp2);   
}

**View complete Delete code here. **

Sort linked list

We use merge sort to sort linked list
1.Split your list into two parts


void split(struct node* Node,struct node **front,struct node **back)
{
    struct node* a;
    struct node* b;
    if(Node==NULL || Node->next==NULL)
    {
        *front=Node;
        *back=NULL;
    }
    else
    {
        a=Node;
        b=Node->next;
        while(b != NULL)
        {
            b=b->next;
            if(b!= NULL)
            {
                a=a->next;
                b=b->next;
            }
        }
        *front=Node;
        *back =a->next;
        a->next= NULL;
    }
}
struct node* merge(struct node *a,struct node *b)
{
    struct node* list=NULL;
    if(a==NULL)
    {
        return b;
    }
    else if(b==NULL)
    {
        return a;
    }
    if(a->data > b->data)
    {
        list=b;
        list->next=merge(a,b->next);
    }
    else
    {
        list=a;
        list->next=merge(a->next,b);
    }
    return list;
}
void mergesort(struct node** Node)
{
    head=*Node;
    struct node* a=NULL;
    struct node* b=NULL;

    if(head==NULL || head->next== NULL)
    {
        return;
    }
    split(head,&a,&b);
    mergesort(&a);
    mergesort(&b);
    *Node=merge(a,b);
}

Complete code here

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,018評論 0 23
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,891評論 0 33
  • 周五的晚上,高峰期,實(shí)在不好打車。她想取消訂單的那一秒,系統(tǒng)分配了一輛車,2.6公里開過來,還都是紅色的擁堵路段。...
    良仔l(wèi)z閱讀 142評論 0 0
  • 柴靜的《看見》是從她剛進(jìn)央視的時候,歷經(jīng)10年風(fēng)雨成長,所寫出來的一本書。在這本書中我們不光能了解柴靜這個人,還能...
    錢萊愛讀書閱讀 481評論 0 1
  • 艾蘇搬著小板凳坐在母親的身邊,母親正用一把大鐵剪在給公雞放血。公雞的脖子和翅膀被母親的大手牢牢地握著,鮮紅的血從脖...
    斑斑的四喜丸子閱讀 461評論 0 2

友情鏈接更多精彩內(nèi)容